laravel验证无线电阵列

时间:2016-12-17 05:24:22

标签: php laravel validation laravel-5

我使用laravel 5.3,这是我的代码段视图代码:

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

这是我在控制器中的验证:

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

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

如果我检查所有收音机并提交表格,则会显示错误消息:val []字段是必需的。

如何验证无线电阵列?

更新此解决方案

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

感谢回答这个问题

2 个答案:

答案 0 :(得分:1)

您可以尝试:

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

答案 1 :(得分:1)

就这样做:

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

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

希望这有帮助!