get_fieldset()method of the caught exception would contain a nested array that contains all the fields, in the base model or any of its children, that did not pass validation.
class Model_User extends \Orm\Model
{
protected static $_observers = array('Orm\\Observer_Validation' => array(
'events' => array('before_save')
));
protected static $_properties = array(
'id',
'username',
'password',
'group',
'email' => [
'validation' => ['required', 'valid_email', 'max_length' => [255]]
]
);
class Model_Contact extends \Orm\Model
{
protected static $_observers = array('Orm\\Observer_Validation' => array(
'events' => array('before_save')
));
protected static $_properties = array(
'id',
'title',
'user_id',
'locale',
'street',
'house_number',
'zipcode',
'city',
'country',
'telephone' => [
'validation' => ['min_length' => [10]]
],
'mobile',
'email' => [
'validation' => ['required', 'valid_email', 'max_length' => [255]]
]
);
$user->set(\Input::json()); // Now works due to recent fix in 1.8 version of ORM!
try {
$user->save();
} catch (Orm\ValidationFailed $e) {
foreach ($e->get_fieldset()->validation()->error() as $field => $error) {
echo "Error: " . $error->__toString() . " ";
}
}
{"id":"2","username":"leo","group":"2","email":"foo","contact":{"title":"Leo Leeghte","street":"Westersingel","housenumber":"","city":"Rotterdam","zipcode":"3011 BG","country":"Nederland","telephone":"123","mobile":"0612345678","email":"bar","locale":"en_US"}}
Error: The field telephone has to contain at least 10 characters.
Error: The field email must contain a valid email address.
Error: The field email must contain a valid email address.
It looks like you're new here. If you want to get involved, click one of these buttons!