How can I change the value used in the error feedback, lets say I have this rule:
$rules = array(
'valid_country_code' => 'required',
);
But instead of 'valid_country_code' I want the user to see 'country' in the error message. The message at the moment.
valid_country_code is required.
what I want
country is required.
But I dont want to change the name in the form when posting because I want to bind the form to a model.
You may pass the custom messages as the third argument to the Validator::make method:
$rules = array(
'valid_country_code' => 'required',
);
$messages = [
'valid_country_code.required' => 'country is required.',
];
$validator = Validator::make($input, $rules, $messages);
Related
I have a laravel validation rules array that I have defined like this.
$rules = array(
'name' => 'required|regex:/(^[A-Za-z0-9 ]+$)+/',
'email' => 'required|email',
'mobile' => 'regex:/^\+?\d+$/',
);
$validation = Validator::make($input, $rules);
now depending on another if condition I want to add another validation rule into this array.
address => 'required'
how do I do it? I have tried the using array_push() function with the $rules array, but it doesn't work.
$rules['address'] = 'required';
For example I have validation of HTML form:
$validator = Validator::make($request->all(), [
"code" => "required|string|size:10"]);
If I get error it look as:
The code field is required.
Wrere code is name of field.
How can I translate(localize) this?
You can pass 3rd argument in make method like this:
$rules = [
"code" => "required|string|size:10",
];
$messages = [
'required' => 'The :attribute field is required.',
];
$validator = Validator::make($input, $rules, $messages);
See more about Custom Validation Error Messages
Hop this helps!
I am using following code in controller file. I have an element name title in view form. Following code is showing correct validation. But, i need to customize this error.
$v = Validator::make($request->all(), [
'title' => 'required|max:255',
]);
if ($v->fails())
{
return redirect()->back()->withErrors($v->errors());
}
You can pass as third argument of Validator your custom messages.
$messages = [
'title.required' => 'You need to insert a pretty title'
];
v = Validator::make($request->all(), [
'title' => 'required|max:255',
], $messages
);
This question already has answers here:
Laravel 4 validation email unique constraint
(2 answers)
Closed 4 years ago.
I have a view that shows a form with pre-populated data related to a user model. This is for updating the model data. When the form is submitted, however, there's a conflict because the email address is not unique (if it hasn't been changed). Yet I still want to be able to store this (or ignore it).
I'm trying to update a model with this controller code:
$input = Input::all();
$validator = Validator::make($input, User::$rules['edit']);
if ($validator->fails()) {
return Response::json(array(
'error' => $validator->messages()
));
}
In the model, I've got:
public static $rules = array(
'create' => array(
'email' => 'required|email|unique:users',
'password' => 'required|confirmed',
'firstname' => 'required',
'lastname' => 'required',
'address_one'=> 'required',
'postcode'=> 'required'
),
'edit' => array(
'email' => 'sometimes|required|email|unique:users',
'password' => 'sometimes|required|confirmed'
),
);
But when I update, I get the error message:
"error": {
"email": [
"The email has already been taken."
]
}
I thought that the sometimes would stop this behaviour. What am I doing wrong?
You need to specify the user ID so the validator knows it needs to ignore the entry with that ID when checking the entries for uniqueness:
'email' => 'sometimes|required|email|unique:users,' . $id
Taken from the Laravel Docs:
Forcing A Unique Rule To Ignore A Given ID
'email' => 'unique:users,email_address,10'
In your case, since you're keeping the rules in a model property, you'll need to append the ID before passing the rules to the validator. Something like this should do:
$input = Input::all();
$rules = User::$rules['edit'];
// this assumes the user your want to update
// is stored in the $user variable
$rules['email'] .= ',' . $user->id;
$validator = Validator::make($input, $rules);
if ($validator->fails()) {
return Response::json(array(
'error' => $validator->messages()
));
}
I used Laravel Form validation for validate form:
$rules = array(
'user_name' => 'required|min:3|max:20|unique:users',
'email' => 'required|email|max:50|unique:users',
'mobile' => 'required'
);
this is working well on some thing add. but when i going to edit unique:users part given error "already been taken", so how to write validation to check exclude edit row.
If I wrote this way, does it work?
$rules = array(
'user_name' => 'required|min:3|max:20|unique:users,'.$id,
'email' => 'required|email|max:50|unique:users,'.$id,
'mobile' => 'required'
);
#user3099298 Your second set of code looks alright.
$rules = array(
'user_name' => 'required|min:3|max:20|unique:users,user_name,' . $user_id, // $user_id = Currently being edited user id.
'email' => 'unique:users,email_address,' . $user_id, // $user_id = Currently being edited user id.
'mobile' => 'required',
);
I have added column names in rules - may be you can try that one.
Please check here the documentation
Where they explain how to ignore some rules by providing ID.