0

Laravel 5 has the option to deal with validation in the controller and provide a custom message to a validation rule for a specific field, such as:

$this->validate($request,
    ['title' => 'required'],
    ['title.required' => 'The Title field is required.' ]
]);

However, since I can comfortably deal with custom names to validation rules from the lang/en/validation.php file, I was wondering if is there a way to specify a custom name for the attribute from the validation in controller, such as:

$this->validate($request,
    ['title' => 'required'],
    ['title' => 'Title' ]
]);

This example of course will not work.

1
  • No, You can completely provide a different message. Commented Jul 4, 2015 at 0:36

1 Answer 1

1

If you're on Laravel 5.0, you won't be able to do this. However, if you've moved to Laravel 5.1, you're in luck. 5.1 added the custom attributes as the fourth parameter.

So, if you're on 5.1, you can do this:

$this->validate(
    $request,
    ['title' => 'required'],
    [],
    ['title' => 'Title' ]
]);
Sign up to request clarification or add additional context in comments.

1 Comment

I've tried this only in 5.0, thank you for pointing this out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.