I am trying to validate a form in Laravel 5.3. The form has checkboxes. I need at least one checkbox to be selected for the form to be vald. This is my form
Here is my form
<div class="form-group {{ $errors->has('gender') ? 'has-error' : ''}}">
<div class="col-md-2"></div>
<div class="col-md-10">
<label for="gender_" class="checkbox-inline">
{!! Form::checkbox('gender', '', null, ['id' => 'gender_']) !!}
{{ trans('blogs.gender_') }}
</label>
<label for="gender_1" class="checkbox-inline">
{!! Form::checkbox('gender', '1', null, ['id' => 'gender_1']) !!}
{{ trans('blogs.gender_1') }}
</label>
<label for="gender_2" class="checkbox-inline">
{!! Form::checkbox('gender', '2', null, ['id' => 'gender_2']) !!}
Female
</label>
{!! $errors->first('gender', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}">
{!! Form::label('name',trans('blogs.name'),['class' => 'col-md-2 control-label']) !!}
<div class="col-md-10">
{!! Form::text('name',null, ['class' => 'form-control']) !!}
{!! $errors->first('name', '<p class="help-block">:message</p>') !!}
</div>
</div>
I am using FormRequest
object. Here is my rules()
method
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'gender' => 'required'
];
}
However, the validation fails unless I select every checkbox!
How can I correctly ensure that the validation only fails if no checkboxes is selected?