Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Validation when setting and saving nested models
  • In reference to this thread:

    Does 1.8 already have a validation system that returns a nested fieldset when setting a nested array?

    Currently in ORM 1.8, when saving a model after setting it with an array with child arrays, saving works fine, but the validation seems to return the fields of only one of either a child or the parent model that has been changed.

    It would be ideal if the
    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.


    Example

    I have two models:

     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]]
    ]
    );


    and

    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]]
    ]
    );


    I have the following put handler:

            
    $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() . " ";
    }
    }


    When I PUT the following payload:

    {"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"}}

    (note that there is invalid info in the email field in the user model but also in the email field in the contact model, and additionally there is invalid info in the telephone field in the contact model)

    I currently get the following echo's:

    Error: The field telephone has to contain at least 10 characters. 
    Error: The field email must contain a valid email address.


    It would be great if there was a way to get validation errors for all invalid fields.


  • UPDATE

    The example wasn't entirely accurate, it looks like the user model email field was already set to 'foo' before, so it was skipped by validation. When I change the contents of this field to something else before PUTting, I now get the validation errors for the user model only:


    Error: The field email must contain a valid email address.


    and not the validation errors for the contact model.
  • HarroHarro
    Accepted Answer
    Fieldset and Validation are a bit voodoo I'm afraid.

    I never use nested fieldsets, so I would have to setup a test environment and dive into it, something I don't have the time for at the moment.

    Can you create an issue for this (https://github.com/fuel/core/issues) so it can be looked at?
  • Thanks for the info!

    I created the issue.
  • HarroHarro
    Accepted Answer
    Already responded. ;-)

    For other readers: https://github.com/fuel/core/issues/1853

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion