PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 18, 2022

[FIXED] how to add custom validation and message in laravel nova for one field

 March 18, 2022     eloquent, laravel, laravel-5, laravel-5.2, laravel-nova     No comments   

Issue

I want to add custom validation for one field in laravel nova resource file.

I have one field in db which is service_id in the service table and I have nova resource file for the same table. 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.

So can someone help me how can I write custom validation rules in laravel nova resource file?


Solution

Using the following command, you can create a custom rule class:

php artisan make:rule CustomRule

Inside the fields function in your resource:

Text::make('Service ID', 'service_id')->rules(new CustomRule());

Go back to your rule file and inside the passes function:

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';
}


Answered By - Hadi Najem
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing