Feature Request #4035
Nested validation objects
I would like the ability to validate multi-dimensional arrays and I think allowing the validation objects to be nested is the right solution.
Here is a quick example of the possible syntax. Will be working on a proof of concept soon.
$data = array( 'one' => 'Hello, World!', 'two' => array( 'a' => 'foo', 'b' => 'bar', ), ); Validation::factory($data) ->rule('one', 'not_empty') ->rule('two', 'is_array')// You can still validate the entire array as one rule ->rule('two', Validation::factory($data['two'])// Any validation object ->rule('a', 'not_empty') ->rule('b', 'not_empty') );
Adding the functionality is as simple as checking if the rule callback is an instanceof Validation instead of a callback and running check() on it... managing the errors is a little harder but we can figure that out. We could also make it a different method instead of adapting rule() but those are specifics.
#2 Updated by Kiall Mac Innes over 3 years ago
Not sure I like the syntax for consistency reasons, we use dot notation in other areas of Kohana, why not continue to? (Even if internally, it generates a new validation object etc as your example does)
Validation::factory($data) ->rule('one', 'not_empty') ->rule('two', 'is_array') ->rule('two.a', 'not_empty') ->rule('two.b', 'not_empty');
#3 Updated by Lorenzo Pisani over 3 years ago
- Target version changed from v3.3.0 to v3.2.0
Me and zombor kind of dislike dot notation because it's valid for array keys. It also actually makes the class more complicated (at least how I was trying to do it). I had initially tried doing something like that but was having issues with the storage of errors. Also, `Arr::path` is quite slow (not sure if that matters too much). What exactly don't you like? Would you be more likely to use something like this?
Validation::factory($data) ->rule('a', 'not_empty') ->validation(Validation::factory(...) ->rule('b', 'not_empty'));
My main issue is with the setting of data to the inner validation object so maybe it is best to do that automatically inside Validation.
#4 Updated by Kiall Mac Innes over 3 years ago
As I said, I'm simply going for consistency throughout here .. The originally suggested syntax is fine, I'm simply saying it's not consistent with how we handle nested arrays elsewhere... (which is basically .. umm .. just config off the top of my head .. so maybe just ignore me!)
#5 Updated by Isaiah DeRose-Wilson over 3 years ago
I dislike the dot notation syntax too. I think being able to inject a validation object in for a field rule would be very handy. It would also make reusing validation objects easier.
Unfortunately, as much as I would like to have this included in 3.2, I think it's probably best if we wait until 3.3 to add this change. However, this change would be backwards compatibility and not too much work to add... I'm open to including it in 3.2 if the other devs agree. We are pretty much at (or should be) our feature-freeze deadline though.