gpt4 book ai didi

laravel - 验证laravel 4 错误中的数组表单字段

转载 作者:行者123 更新时间:2023-12-03 21:05:01 27 4
gpt4 key购买 nike

我们如何验证作为数组的表单字段?看看下面的代码

用户电话型号:

 public static $rules= array(
'phonenumber'=>'required|numeric',
'isPrimary'=>'in:0,1'
)
...........

用户 Controller :

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

自定义规则

此方法支持您开箱即用的任何自定义规则。

执行

1.扩展验证器类
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);
}
}

2. 注册您的验证器扩展

在您通常的 bootstrap 位置的任何位置,添加以下代码:
Validator::resolver(function($translator, $data, $rules, $messages)
{
return new ExtendedValidator($translator, $data, $rules, $messages);
});

就是这样!享受!

奖励:数组的大小规则

正如评论指出的那样,似乎没有简单的方法来验证数组大小。但是,Laravel 文档缺乏大小规则:它没有提到它可以计算数组元素。这意味着您实际上可以使用 size , min , maxbetween计算数组元素的规则。

关于laravel - 验证laravel 4 错误中的数组表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18161785/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com