简体   繁体   English

如何在laravel nova中为一个字段添加自定义验证和消息

[英]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.

相关问题 如何将自定义中间件添加到 Laravel Nova - How to add custom middleware to Laravel Nova 以一种形式在 laravel nova 中添加来自关系的字段文本 - Add field text from relationship in laravel nova in ONE form 如何定义特定于字段的错误消息或覆盖Laravel 5.5中FormRequest中使用的自定义验证规则的默认错误消息 - How to define field specific error message or overwrite the default one for a custom validation rule used in a FormRequest in Laravel 5.5 Laravel Nova:如何创建自定义字段及其 crud 方法? - Laravel Nova: How to create a custom field and its crud methods? 如何在 Laravel 表单验证错误消息中给出自定义字段名称 - How to give custom field name in laravel form validation error message Laravel Nova如何在文本字段中添加实时计数器 - Laravel Nova how to add a live counter to text field 使用 Rule::in 作为验证时如何添加自定义 Laravel 错误消息 - How to add a custom Laravel error message when using Rule::in as validation Laravel:在一个字段中验证相同规则的消息 - Laravel: validation message for same rules in one field 添加自定义验证错误信息 laravel - Add a custom validation error message laravel 为 laravel 5.3 中的自定义验证添加自定义验证消息不起作用 - add custom validation message for custom validation in laravel 5.3 not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM