Reputation: 392
I'm new to Laravel and trying to fix a email validation message. The scenario is:
If I send empty value, the validator returns a response "The User Email field is required".
If I send an invalid value like 'my_email_id' [ without @ sign ], it still returns "The User Email field is required".
If I send empty value like 'my_email_id@domain', it still returns "The User Email must be a valid email address.".
Now, my question is how can I return the response "The User Email must be a valid email address." for Case 2 as well? Is there any way or is it just how Laravel does it by default?
Thanks.
Upvotes: 0
Views: 6041
Reputation: 6337
Laravel has a built-in validation for email addresses, and it just so happens that the examples directly solve your specific needs too.
You will need to look into using the email validator and adding custom error messages, both are documented in detail in the linked documentation.
However, I've gone ahead and combined the two solutions directly from the linked resource to solve your needs:
$validator = Validator::make($request->all(), [
'email' => 'required|email|unique:users',
], [
'email.required' => 'The User Email must be a valid email address.'
)];
Upvotes: 0
Reputation: 5041
You should make it with your custom validation message like this :
$rules = array(
'email'=>'required|email|unique:users'
);
$messsages = array(
'email.required'=>'The User Email must be a valid email address'
);
$validator = Validator::make(Input::all(), $rules, $messsages);
Upvotes: 3