I want to create a function and say in register form, user can put just Gmail or Ymail
And if the email isn't from them, shows: "Please put a valid email"
Register controller:
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'username' => 'required|string|alpha_dash|max:25|unique:users',
]);
}
To put some customized message, you need to paste third arguments to Validator::make
See this documentation link
And in the email validation rules, you need to add regex rule
like this
regex:/gmail|ymail]/
Regex Documentation
$message = ['email.regex' => 'Please put a valid email'];
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users|regex:/gmail|ymail]/',
'password' => 'required|string|min:6|confirmed',
'username' => 'required|string|alpha_dash|max:25|unique:users',
], $message);
Related
I want validate name and email (do required) only if user is guest. How I can do it correctly?
I tried paste the if condition. But I don't think this is correct:
if (Auth::check()) {
$request->validate([
'name' => 'required|string',
'email' => 'required|string',
'body' => 'required|string',
'type' => 'required|integer|in:1,2,3'
]);
} else {
$request->validate([
'body' => 'required|string',
'type' => 'required|integer|in:1,2,3'
]);
}
I can't think of a validation rule that'll do this, but you can at least get the code a little cleaner by skipping the else condition, for instance like so:
$rules = [
'body' => 'required|string',
'type' => 'required|integer|in:1,2,3',
];
if (Auth::guest()) {
$rules['name'] = 'required|string';
$rules['email'] = 'required|string';
}
$request->validate($rules);
Another option would be to simply check the requirement within the array and just skip the field in the validation for authorised users. It's shorter, but not quite as clean:
$request->validate([
'body' => 'required|string',
'type' => 'required|integer|in:1,2,3',
'name' => Auth::guest() ? 'required|string' : '',
'email' => Auth::guest() ? 'required|string' : '',
]);
By the way, I would recommend validating the email address as an email address: https://laravel.com/docs/5.7/validation#rule-email
I'm trying to update a user, as an admin.
I'm changing the username, but it says email must be unique.
How do I fix this.
public function update($id, PutUser $request)
{
if (auth()->id() == $id) {
return redirect()->back()->withFlashDanger('Permission Denied, You can not edit own profile here.');
}
$user = User::find($id);
$user->update((array_merge($request->validated(), ['county' => request('county')])));
//Update model_has_roles model with assignees_roles
return redirect()->route('users.index')->withFlashSuccess(trans("alerts.users.updated"));
}
This is the request class
public function authorize()
{
return true;
}
public function rules()
{
$user_id = $this->input('id');
return [
'name' => 'required|string',
'username' => 'required',
'email' => 'required|email|unique:users,email'.$user_id,
'gender' => 'required',
'phone' => 'sometimes|numeric',
'address' => 'sometimes|string',
'country_id' => 'required',
];
}
}
I keep getting a failed email validation. 'Email has already been taken'. Any idea
You are missing a comma after the email label in your validation:
return [
'name' => 'required|string',
'username' => 'required',
'email' => 'required|email|unique:users,email,'.$user_id,
'gender' => 'required',
'phone' => 'sometimes|numeric',
'address' => 'sometimes|string',
'country_id' => 'required',
];
Since Laravel 5.3 (I believe), you can also use rule builders for more descriptive validation rules. Those are better to read and interpret for humans so it would result in a lower error rate:
use Illuminate\Validation\Rule;
return [
'email' => [
'required',
Rule::unique('users', 'email')->except($user_id),
]
];
https://medium.com/#tomgrohl/why-you-should-be-using-rule-objects-in-laravel-5-5-c2505e729b40
My application uses the standard validator, and my form makes the user provide an email address. They may continue as a guest, but if they do want to create an account; the only thing they will have to provide is a password and in combination with that email address will create the user account.
However, my issue is I am not sure how to use the validator exists only if the password field has been filled in.
$this->validate($request, [
'first_name' => 'required',
'email' => 'required|confirmed|email',
'last_name' => 'required',
'street_1' => 'required',
'zip_code' => 'required',
'phone_1' => 'required',
'password' => 'required_if:account,1|confirmed',
]);
I could do a check and return redirect with an error message, but I'd prefer to go through the validator if I can.
The simplest solution is to put your validation rules into an array then perform your desired check. So if the user checked the "account creation" checkbox, add the rules.
$rules = [
'first_name' => 'required',
'last_name' => 'required',
'street_1' => 'required',
'zip_code' => 'required',
'phone_1' => 'required',
'password' => 'required_if:account,1|confirmed',
]
if ($request->input('acount') == 1) {
$rules['email'] = 'required|confirmed|email'
}
I am using Laravel 5.2,
I added a verification code input in register form,
verification code was saved in session when making it,
the question is:
How to write the validation rule of verification code in function validator,
AuthController.php
protected function validator(array $data)
{
$verification_code_session = $request->session()->get('verification_code', '');
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'verification_code' => '',// How to write the validation rule?
]);
}
Add:
add an argument:Request $request like this:
protected function validator(array $data,Request $request)
{
$verification_code_session = $request->session()->get('verification_code', '');
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'verification_code' => 'required|in:' . $verification_code_session
]);
error:
FatalThrowableError in AuthController.php line 56:
Type error: Argument 2 passed to App\Http\Controllers\Auth\AuthController::validator() must be an instance of Illuminate\Http\Request, none given, called in D:\wnmp\www\laravel-entrust\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 69
Where do I ought to use $this?
Maybe you could do that using the in pattern :
protected function validator(array $data)
{
$verification_code_session = \Request::session()->get('verification_code', '');
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'verification_code' => 'required|in:' . $verification_code_session
]);
}
I have a page setup to reset the user's email address password and the users zip_code for an application. I have it working correctly however, if for example I want to just change the zip code my validation will not allow it due to it reading the email as already existing in the database and returns an error. What is the best way around this? I could create a controller for each field and do it individually but I feel there is some if statement I could use maybe?
Controller:
enter
public function getEditUser() {
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password'),
'confirm_password' => Input::get('confirm_password'),
'user_zip_code' => Input::get('user_zip_code')
);
$rules = array(
'email' => 'required|email|unique:users,email',
'password' => 'required|min:5',
'confirm_password' => 'required|same:password',
'user_zip_code' => 'required'
);
$validation = Validator::make($userdata, $rules);
if ($validation->fails()) {
return Redirect::to('dashboard/edit/account')->withErrors($validation)
->withInput();
}
$userdata['password'] = Hash::make($userdata['password']);
$userdata['confirm_password'] = Hash::make($userdata['confirm_password']);
User::find(Auth::user()->id)->update($userdata);
return Redirect::to('dashboard');
}
Unique needs to be in place obviously so it doesn't get matched with another account
Also my email is set through Auth..Auth::user()->email if that helps
You can set the validation unique rule to ignore the current id
$rules = array(
'email' => 'required|email|unique:users,email,'.Auth::user()->id,
'password' => 'required|min:5',
'confirm_password' => 'required|same:password',
'user_zip_code' => 'required'
);
See here for more info from the docs