在复选框Laravel验证,因而命名为数组 [英] Laravel validation on checkbox which named as array

查看:380
本文介绍了在复选框Laravel验证,因而命名为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些地区在我的形式是这样的:

I've some areas in my form something like:

<ul>
<li>
<input type="checkbox" name="post_categories[]" value="16">English First Main Category<br>
<ul>
<li><input type="checkbox" name="post_categories[]" value="17">English First Subcategory<br></li>
</ul>
</li>
</ul>

当我试图验证他们为必填字段或别的东西,Laravel没有验证的规则。我的规则类似下面(在/application/models/posts.php):

When I try to validate them as required fields or something else, Laravel did not validate rules. My rules something like below (In /application/models/posts.php):

public static $rules = array(

    'post_title' => 'required',
    'post_body' => 'required',
    'content_language'=>'required|alpha',
    'post_categories'=>'array_numeric',
    'post_sequence_number'=>'numeric'

    );


public static function validate($data){ 

    return Validator::make($data, static::$rules);

}

在/application/library/validate.php我有一个验证数组是数字或一个功能不:

In /application/library/validate.php I've a function that validates the array is numeric or not:

Class Validator extends Laravel\Validator {

        public function validate_array_numeric($attribute, $value, $parameters){
            $numeric_values = array_filter($value, create_function('$item', 'return (is_numeric($item));'));
            return count($numeric_values) == count($value);
        }

    }

规则完美的作品,除了post_categories []。我得到的错误:

Rules works perfectly, except post_categories[]. I get the error:

Method [array_numeric] does not exist.

干杯。

推荐答案

我不知道这个问题在Laravel 4.解决了也许你可以试试。
但是我在做什么,现在是扩展验证类。

I don't know if this issue has been solved in Laravel 4. Maybe you can try it. But what I'm doing right now is extending the validation class.

您可以创建一个扩展验证类新库。
为了验证如果阵列中的所有项目都有数值。这是应用程序/库

You can create a new library that extends the validation class. To validate if all the items in the array has numeric values. This is in application/libraries:

class Validator extends Laravel\Validator {

    public function validate_arraynumeric($attribute, $value, $parameters){
        $numeric_values = array_filter($value, create_function('$item', 'return (is_numeric($item));'));
        return count($numeric_values) == count($value);
    }

}

要更改默认的错误消息时验证失败。转到应用程序/语言/ EN / validation.php 。只需使用函数的名称作为新的数组项的关键是:

To change the default error message when the validation fails. Go to application/language/en/validation.php. Just use the name of the function as the key for the new array item:

"arraynumeric"   => "The :attribute contains non-numeric values",

更新

然后,你需要从删除以下行的application / config / application.php 文件:

Then you need to remove the following line from application/config/application.php file:


'验证'=&GT; Laravel \\\\验证

要使用新的验证:

public static $rules = array(
'post_categories'=>'array_numeric'
);

现在为所需的复选框。我以为你只是需要一个复选框进行检查。你可以只检查在功能新验证选中的复选框的数量,如果它至少是1。

Now for the required checkbox. I assume you're just requiring one checkbox to be checked. You can just check in the function for the new validation the count of the checked checkboxes if it is at least 1.

这篇关于在复选框Laravel验证,因而命名为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆