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.
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);
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.'
)];
Related
Hello Guys And Hi I Have This Validation Inside My Request
public function rules()
{
return [
'email' => 'required|email|unique:admins,email,' ,
'name' => 'required|unique:admins,name',
'code' => 'required|min:2|unique:admins,code',
'password' => 'required|min:8',
];
}
I want to ignore an email when updating data during the unique check. For example, consider an "update profile" screen that includes the user's name, e-mail address, I want to verify that the e-mail address is unique. However, if the user only changes the name field and not the e-mail field, I do not want a validation error to be thrown because the user is already the owner of the e-mail address. I only want to throw a validation error if the user provides an e-mail address that is already used by a different user.
I'm Fixing It I'm Passing The Id To The Request Like This
<input name='id' value="$item -> id" hidden >
and i write in Validation
'email' => 'required|email|unique:admins,email,' . $this -> id ,
I have a function that does some validation. Instead of $errors->get(key) returning the custom error messages I've defined, I'm getting the validation rule name. For example, if I use an email that's not unique:
$messages = [
'new_email.required' => 'Your new email address is required.',
'new_email:unique' => 'That email is already in use.',
'current_password|required' => 'Your current password must be provided.'
];
$rules = [
'new_email' => 'required|email|unique:users,email,' . $user->id,
'current_password' => 'required',
];
$validator = Validator::make($request->all(), $rules, $messages); // <-- custom error messages passed in here
if ($validator->fails()) {
$errors = $validator->errors();
if ($errors->has('new_email')) {
$msg = $errors->get('new_email'); // $msg contains ['validation.unique'] instead of ['That email is already in use.']
...
}
}
$errors->get('new_email') returns ['validation.unique'] instead of the custom error message in the array that's passed as the 3rd parameter to Validator::make. How can I get the custom error message instead of the validation rule that has been broken by the request?
There are some similar questions to this, but all the answers seem to focus on the resource/lang/xx/validation.php file missing or something like that. I'm not using those localization features at all.
Based on the documentation you should set your message with a string between property and validation rule.
$messages = [
'new_email.unique' => 'That email is already in use.',
];
In my Laravel project, I have a form that receives as input name, email etc. and submits the values to a store method in my controller. I created a request to validated the submitted input before storing them in the datebase. Here are the the validation rules and custom messages:
public function rules(){
return [
'name' => 'required|unique:users|min:8',
'email' => 'required|email',
];
}
public function messages(){
return [
'name.required' => 'You must enter name',
'name.unique' => 'Sorry, that name is already taken',
'email.required' => 'An email address is required',
];
}
Ajax and every other part of the code (like the ajax, and laravel) works just fine without the unique validation proberty. But when I include the unique validation, the form does not submit but instead when I look at the developer tools I get the following error:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
all_ajax.js:54 Uncaught TypeError: Cannot read property 'name' of undefined
Please what could I be doing wrong?
For the benefit of doubt let me just add the controller method
I believe the table users don't have a column name, you should specify the corresponding column of the table
I am running the following code (I've hidden ID's) to update a subscriber's email address in a MailChimp list:
$mailchimp->patch('lists/1234567/members/' . md5('test#test.com'), [
'email_address' => 'new-email#newtest.com',
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => 'Ben',
'LNAME' => 'Sinclair',
),
]);
It does not seem to work. I do not receive any errors, it just does nothing.
How do you update an email address in a MailChimp list using API V3?
http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#edit-patch_lists_list_id_members_subscriber_hash
Currently, the email address is a parameter (read only = false) in the PUT method (.../3.0/lists/{listId}/members/{md5}) that allows to change the email address of the subscriber.
I'm sending the new email in the body and MERGE0 (EMAIL) tag but using the md5 from the previous email. It is changing the email correctly.
You can change the email address when you make the request
PUT https://usx.api.mailchimp.com/3.0/lists/{list_id}/members/{subscriber_hash}
and the body was this:
{"email_address": "new#email.com"}
$List = 123456;
$subscriber_hash = md5("old#email.com")
$data = array('email_address' => "new#email.com" );
$result = $mailchimp->put("lists/$List/members/$subscriber_hash", $data);
I'm new in Laravel. I try to make profile update page... all works good but if I try to apply rule to set email field unique:users I have problem when user try to update for example name and don't want change email.
public function rules()
{
return [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
];
}
I want restrict that user to use the same e-mail that someone else is using... but I want to ignore that if this is the same e-mail already in that user profile and he don't want to change that.
public function updateData(UpdateDataRequest $request)
{
DB::table('users')
->where('id', Auth::user()->id)
->update(array('email' => $request->email, 'name' => $request->name));
return redirect('panel');
}
How to do it right?
This exact situation is used as an example in the docs.
https://laravel.com/docs/5.2/validation#rule-unique
Forcing A Unique Rule To Ignore A Given ID:
Sometimes, you may wish to ignore a given ID during the unique check. For example, consider an "update profile" screen that includes the user's name, e-mail address, and location. Of course, you will want to verify that the e-mail address is unique. However, if the user only changes the name field and not the e-mail field, you do not want a validation error to be thrown because the user is already the owner of the e-mail address. You only want to throw a validation error if the user provides an e-mail address that is already used by a different user. To tell the unique rule to ignore the user's ID, you may pass the ID as the third parameter:
'email' => 'unique:users,email_address,'.$user->id
If your table uses a primary key column name other than id, you may specify it as the fourth parameter:
'email' => 'unique:users,email_address,'.$user->id.',user_id'
In new version. laravel using Rules to ignore a user or record
https://laravel.com/docs/5.8/validation#rule-unique
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
$user->id can be a specific id or the id of current user which is login
try to this validation(Laravel 8.x)
'email' => ['email:rfc','confirmed','unique:App\Models\User,email'],