【发布时间】:2019-07-08 20:22:18
【问题描述】:
我正在尝试使用规则实现验证以验证模型中的字段;如官方文档所示,这样:
1) 在文件夹 App/Rules 我把文件 Um.php:
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use App\Models\Common\Item;
class Um implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if(strlen($attribute) < 5)
return false;
return true;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The field is too short ';
}
}
2) 在我的控制器类中,在方法更新中:
use App\Rules\Um as RuleUm;
...
public function update(Request $request $item)
{
//$item is the model don't worry for this
//Here is where I invoke the rule
$request->validate([
'codum' => [ new RuleUm],
]);
$item->update($request->input());
//...son on
}
到目前为止一切顺利,更新数据后出现问题; pass() 方法被完全忽略;并恰好执行更新。这不依赖于方法的逻辑,因为无论如何它仍然返回 false,就像 Laravel 仍然忽略该方法一样,它没有被执行。
有人可以帮助我吗? 我做错了什么?
【问题讨论】:
-
您正在检查
codum的属性长度,它是 5 个字符,而不是$value。那么您是在验证自己还是验证传入的数据?
标签: laravel validation rules