[英]how to add custom validation and message in laravel nova for one field
我想为 laravel nova 资源文件中的一个字段添加自定义验证。
我在 db 中有一个字段,它是service
表中的service_id
,并且我有同一张表的 nova 资源文件。 在创建服务时,我想使用其他表中的service_id
获取数据并检查数据是否存在,如果存在,那么我不想在表中输入该service_id
。
那么有人可以帮助我如何在 laravel nova 资源文件中编写自定义验证规则?
使用以下命令,您可以创建自定义规则类:
php artisan make:rule CustomRule
在您的资源中的 fields 函数中:
Text::make('Service ID', 'service_id')->rules(new CustomRule());
回到你的规则文件和 pass 函数内部:
public function passes($attribute, $value)
{
//You can check inside the database if your record exists
return false; //if the rule should stop the user
return true; //if everything is fine and you want the user to proceed.
}
public function message()
{
return 'Return your custom error message';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.