Laravel 3 - How to validate checkbox array, for at least 1 checked?

泪湿孤枕 提交于 2019-12-21 18:39:42

问题


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?


回答1:


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.




回答2:


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;
}


来源:https://stackoverflow.com/questions/16326693/laravel-3-how-to-validate-checkbox-array-for-at-least-1-checked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!