我们如何验证数组形式的表单字段?看看下面的代码
用户手机型号:
public static $rules= array(
'phonenumber'=>'required|numeric',
'isPrimary'=>'in:0,1'
)
...........
用户控制器:
$validation = UserPhone::validate(Input::only('phonenumber')));
if($validation->passes())
{
$allInputs = Input::only('phonenumber','tid');
$loopSize = sizeOf($allInputs);
for($i=0;$i<$loopSize;$i++)
{
$phone = UserPhone::find($allInputs['tid'][$i]);
$phone->phonenumber = $allInputs['phonenumber'][$i];
$phone->save();
}
return Redirect::to('myprofile')->with('message','Update OK');
}
else
{
return Redirect::to('editPhone')->withErrors($validation);
}
}
$validation
来自扩展 Eloquent 的 BaseModel。
我认为:
<?php $counter=1; ?>
@foreach($phones as $thephone)
<section class="col col-12">
<label class="label">Phone Number {{$counter++}}</label>
<label class="input">
<i class="icon-append icon-phone"></i>
{{Form::text('phonenumber[]',$thephone->phonenumber)}}
{{Form::hidden('tid[]',$thephone->id)}}
</label>
</section>
@endforeach
一切正常,我在更新表单中得到了我想要的所有电话号码,但我无法更新模型,因为验证失败并显示消息“电话号码必须是数字”。
我知道验证数组表单字段没有一个简单的解决方案,我尝试扩展验证器类但没有成功。
如何验证此类字段?
这是我使用的解决方案:
只需添加前缀
each
即可改变您的常用规则。例如:
'names' => 'required|array|each:exists,users,name'
请注意,
each
规则假设您的字段是一个数组,因此不要忘记使用之前的array
规则,如此处所示。
错误消息将通过字段的单数形式(使用 Laravel 的
str_singular()
帮助器)自动计算。在前面的示例中,属性是 name
。
此方法开箱即用,适用于点表示法中任意深度的嵌套数组。例如,这有效:
'members.names' => 'required|array|each:exists,users,name'
同样,此处用于错误消息的属性将是
name
。
此方法支持任何开箱即用的自定义规则。
class ExtendedValidator extends Illuminate\Validation\Validator {
public function validateEach($attribute, $value, $parameters)
{
// Transform the each rule
// For example, `each:exists,users,name` becomes `exists:users,name`
$ruleName = array_shift($parameters);
$rule = $ruleName.(count($parameters) > 0 ? ':'.implode(',', $parameters) : '');
foreach ($value as $arrayKey => $arrayValue)
{
$this->validate($attribute.'.'.$arrayKey, $rule);
}
// Always return true, since the errors occur for individual elements.
return true;
}
protected function getAttribute($attribute)
{
// Get the second to last segment in singular form for arrays.
// For example, `group.names.0` becomes `name`.
if (str_contains($attribute, '.'))
{
$segments = explode('.', $attribute);
$attribute = str_singular($segments[count($segments) - 2]);
}
return parent::getAttribute($attribute);
}
}
在您常用的引导位置的任何位置,添加以下代码:
Validator::resolver(function($translator, $data, $rules, $messages)
{
return new ExtendedValidator($translator, $data, $rules, $messages);
});
就是这样!享受吧!
正如评论所指出的,似乎没有简单的方法来验证数组大小。然而,Laravel 文档缺乏大小规则:它没有提到它可以计算数组元素。这意味着您实际上可以使用
size
、min
、max
和 between
规则来计算数组元素。
最好扩展 Validator 类并重用现有的 Validator 函数:
Validator::resolver(function($translator, $data, $rules, $messages)
{
return new Validation($translator, $data, $rules, $messages);
});
class Validation extends Illuminate\Validation\Validator {
/**
* Magically adds validation methods. Normally the Laravel Validation methods
* only support single values to be validated like 'numeric', 'alpha', etc.
* Here we copy those methods to work also for arrays, so we can validate
* if a value is OR an array contains only 'numeric', 'alpha', etc. values.
*
* $rules = array(
* 'row_id' => 'required|integerOrArray', // "row_id" must be an integer OR an array containing only integer values
* 'type' => 'inOrArray:foo,bar' // "type" must be 'foo' or 'bar' OR an array containing nothing but those values
* );
*
* @param string $method Name of the validation to perform e.g. 'numeric', 'alpha', etc.
* @param array $parameters Contains the value to be validated, as well as additional validation information e.g. min:?, max:?, etc.
*/
public function __call($method, $parameters)
{
// Convert method name to its non-array counterpart (e.g. validateNumericArray converts to validateNumeric)
if (substr($method, -7) === 'OrArray')
$method = substr($method, 0, -7);
// Call original method when we are dealing with a single value only, instead of an array
if (! is_array($parameters[1]))
return call_user_func_array(array($this, $method), $parameters);
$success = true;
foreach ($parameters[1] as $value) {
$parameters[1] = $value;
$success &= call_user_func_array(array($this, $method), $parameters);
}
return $success;
}
/**
* All ...OrArray validation functions can use their non-array error message counterparts
*
* @param mixed $attribute The value under validation
* @param string $rule Validation rule
*/
protected function getMessage($attribute, $rule)
{
if (substr($rule, -7) === 'OrArray')
$rule = substr($rule, 0, -7);
return parent::getMessage($attribute, $rule);
}
}
它不在文档中,但 4.2 分支可能有一个简单的解决方案大约第 220 行。
就像
sometimes($attribute, $rules, callable $callback)
函数一样,现在有一个 each($attribute, $rules)
函数。
要使用它,代码将比
sometimes()
调用更简单:
$v->each('array_attribute',array('rule','anotherRule')); //$v is your validator
sometimes()
和each()
似乎不容易相互链接,因此如果您想对数组值执行特定的条件规则,那么现在最好使用其他答案中的神奇解决方案。each()
仅深入一层,这与其他解决方案没有什么不同。神奇解决方案的好处是,它们将根据需要通过调用适当的基本规则来深入 0 或 1 层,所以我想如果你想深入 1 到 2 层,你可以简单地通过调用 来合并这两种方法。 each()
并从其他答案中传递一条神奇的规则。each()
只接受一个属性,而不是像 sometimes()
那样是一组属性,但将此功能添加到 each()
不会对 each()
函数进行巨大更改 - 只需循环遍历 $attribute
和 array_merge()
$data
以及 array_get()
结果。如果有人认为它是理想的并且尚未完成,则可以将其作为 master 上的拉取请求,我们可以看看它是否会进入未来的构建。这是对 Ronald 代码的更新,因为我的自定义规则不适用于数组扩展。使用 Laravel 4.1 进行测试,默认规则,扩展规则,......
public function __call($method, $parameters) {
$isArrayRule = FALSE;
if(substr($method, -5) === 'Array') {
$method = substr($method, 0, -5);
$isArrayRule = TRUE;
}
//
$rule = snake_case(substr($method, 8));
// Default or custom rule
if(!$isArrayRule) {
// And we have a default value (not an array)
if(!is_array($parameters[1])) {
// Try getting the custom validation rule
if(isset($this->extensions[$rule])) {
return $this->callExtension($rule, $parameters);
}
// None found
throw new \BadMethodCallException("Method [$method] does not exist.");
} // Array given for default rule; cannot be!
else return FALSE;
}
// Array rules
$success = TRUE;
foreach($parameters[1] as $value) {
$parameters[1] = $value;
// Default rule exists, use it
if(is_callable("parent::$method")) {
$success &= call_user_func_array(array($this, $method), $parameters);
} else {
// Try a custom rule
if(isset($this->extensions[$rule])) {
$success &= $this->callExtension($rule, $parameters);
}
// No custom rule found
throw new \BadMethodCallException("Method [$method] does not exist.");
}
}
// Did any of them (array rules) fail?
return $success;
}
现在有数组验证规则,以防这对任何人有帮助。这些似乎还没有写在文档中。
https://github.com/laravel/laravel/commit/6a2ad475cfb21d12936cbbb544d8a136fc73be97