4

I'm starting to learn Laravel and still on the learning curve. Now I'm starting with Laravel 3 but will most probably switch my project into Laravel 4 once I get something working. Now the question being, how to validate an array of checkbox, I want to validate that at least 1 inside the group is enable(checked). I read somewhere on Laravel forum that we just validate them using a required, but when I dd(input::all()) I don't see anything else but the inputs field and checkbox are not part of them...

Part of my Blade Create code for the checkbox:

<label class="checkbox">{{ Form::checkbox('changeReasons[]', 'ckbCRCertification', Input::had('ckbCRCertification'), array('id' => 'ckbCRCertification')) }} Certification</label>
<label class="checkbox">{{ Form::checkbox('changeReasons[]', 'ckbCRDesignCorrection', Input::had('ckbCRDesignCorrection'), array('id' => 'ckbCRDesignCorrection')) }} Design Correction</label>

My controller (REST) code is:

public function post_create()
{
    print "Inside the post_create()";
    // validate input
    $rules = array(
        'ecoNo'             => 'min:4',
        'productAffected'   => 'required',
        'changeReasons'     => 'required'
    );

    $validation = Validator::make(Input::all(), $rules);

    if($validation->fails())
    {
        return Redirect::back()->with_input()->with_errors($validation);
    }

    $eco = new Eco;

    $eco->ecoNo = Input::get('ecoNo');
    $eco->productAffected = Input::get('productAffected');

    $eco->save();

    return Redirect::to('ecos');
}

I also want to know the correct code for getting the checkboxes state after a validation fails, I thought I saw the Input::had(checkBoxName) somewhere but that doesn't seem to work, I'm probably not using it correctly and I'm getting a little confuse on that since all example I see are for inputs and nothing else. I assume the validation is roughly the same in L4, would it be?

ghiscoding
  • 12,308
  • 6
  • 69
  • 112

2 Answers2

3

Going back on this project and making some more researches, I have found the best way for this problem is the following.

My blade view:

<div class="control-group row-fluid">
    <?php $arrChangeReasons =  Input::old('changeReasons', array()); // array of enable checkboxes in previous request ?>

    <label class="checkbox">{{ Form::checkbox('changeReasons[]', 'certification', in_array('certification', $arrChangeReasons)) }} Certification</label>
    <label class="checkbox">{{ Form::checkbox('changeReasons[]', 'designCorrection', in_array('designCorrection', $arrChangeReasons)) }} Design Correction</label>
</div>

The explanation of the blade view is a 2 steps process, after a validation occur, is the following:

  1. Pull the checkbox array (in my case 'changeReasons[]') with Input::old
  2. From that array we can then search for individual checkbox and see if they are in there, if they are then change the checkbox as a checked state. That is the job of the in_array() function, returning a true/false will change the state of the checkbox.

My controller (REST) code is exactly as it was written in my question at the beginning. For more information, defining $rules = array('changeReasons' => 'required'); will make sure that at least 1 of the checkboxes is checked.

ghiscoding
  • 12,308
  • 6
  • 69
  • 112
0

Please remember that Checkboxes need a value like . It the Checkbox is checked Input::get('foo') will return 1, but if it is unchecked it will return nothing, because it is not in the post-array.

I'm using this code:

if(Input::get('foo')){
    $bar->is_foo = 1;
}
else{
    $bar->is_foo = 0;
}
JackPoint
  • 4,031
  • 1
  • 30
  • 42
Bernd Strehl
  • 2,852
  • 4
  • 24
  • 43
  • 1
    Or you can use the default fallback like so `Input::get('foo', 0);` – Mirko Akov May 02 '13 at 06:39
  • 2
    Ok but how would I had this to my `Validator` check and make it return through the `return Redirect::back()->with_input()->with_errors($validation);` I want it to be part of my validations, not as an extra that I can't return at same time as inputs, if possible – ghiscoding May 02 '13 at 13:27
  • I think I am starting to understand why you use it this way, now the code you wrote would work for a single checkbox name but in my case I use array of checkbox, so how would I retrieve with 1 live of code of my checkbox is checked? I tried something like `Input::get('changeReasons[ckbCRCertification]', 0)` but that doesn't work since the `get` works only with single key and not arrays... So how to know if my checkbox is enable or not then? – ghiscoding May 02 '13 at 22:23
  • I'm not sure, but since the Controller doesn't know that you are using an boolean-type, you have to do it manually. Correct me if I'm wrong. – Bernd Strehl May 03 '13 at 09:45
  • Not much response on this.. I just want to know how to see if my checkbox is activated with 1 line of code... `Input::get('changeReasons', 0)` this would work for a simple checkbox by mine is an array of checkbox...so how to do it? I can't accept your answer as it is now.. – ghiscoding May 03 '13 at 19:58