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
);
Related
When using the validation like below with custom messages how is possible to check if the validation fails?
$rules = [
'email' => 'required|email'
];
$customMessages = [
'email.required' => 'The email is required.',
'email.email' => 'Please insert a valid email.'
];
$this->validate($request, $rules, $customMessages);
Like below, without custom messages is possible to use $validator->fails() but in the above case how to verify if the validation fails?
$validator = Validator::make($request->all(), [
//...
]);
if ($validator->fails()) {
//...
}
When you use $this->validate it validate your request against your validation rules and if failed then it redirect to previous page with validation message. If you want to use $validator->fails() then use Validator::make with custom message. Like this
$rules = [
'email' => 'required|email'
];
$customMessages = [
'email.required' => 'The email is required.',
'email.email' => 'Please insert a valid email.'
];
$validator = Validator::make($request->all(), $rules, $customMessages);
if ($validator->fails()) {
//...
}
Check validator make method https://laravel.com/api/5.0/Illuminate/Validation/Factory.html#method_make
much simpler solution is to use custom handler in app\Exceptions\Handler.php:
public function register()
{
$this->renderable(function (ValidationException $e, $request) {
//list of specific failed tests
$e->validator->failed();
return response()->json(['message' => 'custom message','meow'=>'meow',$e->status);
});
}
this way you keep old behavior AND can still change messages sent back to the user/frontend
need help to validate my form input
i need user only able to upload file with extension .log
so far i've tried as the code shown below but the validator still reject the input even the file is .log
thanks in advance
$validator = Validator::make($request->all(),
[
'email' => 'required|email',
'file_log' => 'mimes:text/plain,log'
],
[
'email.email' => 'Please input valid email address',
'file_log.mimes' => 'Mohon hanya mengunggah file yang berekstensi .log ',
]
);
Please check this :
'file_log' => 'mimes:text/x-log '
another way
$validator = Validator::make(
[
'file' => $request->file,
'extension' => strtolower($request->file>getClientOriginalExtension()),
],
[
'file' => 'required',
'extension' =>'required|in:log',
]
);
ok finally i find an answer
thanks for your answer
// Create custom validation rule
Validator::extend('logfile', function ($attribute, $value, $parameters, $validator) {
$regexPattern = '/\.log$/';
$filename = $value->getClientOriginalName();
return preg_match($regexPattern, $filename);
});
$validator = Validator::make($request->all(),
[
'email' => 'required|email',
'file_log' => 'logfile',
],
[
'email.email' => 'Please input valid email address',
'file_log.logfile' => 'Please upload only file .log',
]
);
$id = $request->id;
$validation = Validator::make($request->all(), [
'email' => 'unique:customers,email,'.$request->id
]);
You're using a custom validator. You need to handle the validation failure manually. Also your code checks for unique email in the table customers except for the email of the user $request->id. I assume this is intended.
$validator = \Validator::make($request->all(), [
'email' => 'email|unique:customers,email,' . $request->id
]);
if ($validator->fails()) {
// Handle failure
}
The code below will automatically handle validation failure and redirect back with errors and inputs.
$this->validate($request, [
'email' => 'email|unique:customers,email,' . $request->id
]);
You can try something like
Validator::make($data, [
'email' => [
'required',
Rule::unique('customers')->ignore($customer->id),
],
]);
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!
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);