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.']);
}
This question already asked but that is not solving my issue.
I have try to use validation in my laravel project. But it not working this is my code
$rules = array(
'coupon_title' => 'max:10',
'coupon_type_id' => 'numaric',
'expiry_start_date' => 'required|before_or_equal:expiry_end_date',
'expiry_end_date' => 'required',
);
$messages = [
'before_or_equal' => 'The :attribute must be smaller than the End Date',
'before' => 'The :attribute must be Greater than the Interview Start Date',
'after' => 'The :attribute must be Greater than the Interview End Date',
'after_or_equal' => 'The :attribute must be Greater than the Start Time',
//'max' => 'asdasd'
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails())
{
$messages = $validator->messages();
return response()->json(['message'=>$messages],401);
}
In my code only expiry_start_date field validated. But coupon_title and coupon_type_id fields not validated.
You need to correct spell numaric to numeric and add nullable keyword in validation rule
'coupon_title' => 'string|max:10|nullable',
'coupon_type_id' => 'numeric',
Also you can add custom message for numeric and max
$messages = [
'before_or_equal' => 'The :attribute must be smaller than the End Date',
'before' => 'The :attribute must be Greater than the Interview Start Date',
'after' => 'The :attribute must be Greater than the Interview End Date',
'after_or_equal' => 'The :attribute must be Greater than the Start Time',
'max' => 'The :attribute has crossed the limit',
'numeric' => 'The :attribute must have numeric value'
];
I'm trying to use the Laravel Validator class for a PHP project that is not made in laravel. Essentially I'm doing something similar to Illuminate Validator in stand-alone non-Laravel application
Here's my code:
$filesystem = new \Illuminate\Filesystem\Filesystem();
$fileLoader = new \Illuminate\Translation\FileLoader($filesystem, '');
$translator = new \Illuminate\Translation\Translator($fileLoader, 'en_US');
$messages = [
'required' => 'The :attribute field is required.',
'string' => 'The :attribute field must be a string.',
];
$factory = new \Illuminate\Validation\Factory($translator);
$rules = [
'title' => 'required'
];
$validator = $factory->make($this->request->post(), $rules, $messages);
$validator = new \Illuminate\Validation\Validator($translator, $this->request->post(), $rules);
if ($validator->fails()) {
$errors = $validator->errors();
foreach ($errors->all() as $message) {
var_dump($message);
}
}die;
My problem here is that I need to create all the error validation messages myself. I was wondering if there is a way use the default Laravel error messages. I'm even considering setting all those default error messages as one complete array in a seperate file, if I could get those. Any ideas where I can find all the default validation error messages for Laravel?
This is the list of all default error messages
https://github.com/laravel/laravel/blob/master/resources/lang/en/validation.php
[
'accepted' => 'The :attribute must be accepted.',
'active_url' => 'The :attribute is not a valid URL.',
'after' => 'The :attribute must be a date after :date.',
'after_or_equal' => 'The :attribute must be a date after or equal to :date.',
'alpha' => 'The :attribute may only contain letters.',
'alpha_dash' => 'The :attribute may only contain letters, numbers, dashes and underscores.',
'alpha_num' => 'The :attribute may only contain letters and numbers.',
'array' => 'The :attribute must be an array.',
'before' => 'The :attribute must be a date before :date.',
'before_or_equal' => 'The :attribute must be a date before or equal to :date.',
'between' => [
'numeric' => 'The :attribute must be between :min and :max.',
'file' => 'The :attribute must be between :min and :max kilobytes.',
'string' => 'The :attribute must be between :min and :max characters.',
'array' => 'The :attribute must have between :min and :max items.',
],
'boolean' => 'The :attribute field must be true or false.',
'confirmed' => 'The :attribute confirmation does not match.',
'date' => 'The :attribute is not a valid date.',
'date_format' => 'The :attribute does not match the format :format.',
'different' => 'The :attribute and :other must be different.',
'digits' => 'The :attribute must be :digits digits.',
'digits_between' => 'The :attribute must be between :min and :max digits.',
'dimensions' => 'The :attribute has invalid image dimensions.',
'distinct' => 'The :attribute field has a duplicate value.',
'email' => 'The :attribute must be a valid email address.',
'exists' => 'The selected :attribute is invalid.',
'file' => 'The :attribute must be a file.',
'filled' => 'The :attribute field must have a value.',
'gt' => [
'numeric' => 'The :attribute must be greater than :value.',
'file' => 'The :attribute must be greater than :value kilobytes.',
'string' => 'The :attribute must be greater than :value characters.',
'array' => 'The :attribute must have more than :value items.',
],
'gte' => [
'numeric' => 'The :attribute must be greater than or equal :value.',
'file' => 'The :attribute must be greater than or equal :value kilobytes.',
'string' => 'The :attribute must be greater than or equal :value characters.',
'array' => 'The :attribute must have :value items or more.',
],
'image' => 'The :attribute must be an image.',
'in' => 'The selected :attribute is invalid.',
'in_array' => 'The :attribute field does not exist in :other.',
'integer' => 'The :attribute must be an integer.',
'ip' => 'The :attribute must be a valid IP address.',
'ipv4' => 'The :attribute must be a valid IPv4 address.',
'ipv6' => 'The :attribute must be a valid IPv6 address.',
'json' => 'The :attribute must be a valid JSON string.',
'lt' => [
'numeric' => 'The :attribute must be less than :value.',
'file' => 'The :attribute must be less than :value kilobytes.',
'string' => 'The :attribute must be less than :value characters.',
'array' => 'The :attribute must have less than :value items.',
],
'lte' => [
'numeric' => 'The :attribute must be less than or equal :value.',
'file' => 'The :attribute must be less than or equal :value kilobytes.',
'string' => 'The :attribute must be less than or equal :value characters.',
'array' => 'The :attribute must not have more than :value items.',
],
'max' => [
'numeric' => 'The :attribute may not be greater than :max.',
'file' => 'The :attribute may not be greater than :max kilobytes.',
'string' => 'The :attribute may not be greater than :max characters.',
'array' => 'The :attribute may not have more than :max items.',
],
'mimes' => 'The :attribute must be a file of type: :values.',
'mimetypes' => 'The :attribute must be a file of type: :values.',
'min' => [
'numeric' => 'The :attribute must be at least :min.',
'file' => 'The :attribute must be at least :min kilobytes.',
'string' => 'The :attribute must be at least :min characters.',
'array' => 'The :attribute must have at least :min items.',
],
'not_in' => 'The selected :attribute is invalid.',
'not_regex' => 'The :attribute format is invalid.',
'numeric' => 'The :attribute must be a number.',
'present' => 'The :attribute field must be present.',
'regex' => 'The :attribute format is invalid.',
'required' => 'The :attribute field is required.',
'required_if' => 'The :attribute field is required when :other is :value.',
'required_unless' => 'The :attribute field is required unless :other is in :values.',
'required_with' => 'The :attribute field is required when :values is present.',
'required_with_all' => 'The :attribute field is required when :values is present.',
'required_without' => 'The :attribute field is required when :values is not present.',
'required_without_all' => 'The :attribute field is required when none of :values are present.',
'same' => 'The :attribute and :other must match.',
'size' => [
'numeric' => 'The :attribute must be :size.',
'file' => 'The :attribute must be :size kilobytes.',
'string' => 'The :attribute must be :size characters.',
'array' => 'The :attribute must contain :size items.',
],
'string' => 'The :attribute must be a string.',
'timezone' => 'The :attribute must be a valid zone.',
'unique' => 'The :attribute has already been taken.',
'uploaded' => 'The :attribute failed to upload.',
'url' => 'The :attribute format is invalid.'
]
In my application, custom validation for address field not working, but other fileds are working. No idea regarding the issue.
View
<div class="col-md-4">
<label for="email">Location:</label>
<div class="form-group">
<input name="address" id="autocomplete" placeholder="Enter your address"
onFocus="geolocate()" class="form-control" value="{!!$company->address!!}" type="text"></input>
</div>
</div>
Controller
$this->validate($request, [
'name' => 'required|regex:/^[a-zA-Z .0-9]+$/',
'office_mail' => 'required|email|max:255',
'address' => 'required|regex:/^[a-zA-Z ,0-9]+$/',
'industry_id' => 'required',
'contact_number' => 'numeric|digits_between:10,12',
'company_website_url' => 'required|url',
]);
Validation.php
'custom' => [
'name' => [
'required' => 'Your name is required',
],
'address' => [
'required' => 'The location field is required',
],
'address' => [
'regex' => 'Please enter a valid location',
],
'company_id' => [
'required' => 'The company field is required',
],
]
The custom validation for name is working, I get "Your name is required" validation message.But for address field I'am getting 'The address field is required' instead of 'The location field is required'
'custom' => [
'name' => [
'required' => 'Your name is required',
],
'address' => [
'required' => 'The location field is required',
'regex' => 'Please enter a valid location',
],
'company_id' => [
'required' => 'The company field is required',
]
]
Because your define address field 2 times in array so 1st one override
by 2nd .
In 2nd array no message for required filed that is the reason its displaying default message
'address' => [
'regex' => 'Please enter a valid location', // No message for required
],
Need change it to :
'address' => [
'required' => 'The location field is required',
'regex' => 'Please enter a valid location',
],
Am using the Laravel Validator class to do some basic validation on an array.
My array :
$employee['name']='name';
$employee['address']='address';
$employee['department']['department_name']='deptname';
$employee['department']['department_address']='deptaddress';
I have the validation rules as below:
$rules = array(
'name'=> 'required',
'address' => 'required',
'department.department_name' => 'sometimes|required'
)
And the custom messages as below :
$messages = array(
'name.required' => 'Employee Name is required',
'address.required' => 'Address is required'
'department.department_name.required' => 'Department name is required'
)
I will use Validator::make($employee, $rules, $messages);
As per my rules, department_name should be validated if and only if it is present in the array. But currently the Validator is not validating department_name when its present and blank. Any ideas what I might be doing wrong?
You are going a bit wrong here see the docs here
it is mentioned there If I have $photos['profile'] then my validation will go like this
$validator = Validator::make($request->all(), [
'photos.profile' => 'required|image',
]);
From the above example, In your case it should be like this
$rules = array(
'name'=> 'required',
'address' => 'required',
'employee.department.department_name' => 'sometimes|required'
)
Since you have array like this $employee['department']['department_name']
So does the $message will go like this
$messages = array(
'name.required' => 'Employee Name is required',
'address.required' => 'Address is required'
'employee.department.department_name.required' => 'Department name is required'
)