laravel change validation default message from front side - php

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`
}

Related

Laravel : Validation messages for custom rule

I am trying to add a custom validation message using the below code,
$validator = Validator::make(
$user,
[
'first_name' => 'required|min:2',
'email' => [
'required',
'email',
Rule::notIn(array_column(Customer::getEmails(), 'email'))
]
],
['email.required' => 'Email is required (Custom message)']);
I have added a custom message for email required validation. No issues there.
For Rule::notIn validation, currently it is returning The email is invalid. How can I add a custom message in this case?
Unable to find anything related in Laravel docs about this.
Try this:
$validator = Validator::make(
$user,
[
'first_name' => 'required|min:2',
'email' => [
'required',
'email',
Rule::notIn(array_column(Customer::getEmails(), 'email'))
]
],
[
'email.required' => 'Email is required (Custom message)',
'email.email' => 'Your custom message here',
'email.not_in' => 'Your custom message here',
]
);

return Validator response in custom format in laravel?

I am using validators as
$validator = Validator::make($request->all(), [
'name' => 'required|max:255',
'phone' => 'required|max:10|unique:users',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6',
]);
if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()], 401);
}
this gives response in format below:
{
"error": {
"phone": [
"The phone has already been taken."
],
"email": [
"The email has already been taken."
]
}
}
I want response as
{
"error": 1,
"error_msg": "The phone has already been taken."
}
Just one error should show and that too in above format.
For validation to stop after first validation failure, laravel use bail check this. So the rules will be like this
$validator = Validator::make($request->all(), [
'name' => 'bail|required|max:255',
'phone' => 'bail|required|max:10|unique:users',
'email' => 'bail|required|email|max:255|unique:users',
'password' => 'required|min:6',
]);
For formatting error message, you need to do some change in response
if ($validator->fails()) {
return response()->json(['error'=> 1, 'error_msg' => $validator->errors()->first()], 400);
}
You can create a central place to render the validation error so that it will be consistent validation response format through out the application.
Hope this may help you

Laravel 5.4 sometimes|required validation not raising on "null" input

I'm having a problem validating inputs that are only going to be present sometimes in the Request.
// Controller
public function update(Request $request, User $user)
{
$updateResult = $user->updateUser($request);
return dd($updateResult);
}
// User Model
protected $validation = [
'rules' => [
'email' => [
'sometimes',
'email',
'required',
],
'password' => [
'sometimes',
'min:6',
'required',
],
'first_name' => [
'sometimes',
'required',
],
'last_name' => [
'sometimes',
'required',
],
],
'messages' => [
'email.required' => 'An email is required.',
'email.email' => 'The email must be valid.',
'password.required' => 'A password is required.',
'password.min' => 'Your password must be at least six (6) characters long.',
'first_name.required' => 'Your first name is required.',
'last_name.required' => 'Your last name is required.',
],
];
public function updateUser(Request $request)
{
$validation = Validator::make($request->all(), [
$this->validation['rules'],
$this->validation['messages'],
]);
if ($validation->fails())
{
return $validation;
}
else
{
return "OK";
}
}
So in some update pages $request->all() is only going to have a subset of these fields. However, even a field is present, but the value is null, the required doesn't trigger.
[
'first_name' => null,
'last_name' => 'Davidson',
'job_title' => 'Tech Support',
]
The above request array will return "OK"... If I remove sometimes from the fields, then when a partial input request is sent, it fails saying the fields are required.
I am clearing missing something here, but from reading the docs I thought I'd configured this correctly:
In some situations, you may wish to run validation checks against a
field only if that field is present in the input array. To quickly
accomplish this, add the sometimes rule to your rule list:
$v = Validator::make($data, [
'email' => 'sometimes|required|email', ]);
The problem you are facing is simply due to an error in your call to the validator. The second parameter is not a multidimensional array as you passed. The rules array and the messages array are separate parameters.
$validation = Validator::make($request->all(), [
$this->validation['rules'],
$this->validation['messages'],
]);
Should be replaced by
$validation = Validator::make($request->all(),
$this->validation['rules'], $this->validation['messages']);
In Laravel 5.4 empty strings are converted to Null by the ConvertEmptyStringsToNull middleware... that might cause you some issues...
You should add nullable to all your optional validations...
Hope this helps
'first_name' => [
'sometimes',
'required',
],
Will never work as expected. Sometimes indicates: if something comes, what is the next rule? In this case 'required'. Required what? Change this to:
'first_name' => [
'sometimes',
'required',
'min:1',
],
The null value will still be a null value if no input is given and won't fail. If you want to keep the value of the field in the table for updates, populate the input in the form with it's respected values.
The null value was send as '' and got nulled by the ConvertEmptyStringsToNull::class in the app\Http\kernel.php middleware.

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.

Categories