0

Has anyone been able to succssesfuly use the Respect Validation library with an Idiorm result object? It appears as though Respect Validation will only work on vanilla, not sure if that's the correct term, objects. Idiorm result objects allow you to access attributes by using $user->name but when you var_dump the object it's obviously just mapped to work like an object with attributes but isn't actually a straight object.

Excerpt from a dump below

object(ORM)[47]
  protected '_data' => 
    array (size=9)
      'id' => string '100000' (length=6)
      'name' => string 'test' (length=4)

The code below always fails because Respect Validation can't access the attribute through a reference. Is there a way around this? It would be great to use the objects as is and not have to convert them to arrays.

$user= ORM::for_table('user')->find_one(5);

$userValidator = v::attribute('name', v::string()->length(1,32));

$userValidator->validate($user);

1 Answers1

0

There isn't actually an attribute name into to the object. That is probably made available through magic methods which validation using attributes is not supported (because magic methods are not attributes).

What you could do though, is use this validator inside another validator for the _data attribute:


$user = ORM::for_table('user')->find_one(5);
$userDataValidator = v::arr()
                      ->key('name', v::string()->length(1,32));
$userObjectValidator = v::instance('ORM')
                        ->attribute('_data', $userDataValidator);
$userObjectValidator->validate($user);

Let me know if that works, I haven't tested it. Some rules may be with wrong names... But I think you can get an idea what I am going for.