Laravel 对名为数组的复选框进行验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13981670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel validation on checkbox which named as array
提问by dr.linux
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>
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):
当我尝试将它们验证为必填字段或其他内容时,Laravel 没有验证规则。我的规则如下(在/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);
}
In /application/library/validate.php I've a function that validates the array is numeric or not:
在 /application/library/validate.php 我有一个验证数组是否为数字的函数:
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);
}
}
Rules works perfectly, except post_categories[]. I get the error:
除了 post_categories[] 之外,规则工作得很好。我收到错误:
Method [array_numeric] does not exist.
Cheers.
干杯。
回答by planewalker
I had to solve the same problem. Here's what I did:
我不得不解决同样的问题。这是我所做的:
I created a custom class that extends the default Laravel\Validator
class. I wanted to be able to tell the validator when I'm dealing with multiple values (like in your case). In my implementation this could be done by appending '_array' to every validation rule for a certain field name. What my class does is to check if the rule name has this suffix and if it does the value parameter (which is an array in this case) is broken down to its contained items and passed to the default validation functions in Laravel.
我创建了一个扩展默认Laravel\Validator
类的自定义类。我希望能够在我处理多个值时告诉验证器(比如你的情况)。在我的实现中,这可以通过将“_array”附加到某个字段名称的每个验证规则来完成。我的类所做的是检查规则名称是否具有此后缀,以及是否将 value 参数(在本例中为数组)分解为其包含的项目并传递给 Laravel 中的默认验证函数。
<?php
class Validator extends Laravel\Validator {
public function __call($method, $parameters)
{
if (substr($method, -6) === '_array')
{
$method = substr($method, 0, -6);
$values = $parameters[1];
$success = true;
foreach ($values as $value) {
$parameters[1] = $value;
$success &= call_user_func_array(array($this, $method), $parameters);
}
return $success;
}
else
{
return parent::__call($method, $parameters);
}
}
protected function getMessage($attribute, $rule)
{
if (substr($rule, -6) === '_array')
{
$rule = substr($rule, 0, -6);
}
return parent::getMessage($attribute, $rule);
}
}
As stated in the posts above you will have to make the following change so that your custom Validator
class can be found by the Autoloader:
如上面的帖子所述,您必须进行以下更改,以便Validator
自动加载器可以找到您的自定义类:
Then you need to remove the following line from application/config/application.php file:
'Validator' => 'Laravel\Validator'
然后您需要从 application/config/application.php 文件中删除以下行:
'验证器' => 'Laravel\验证器'
When this is done you'll be able to use all of Laravel's validation rules with the '_array' suffix. Here's an example:
完成此操作后,您将能够使用带有“_array”后缀的所有 Laravel 验证规则。下面是一个例子:
public static $rules = array(
'post_categories'=>'required_array|alpha_array'
);
回答by Wern Ancheta
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.
不知道这个问题在Laravel 4中是否已经解决了,也许你可以试试。但是我现在正在做的是扩展验证类。
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
:
您可以创建一个扩展验证类的新库。验证数组中的所有项目是否都有数值。这是在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);
}
}
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:
验证失败时更改默认错误消息。去application/language/en/validation.php
。只需使用函数名称作为新数组项的键:
"arraynumeric" => "The :attribute contains non-numeric values",
update
更新
Then you need to remove the following line from application/config/application.php
file:
然后您需要从application/config/application.php
文件中删除以下行:
'Validator' => 'Laravel\\Validator'
'Validator' => 'Laravel\\Validator'
To use the new validation:
要使用新的验证:
public static $rules = array(
'post_categories'=>'array_numeric'
);
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.
现在是必需的复选框。我假设您只需要选中一个复选框。如果选中的复选框的数量至少为 1,则您只需签入新验证的函数即可。
回答by aebersold
You're doing something strange here.
你在这里做一些奇怪的事情。
Using post_categories[]
as form names generates an array. This means you cannot validate it with 'post_categories[]'=>'required|numeric'
. Instead you have to loop through the array and validate each field on it's own.
使用post_categories[]
作为表单名称生成的数组。这意味着您无法使用'post_categories[]'=>'required|numeric'
. 相反,您必须遍历数组并自行验证每个字段。