Laravel 3 - How to check a checkbox in a block for at least 1 check?

I am starting to learn Laravel and still on the learning curve. Now I start with Laravel 3, but most likely I will switch my project to Laravel 4 as soon as I get something working. Now the question is how to check the flag array, I want to check that at least 1 inside the group is enabled (checked). I read somewhere on the Laravel forum that we just check them using the required, but when I dd(input::all()) , I see nothing but the input field and the checkbox are not part of them ...

Part of my blade Create code for the flag:

 <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 code (REST):

 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 to get the status of the checkboxes after the check failed, I thought I saw Input::had(checkBoxName) somewhere, but this does not seem to work, maybe I am not using it correctly I got a little confused about this because the whole example that I see is for input and nothing else. I assume that validation is about the same in L4, right?

+4
source share
2 answers

Going back to this project and doing some more research, I found a better way for this problem:

My blade:

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

Explaining the type of blade is a two-step process, after checking :

  • Pull an array of labels (in my case "changeReasons []") with Input::old
  • From this array, we can search for a single flag and see if they are there if they then change the flag as checked . This job of the in_array () function, returning true / false, will change the state of the flag.

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

+3
source

Please remember that flags need this value. It is checked. The input :: get ('foo') will return 1, but if it is not set, it will not return anything, because it is not in the post-array.

I am using this code:

 if(Input::get('foo')){ $bar->is_foo = 1; } else{ $bar->is_foo = 0; } 
0
source

All Articles