-1

Good day to all, I want to change default error message as "Title is required" to "Please enter title" The code I use: Controller

$this->validate($request, [
            'Title'=>'required',

        ]);

Also, how can I ensure that a user cannot save the same data into database, for example, if there is already a Title as Movie 43 we do not have to let user save that Title again in the database.

Mirasan
  • 259
  • 1
  • 4
  • 16

3 Answers3

2

The signature of the validate function is:

public function validate(Request $request, array $rules, array $messages = [], array $customAttributes = [])

You can pass in custom messages as the third parameter. The key of the custom message can be either field_name for all errors relating to that field, or you can be more specific and use field_name.rule. In this case you should use:

$this->validate(
    $request,
    ['Title' => 'required'],
    ['Title.required' => 'Please enter title']
);
Jonathon
  • 15,873
  • 11
  • 73
  • 92
  • Also, how can I ensure that a user cannot save the same data into database, for example, if there is already a Title as Movie 43 we do not have to let user save that Title again in the database. – Mirasan Sep 05 '18 at 11:38
  • 1
    ['Title' => 'required|unique'] – arun Sep 05 '18 at 11:39
  • @Jonathon, please dude can you very briefly explain why you used array $rules, array $messages = [], array $customAttributes = [] I hope for your answer:) – Mirasan Sep 05 '18 at 11:47
  • As @arun pointed out, refer to the [unique](https://laravel.com/docs/5.6/validation#rule-unique) rule to do that. – Jonathon Sep 05 '18 at 11:48
  • @Mirasan That is the actual signature of the method in Laravel. I've just included it to show you what the method expects. The second code block shows you how to use it. Take a look at the `ValidatesRequests` trait which should be included in your controller. – Jonathon Sep 05 '18 at 11:49
1
use Validator;

if you have much more validations this could be better

$validator = Validator::make($request->all(), $rules, $messages);
manu
  • 351
  • 2
  • 15
1

Try this

$rules = [
   'Title'=>'required|unique'
];
$messages = [
   'Title.required' => 'Please Enter Title',
   'Title.unique' => 'Please Enter Unique Title'
];

$validator = Validator::make(Input::all(), $rules, $messages);

And above declaration of controller class

use Validator;
use Illuminate\Support\Facades\Input;

Hope it helps you!

Leena Patel
  • 2,423
  • 1
  • 14
  • 28