I have a multiple fields binded to "x-editable" plugin that sends the changed data and like all of the data, I want to validate them.
But the problem here is that it always sends just 2 fields: name=is_on_probation&value=1
. Here, the name
key is the field that must be validated (it is the same as DB column) and a value
is pretty self-explanatory.
I've written a validation rule like this:
'is_on_probation' => 'sometimes|...'
But then I realized that it won't be validated, because the actual subject of validation is in the value
field.
So, the question here is: how can I validate the value
field and apply the corresponding rule, accordingly to the name
value?
I created this logic in order not to pollute the controller with additional arrays and logic, so it is going to save only the passed parameter.
Kinds regards!
P.S. I've searched for the StackOverflow, but did not find the question that describes my problem the best. May be I am wrong and did not see it.
Update
After thinking a bit, I cam up with the solution to use switch
and pass the request field there, like this:
switch($this->request->get('name')) {
case "email":
$rules = [
'value' => 'sometimes|required|unique:users,email|email:rfc',
];
break;
case "phone":
$rules = [
'value' => 'sometimes|required|min:8|max:13|regex:/^(\+371)?\s?[0-9]{8}$/'
];
break;
}
$rules[] = [
'pk' => 'required|exists:users,id'
];
The question is still in place, however - is it the best solution and a good practice to do so?
Try to merge your data into the request before starting validation.
Then your validation