Laravel before/after validation message - validation

In Laravel you can make a custom messages for validators. But I found that the prepared messages are little bit wrong. With before and after validation rules, the parameter is converted with strtotime like that said in the documentation.
So if I set rule 'expires' => 'before:+1 year' the rule is working and correct. But if the user inputs a wrong value, Laravel prints the message like that:
The expires must be a date before +1 year
This message is very unclear for the average client. I was expected that the message will be converted with the strtotime also.
There is a clean way to print more clear error message?

You can override the validation messages with a custom ones if you want to.
In the Form Request class, add the following method and change the messages like so:
public function messages()
{
return [
// 'fieldname.rulename' => 'Custom message goes here.'
'email.required' => 'Er, you forgot your email address!',
'email.unique' => 'Email already taken m8',
];
}
Update:
If you want to change the global messages, you can open the validation.php file inside resources/lang/en/validation.php and edit the message there.

You can user AppServiceProvider.php
and you can write your new validation rule there and set your error message in validation.php file
/app/Providers/AppServiceProvider.php
/resources/lang/en/validation.php
Ex:
Validator::extend('before_equal', function ($attribute, $value, $parameters) {
return strtotime(Input::get($parameters[0])) >= strtotime($value);
});
and the message is
'before_equal' => 'The :attribute field is only accept date format before or equal to end date',
Please try this and you can alter this to based on your require.

Related

Laravel Predefined options in form request

I've created a FormRequest to validate some fields, and I would like that one of those fields only accept the options that a I give it
Searching I found something like this
"rule" => 'required|in:Option1,Option2,Option3',
This accept only the predifined options, but in the error message only shows
"The rule type is invalid."
And I would like that too shows the valid options predefined in the rules.
How could i do this?
In the end I modified my rule to this:
"rule" => ['required', 'in:Option1,Option2,Option3']
and I customized the error message with the following
public function messages()
{
return [
'rule.in' => 'The rule field must be Option1, Option2 or Option3.'
];
}

Laravel - form validation to pass only if certain field is empty

So I'm building a form and I need specific fields to be empty.
They return an empty string and from other similar questions, I looked for
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class
in Kernel.php which is commented out by default, I believe.
I don't want to change its behavior since it's a global middleware.
I have tried making them nullable, string|sometimes, present|max:0 yet none of these give me the desired result. I want the validation to pass only if the fields are empty.
Any help will be deeply appreciated.
So as I understood, you want for specific field to be required, if the other field in form is empty? To achieve that, you can use required_without property in Request validation like this:
public function rules()
{
return [
'filed_name' => 'required_without:other_field_name',
];
}
public function messages()
{
return [
'filed_name.required_without' => 'filed_name is required.',
];
}
More on validation on official documentation.

Laravel validation : different messages with the same rule

I have a validate like this :
if ($a > $b) {
$rule['to'] = 'required|date_format:Y-m-d H:i:s|after:from';
else {
$rule['to'] = 'required|date_format:Y-m-d H:i:s|after:now';
}
Can i have the two different messages like:
'to.after:from' => 'aaaaaaaaaaaa',
'to.after:now' => 'bbbbbbbbbbb',
Sorry my message is japanese so i don't up here.
I want to know if it has the way to show two messages with the same rule.
I tried to change the defaul message of laravel
'after'=> 'The :attribute must be a date after :date.',
but didn't work.
Please help me.
Thank so much!
I have checked your query in my example demo. I think Laravel cannot prived for the multiple attribute validation.
So here you can just Do as you done.
'to.after' => 'Your message',
You can create a Request to handle the validation separately from the controller

kohana custom validation error message

I have a callback within kohana validation. I'm sending error message this way:
public function way(Validate $array, $field)
{
if (something)
{
$array->error($field, 'super error message', array($array[$field]));
}
}
It works but when I print out the message
echo $errors['field'])
it returns formName.field super error message
How to get rid of formName.field?
These are messages configured in Kohana Core or in Modules or Application.
You can change them in messages folder
e.g. default validation message are in System -> messages -> validation.php
you an copy this file in your application and remove :field from them it will get rid of field name.
'not_empty' => ':field must not be empty',
change it to
'not_empty' => 'must not be empty',

Form input values not remain after flash error message

How to solve above issue? I want to keep test even after a validation failure in the submission. But after session error message is passed all the entered data will be gone.
To re-fill the form with the input data again, check the input validation then if validation fails, redirect the user back along with his input data and validation errors.
$validator = Validator::make($request->all(), [
// your validation rules.
'name' => 'required',
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
// Continue your app logic.
You'll find more information in Laravel validation docs
You should send back the input also to the view.
Like Sameh Salama already mentioned, use the following code:
function () {
return redirect()->back()->withErrors($validator)->withInput();
}
Notice the withInput() function, it returns the old input.
Use it in the View as
<input type="something" value="{{$input->first_something}}" />

Resources