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`
}
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
);
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.'
]
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'
)
Pretty basic question, I'm trying to customise the error message for a regex validation rule in Laravel. The particular rule is for passwords and requires the password to have 6-20 characters, at least one number and an uppercase and lowercase letter so I'd like to communicate this to the user rather than just the default message which says the format is "invalid".
So I tried to add the message into the lang file in a few different ways:
1)
'custom' => array(
'password.regex:' => 'Password must contain at least one number and both uppercase and lowercase letters.'
)
2)
'custom' => array(
'password.regex' => 'Password must contain at least one number and both uppercase and lowercase letters.'
)
3)
'custom' => array(
'password.regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})' => 'Password must contain at least one number and both uppercase and lowercase letters.'
)
None of these have worked. Is there a way to do this?
I was able to solve this by using this method instead:
'custom' => array(
'password' => array(
'regex' => 'Password must contain at least one number and both uppercase and lowercase letters.'
)
)
but I'd love to know why one of the other methods didn't work if anyone happens to know...?
Well seems like laravel 7 solves this:
$messages = [
'email.required' => 'We need to know your e-mail address!',
'password.required' => 'How will you log in?',
'password.confirmed' => 'Passwords must match...',
'password.regex' => 'Regex!'
];
$rules = [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => [
'required',
'string',
'min:7',
'confirmed',
'regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\X])(?=.*[!$#%]).*$/'
]
];
return Validator::make($data, $rules, $messages );