Restful icon indicating copy to clipboard operation
Restful copied to clipboard

Required rule not validating arrays correctly

Open JanMikes opened this issue 8 years ago • 1 comments

$this->input->field("tickets")
    ->addRule(IValidator::REQUIRED);
"tickets" => [
    228 => 1,
]

And i get 422 Unprocessable Entity which is pretty bad! When i remove this validation and do $this->resource->tickets = isset($this->input->tickets) ? $this->input->tickets : NULL; it is printed correctly

JanMikes avatar Dec 06 '15 00:12 JanMikes

I found this issue today as well. One update, what is not working correctly as well.

Lets say that POST body looks like:

{
    "ticket": {
        "id": 5,
        "name": "ticket name"
    }
}

I'll use validation rule REQUIRED and custom CALLBACK.

$this->input->field("ticket")
    ->addRule(IValidator::REQUIRED)
    ->addRule(IValidator::CALLBACK, 'erorr', function($value) {
        return count($value) > 0;
    });

Expected behavior will be that required will check that I send ticket at all. Custom callback will check, that at least one ticket is present. Problem is that this will partially work, unfortunately the validation method will check, that ticket exists and meet custom callback in all sub-arrays as well. That means that it will check, that ticket exists in $body['ticket'] as well.

I think that this is not correct behavior and ->field(*)->addRule() should validate values only in the root of the data, not all arrays inside. It will be nice to add feature, to validate keys in another array, i.e. ->field('ticket/id')->addRule().

I can fix this, is this the way you want to go with?

marten-cz avatar Mar 29 '16 11:03 marten-cz