[英]how to add custom validation and message in laravel nova for one field
I want to add custom validation for one field in laravel nova resource file.我想为 laravel nova 资源文件中的一个字段添加自定义验证。
I have one field in db which is service_id
in the service
table and I have nova resource file for the same table.我在 db 中有一个字段,它是service
表中的service_id
,并且我有同一张表的 nova 资源文件。 On create service I want to get data using service_id
from other table and check data is present or not and if present then I don't want to enter that service_id
in table.在创建服务时,我想使用其他表中的service_id
获取数据并检查数据是否存在,如果存在,那么我不想在表中输入该service_id
。
So can someone help me how can I write custom validation rules in laravel nova resource file?那么有人可以帮助我如何在 laravel nova 资源文件中编写自定义验证规则?
Using the following command, you can create a custom rule class:使用以下命令,您可以创建自定义规则类:
php artisan make:rule CustomRule
Inside the fields function in your resource:在您的资源中的 fields 函数中:
Text::make('Service ID', 'service_id')->rules(new CustomRule());
Go back to your rule file and inside the passes function:回到你的规则文件和 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.