"These credentials do not match our records." - php

I am getting this error while I try to login. user registers successfully, but when trying to login, I get the error "These credentials do not match our records."
This is my login function in UserController:
function login(Request $req)
{
$validatedData = $req->validate([
'name' => 'required|max:255',
'email' => 'required|email',
'password' => 'required',
], [
'name.required' => 'The name field is required.',
'email.required' => 'The email field is required.',
'email.email' => 'The email must be a valid email
address.',
'password.required' => 'The password field is
required.',
'password.min' => 'The password must be at least 8
characters.',
]);
$credentials = $req->only('email','password');
if (Auth::attempt($credentials)) {
return redirect()->intended('/dashboard')
->with('success', "Loggedin successfuly");
}
return redirect()->back()->withErrors(['email' =>
'These credentials do not match our records.']);
}

Related

laravel change validation default message from front side

Laravel change validation default message
change validation message from front side
read validation.php file and than write that file from front side
is it possible ?
Use Validator class
use Illuminate\Support\Facades\Validator;
$rules = [
'name' => 'required',
'email' => 'required|email,
];
$messages = [
'name.required' => 'The :attribute is required',
'email.required' => 'The :attribute is required',
'email.email' => 'The :attribute is not valid',
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return Redirect::back()->withErrors($validator)->withInput();
} else {
`enter code here`
}

Is there a way to validate phone number globally inLaravel?

I have validated UAE-based phone numbers in Laravel. Is there any way to validate all countries numbers (not only UAE-based)?
$messages = [
'required' => 'The :attribute field is required.',
'integer' => 'The :attribute field value should be an integer.',
'regex' => 'The :attribute is invalid',
'digits' => 'The :attribute is invalid',
];
$validator = Validator::make($request->all(),
[
'number' => 'required|digits:12|regex:/(9715)[0-9]{8}/',
'service' => 'required|integer'
],
$messages
);

Getting the index of a list and display in error message during validation in Laravel

I have a validation rule as follows. I'm using the validate method via the ValidateRequests trait.
$this->validate($request, [
'entries' => 'required|max:5',
'entries.*.name' => 'required',
'entries.*.email' => 'required|email',
'entries.*.mobile_number' => 'required'
]);
And these are some sample error messages that I've encountered.
[
'entries.0.name' => ['The entries.0.name is required.'],
'entries.1.email' => ['The entries.1.email must be a valid email address.']
]
Is there a way to modify the message to these by using only the validation.php in modifying such messages?
[
'entries.0.name' => ['Line 0 - The name is required.'],
'entries.1.email' => ['Line 1 - The email must be a valid email address.']
]
If you want to customize the error message then you can do it as:
$validator = Validator::make($request->all(), [
'entries' => 'required|max:5',
'entries.*.name' => 'required',
'entries.*.email' => 'required|email',
'entries.*.mobile_number' => 'required'
]);
$validator->setAttributeNames([
'entries.*.name' => 'name',
'entries.*.email' => 'email',
'entries.*.mobile_number' => 'mobile number'
]);
$errors = $validation->errors()->all();
foreach ($errors as $key => $error) {
$errors[$key] = "Line {$key} - $error";
}
// dd($errors);
if($validation->fails()) {
return redirect()->back()->withErrors($errors());
}

Custom validation errors in laravel

In updating profile i use a validator class method:
class UpdateRequest extends Request {
public function authorize() { return true; }
public function rules() {
return [
'name' => 'required',
'email' => 'required|email',
];
}
}
How to add an additional validation error like:
public function postUpdate(UpdateRequest $request)
if($user->email == $request->get('email')) {
$request->addEerror("The email has already been taken."); //shows an fatal error
}
}
?
Thank you
You have not mentioned which Laravel version you are using, assuming 5.1
You can create a message array for different validation type, like the example below:
$rules = [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email|unique:users,email,'.$user->id
];
In your resources/lang/en/validation.php file
$custom_messages = [
'required' => 'The :attribute field is required.',
'email' => [
'required' => 'The email id field is required.',
'email' => 'Please enter a valid email format.',
'unique' => 'The email id has already been taken.',
]
];
This should do the trick.

Laravel Validation Login form validation fails in Password confimation?

I have two forms, login and signup forms, and one User model, i have set-up my rules and messages in User class, which are static members, $rules and $messages, the problem i face now, is in sign up form everything validate the it should be, but in login form, after i put a correct email and password, it gives me "Password confirmation doesn't match" which it shouldn't because there is no password_confrimation field in the login form.
Rules and Messages in UserModel
public static $rules = [
'email' => 'required|email',
'password' => 'sometimes|required|confirmed',
'password_confirmation' => 'sometimes|required'
];
public static $messages = [
'email.required' => 'The email is required',
'email.email' => 'the email attribute is not in a email format',
'password.required' => 'the password is required'
];
Login action
$validateUser = Validator::make($inputs, User::$rules, User::$messages);
$user = new User();
$user->email = $inputs['email'];
$user->password = $inputs['password'];
I did like this in my project
public static $rules = array(
'email' => 'required|email|unique:users,email',
'password' => 'required|min:6',
'repassword' => 'same:password',
'group' => 'exists:groups,id',
'last_name' => 'required',
'first_name' => 'required',
);
Give it a try .

Categories