0

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?

Jaylen
  • 39,043
  • 40
  • 128
  • 221
  • 1
    If `gender` is supposed to be multiple-choice, the name of the `input` tag should be `gender[]`. However, since you are only specifying Male and Female, did you mean to use `radio` input instead? – tyteen4a03 Jan 04 '17 at 02:45
  • http://stackoverflow.com/questions/23880126/laravel-4-validate-checkbox-at-least-one – Borna Jan 04 '17 at 03:04
  • @tyteen4a03 please ignore the language. I just want to be able to valid checkboxes. I am just trying to figure out the correct way to validate this. – Jaylen Jan 04 '17 at 03:13

1 Answers1

0

Your rules() looks correct so no require change for it. Try below:

<div class="form-group {{ $errors->has('gender') ? 'has-error' : ''}}">
  <div class="col-md-2"></div>
  <div class="col-md-10">
    <label for="gender_0" class="checkbox-inline">
      {!! Form::checkbox('gender[]', 0, null, ['id' => 'gender_0']) !!}
      {{ 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>

Hope this could help you!

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57