Ad

Laravel - Getting The Validation Error

- 1 answer

in Laravel, is there any way to know which rule was invalid. For example:

'email': 'email|max:20'

And let's assume that I want to know is the email max rule failed

Ad

Answer

If you want to get error message considering specific field, then mention the name of the Validation object key on messages array. Ref

If validation has failed, you may retrieve the error messages from the validator.

if ($validator->fails())
{
    $messages = $validator->messages();    
}

echo  $messages;

You may also access an array of the failed validation rules, without messages. To do so, use the failed method:

$failed = $validator->failed();

Retrieving All Error Messages For A Field

foreach ($messages->get('email') as $message)
{
    //
}
Ad
source: stackoverflow.com
Ad