Check this validation rules documentation: http://laravel.com/docs/5.1/validation#available-validation-rules
How to I validate checkboxs in a form?
Hi,
I am trying to validate a form using Laravel 5.1.
One of the forms has multiple checkboxes. Which validating rule would I use to validate returned array? The values in the returned array must be integer >= 1.
Also, when I redirect the user back to the form using withInput, the checkbox are not auto filled where the rest are. how can I fix this?
@jillztom Thank you for your feedback. I check that page before asking this queston. I still don't know what to use. I am using array but that does not allow me to check the values with in it. also, I am not sure why when I am redirected to the form the checkboxes are not checked.
@jillztom
Thank you for that. that worked for the radio buttons but it is not working for the check the name of my check box will be fruit[]
Can you post your markup for the form?
here you go
<label class="checkbox-inline" for="item_1">
<input name="control_4[]" id="item_1" value="1" type="checkbox"> A
</label>
<label class="checkbox-inline" for="item_2">
<input name="control_4[]" id="item_2" value="2" type="checkbox"> B
</label>
<label class="checkbox-inline" for="item_3">
<input name="control_4[]" id="item_3" value="3" type="checkbox"> C
</label>
Try that without the [] when you check the condition in if statement.
@jillztom still not working.
I tried to dd the input::old, and I get this
control_3 = "Blah Blah",
control_4 = array (0 => 1, 1 =>2)
Okay I got the check box to be set on the error like this
{{ ( is_array( Input::old('fruit')) && in_array('orange', Input::old('fruit') ) ) ? 'checked' : '' }}
But I am still struggling on how to validate the array. I tried this validation rule but it is not working
integer|array|min:1
You can use min:value to validate a numeric value and you can also use it to validate an array's size.
Validator::make(
[ 'fruits' => Input::get('fruits') ],
[ 'fruits' => 'min:1' ]
);
$validator = Validator::make([
'fruits' => ['Orange', 'Apple', 'Grapes']
], ['fruits' => 'min: 4']);
$result = $validator->fails(); // returns true
@jillztom I am not looking for the size of the array. What I want to make sure that every value in the array is > 0 "unless the field is not required then empty will pass too"
You need to iterate over the array of inputs and add a rule to the rules array for each checkbox.
@Snapey Can you please tell how to do that? it looks like that I am trying to do is a new feature in 5.2 https://laravel-news.com/2015/11/laravel-5-2-a-look-at-whats-coming/ but I like to do it now with 5.1
public function rules()
{
//validating input text field named "owner"
$rules = ['owner' => 'required|max:15',];
//validating array of input text fields named "item"
foreach($this->request->get('item') as $a => $z)
{
$rules['item.'.$a] = 'required';
}
//validating array of checkbox named "fruit"
foreach($this->request->get('fruit') as $b => $y)
{
$rules['fruit.'.$b] = 'required';
}
return $rules;
}
Thissite will help
public function rules()
{
return [
'password' => 'required_if:password_checkbox,on|min:6|confirmed',
'password_confirmation' => 'required_if:password_checkbox,on|min:6|same:password'
];
}
Please or to participate in this conversation.