简体   繁体   English

如何验证laravel中的多行表单

[英]How to validate multi-row forms in laravel

Is it possible to validate multirow forms in laravel.是否可以在 Laravel 中验证多行表单。 I would like to validate a form which looks like this:我想验证一个看起来像这样的表单:

<input name="address[]" class="addr"/>
<input name="address[]" class="addr"/>

I've tried both methods but it doesn't seem to work:这两种方法我都试过了,但似乎不起作用:

$rule = array('address[]' => 'required'); //still returning error even if all required fields are filled up 
$rule = array('address' => 'required'); //nothing happens

For the first one I made sure that there is no hidden address field:对于第一个,我确保没有隐藏的地址字段:

$('.addr').length

The length returned is equal to the number of address fields I have filled out返回的长度等于我填写的地址字段数

Yes, use either of these:是的,请使用以下任一方法:

$rule = array('address' => 'array'); 
$rule = array('address' => 'countmin:0'); 

I just extended the validator class and created a simple rule that checks if there's an empty value in the array:我只是扩展了验证器类并创建了一个简单的规则来检查数组中是否有空值:

public function validate_arrayfull($attribute, $value, $parameters){
        $is_full = (in_array('', $value)) ? false : true;
        return $is_full;
    }

And in Validation.php a default error message:在 Validation.php 中,有一条默认的错误信息:

"arrayfull"      => "The :attribute contains empty values"

Usage:用法:

$rule = array('address' => 'arrayfull');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM