Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a validation rule for "not present"?

I need to check if the key is not set in the array using Laravel validator.

That would be the complete opposite of the "required" validation rule.

Basically the array will be passed to update method if it passes the validation and I want to make sure one column will not be updated.

Is there a way to check if the value "is not present"?

Thank you

EDIT:

I'm currently using Laravel 5

EDIT:

I managed to write my own validation rule by calling Validator::extendImplicit. However I get $value as null to my validation function both when I set it to null or when I don't set it at all. Is there a way to check if the value is set?

like image 939
MaGnetas Avatar asked Feb 11 '23 19:02

MaGnetas


1 Answers

I believe I found a solution:

$validator->extendImplicit('not_present', function($attribute, $value, $parameters)
{
    return !array_key_exists($attribute, $this->data);
});

I'm not calling extendImplicit statically because the Validator class object is injected to the controller of my class.

I need to access $this->data ($this referring to the Validator object) to make sure the key doesn't exist in the array being validated.

like image 102
MaGnetas Avatar answered Feb 13 '23 22:02

MaGnetas