Laravel 3 - How to validate checkbox array, for at least 1 checked?
laravel, laravel-3
Solution
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:
- Pull the checkbox array (in my case 'changeReasons[]') with `Input::old`
- 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`.
Problem
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?