CakePHP deep (multiple related models) validation?

142 views Asked by At

I have a model structure that is:

Organization belongsTo Address belongsTo CountryCode

So, Organization has foreign keys: mailing_address_id and physical_address_id

Address has foreign key: country_code_id

In the Organization model, relationship is defined as:

public $belongsTo = array(
        'MailingAddress' => array('className'=>'Address', 'foreignKey'=>'mailing_address_id')
        , 'PhysicalAddress' => array('className'=>'Address', 'foreignKey'=>'physical_address_id')
);

This appears to be working great - validation is functioning properly, etc.

In the Address model, relationship is defined as:

public $belongsTo = array(
        'CountryCode' => array('className'=>'CountryCode', 'foreignKey'=>'country_code_id')
    );

In my OrganizationsController, in a function to create a new organization, I'm testing validation using this code:

    if($this->Organization->saveAll(
        $data, array('validate'=>'only')
    )) {
        // Validates
        $this->DBG('Org validated.');
    } else {
        // does not validate
        $this->DBG('Org NOT NOT NOT validated.'.print_r($this->Organization->invalidFields(),true));
    }

The $data array looks like this going into validation.

2015-06-08 21:03:38 Debug: Array
(
    [Organization] => Array
        (
            [name] => Test Organization
        )

    [MailingAddress] => Array
        (
            [line1] => 100 Main Street
            [line2] => 
            [city] => Houston
            [state] => TX
            [postal_code] => 77002
            [CountryCode] => Array
                (
                    [name] => United St
                )

        )

    [PhysicalAddress] => Array
        (
            [line1] => 100 Main Street
            [line2] => 
            [city] => Houston
            [state] => TX
            [postal_code] => 77002
            [CountryCode] => Array
                (
                    [name] => United St
                )

        )

)

The country code SHOULD NOT validate with the rules I have set in the CountryCode model:

public $validate = array(
    'name' => array(
        'nonemptyRule' => array(
            'rule' => 'notEmpty'
            ,'required' => 'create'
            ,'message' => 'Must be provided.'
        )
        ,'dupeRule' => array(
            'rule' => array('isUnique', array('name','code'), false)
            ,'message' => 'Duplicate'
        )
    )
    ,'code' => array(
        'rule' => 'notEmpty'
        ,'required' => 'create'
        ,'message' => 'Must be provided.'
    )
);

However, validation PASSES on Organization->saveAll.

Also, if I attempt to access the CountryCode model from the OrganizationController, it's not loaded.

As in:

$this->Organization->MailingAddress->CountryCode->invalidate('name','Invalid!');

In that case, I get an error that CountryCode is null.

Any ideas why CountryCode wouldn't be validating or loaded?

Is validation supposed to work two steps away?

1

There are 1 answers

0
JR Lawhorne On BEST ANSWER

It turns out, there IS a deep option when validating (and saving). It's documented here with the saveAll options:

http://book.cakephp.org/2.0/en/models/saving-your-data.html

So, the validation block in the question works perfectly well if you include the deep option like so:

if($this->Organization->saveAll(
    $data, array('validate'=>'only', 'deep'=>true)
)) {
    // Validates
    $this->DBG('Org validated.');
} else {
    // does not validate
    $this->DBG('Org NOT NOT NOT validated.'.print_r($this->Organization->invalidFields(),true));
}