I have a controller that handles Route::post, this controller will do an input validation using Illuminate\Support\Facades\Validator. Now I set all the rules and threw the errors to a view via Redirect()->to('some/path')->withErrors($validator)->withInput($inputs). Now I needed to make sure that two fields combined together does not exists on two columns combined together from a database table.
table: test
columns
I must make sure that the title+subtitle inputs does not exists on the title+subtitle columns. So I created an if statement for that and also taking into consideration that the subtitle is an optional field. If the condition is met I will need to add a new error message along with other existing error messages from the validator::make so from the variable holding that validator instance I will do a $validator->errors()->add('title', input['title'] . ' ' . input['subtitle'] . ' already exists.'); this wil work ONLY INSIDE THE IF STATEMENT but outside of it this error doesn't seem to exists so if I do:
if (condition) {
$validator->errors()->add('field', 'error message');
/*
* outputs all the errors with the recently added ERROR MESSAGE
*/
print_r($validator->errors()->all());
}
/*
* outputs all the errors WITHOUT the recently added ERROR MESSAGE
*/
print_r($validator->errors()->all());
if (condition) {
$validator->errors()->add('field', 'error message');
/*
* outputs all the errors with the recently added ERROR MESSAGE
*/
$errors = $validator->errors()->all();
}
return Redirect()->to('path')->withErrors($errors)->withInput($inputs);
This will work! but I believe that I don't have to get around this and this should not happen all the errors I added should be accessible later on after I added them.
create a controller that handles post form inputs.
create a variable to handle user input with Input::all()
create a variable that will handle validation rules
do a Validator::make(inputs, rules) and assign it to a variable
make a custom error check with if statement
do a $validator->errors()->add('myField', 'error message') if the condition was met
You don't need to throw it to a view simply do
display all errors via $validator->errors()->all() INSIDE THE IF STATEMENT'S BODY using print_r
display all errors via $validator->errors()->all() OUTSIDE THE IF STATEMENT'S BODY using print_r
just return nothing.
@four-eyes-04-04 I'm sorry but I don't seem to understand your issue.
When you do $validator->errors()->add('field', 'error message'); what do you expect? and what do you get that's different from what you expect?
Please format the code snippets to make them readable https://guides.github.com/features/mastering-markdown/
this wil work ONLY INSIDE THE IF STATEMENT but outside of it this error doesn't seem to exists
PHP doesn't work like this, there's no scope change inside an if statement, looks like for some reason the condition is false and the message isn't added.
@themsaid I'm sorry, let me clarify a few things for you and also I hope you can help me out with this.
When you do $validator->errors()->add('field', 'error message'); what do you expect? and what do you get that's different from what you expect?
I am expecting that $validator->errors()->add('field', 'error message') would add a new error message to the validator instance.
I am also expecting that $validator->fails() would return true if the and error is present and that should include the errors I added via $validator->errors()->add().
I am also expecting that $validator->errors()->all() would return all the errors including the error I added via $validator->errors()->add() but it doesn't
PHP doesn't work like this, there's no scope change inside an if statement, looks like for some reason the condition is false and the message isn't added.
I know and I don't need to be reminded, did you managed to replicate the scenario?
@four-eyes-04-04 but you say it works when you're inside the condition, right?
Was this closed due to a misunderstanding of the question?
I experimented a bit with this and found that the fails() method is actually mutating the errors list. A call to fails() actually clears all added error messages. I had to work around calling fails() to avoid clearing my custom error messages. I expected fails() to basically return !empty($this->errors()) instead of erasing all error messages and recalculating them.
@aprilmintacpineda I know this post is old, but can help someone else!
To $validator->errors()->add(...) works you need to do like this:
$validator = Validator::make(...);
$validator->after(function($validator) {
if ($this->somethingElseIsInvalid()) {
$validator->errors()->add('field', 'Something is wrong with this field!');
}
});
if ($validator->fails()) {
//
}
@themsaid stop to be rude...
Most helpful comment
@aprilmintacpineda I know this post is old, but can help someone else!
To
$validator->errors()->add(...)works you need to do like this:@themsaid stop to be rude...