1
votes

I use laravel 5.3, this my snippet view code :

<tr>
        <td>
            <label class="radio-inline">
            <input name="val[1]" type="radio" value="1" >Rate 1</label>
            <label class="radio-inline">
            <input name="val[1]" type="radio" value="2" >Rate 2</label>
        </td>
    </tr>
    <tr>
        <td>
            <label class="radio-inline">
            <input name="val[2]" type="radio" value="1" >Rate 1</label>
            <label class="radio-inline">
            <input name="val[2]" type="radio" value="2" >Rate 2</label>
        </td>
    </tr>

and this my validation in controller :

$rules = array(
     'val[]'    => 'required|in:1,2,3,4,5',
);

$validator = Validator::make(Input::all(), $rules);

If I check all radio and submit form, the error appears with this message : The val[] field is required.

How to validate array of radios ?

Update this solution

<tr>
        <td>
            <label class="radio-inline">
            <input name="val[1]" type="radio" value="1" required="true" >Rate 1</label>
            <label class="radio-inline">
            <input name="val[1]" type="radio" value="2" required="true" >Rate 2</label>
        </td>
    </tr>
    <tr>
        <td>
            <label class="radio-inline">
            <input name="val[2]" type="radio" value="1" required="true" >Rate 1</label>
            <label class="radio-inline">
            <input name="val[2]" type="radio" value="2" required="true" >Rate 2</label>
        </td>
    </tr>

thank to answerers this question

2

2 Answers

1
votes

You can try it as:

$rules = array(
     'val'    => 'required|array',
     'val.*'    => 'required|in:1,2,3,4,5',
);
1
votes

Just do it like this:

$rules = array(
   'val'    => 'required|array',
   'val.*'    => 'required|in:1,2,3,4,5',
);

$validator = Validator::make(Input::all(), $rules);

Hope this helps!