Tuesday, May 8, 2012

Re: Use the comntact model twice but with different required fields

Let's say that your current validation rules in model Contact looks like:

    public $validate = array(
        'street' => array(
            'allowEmpty' => true,
            'rule' => array('minLength', 4),
            'message' => 'At least %s characters required',
        ),
        'postcode' => array(
            'allowEmpty' => true,
            'rule' => array('postal', null, 'us'),
            'message' => 'Invalid postal code',
        ),
        'city' => array(
            'allowEmpty' => true,
            'rule' => array('minLength', 2),
            'message' => 'At least %s characters required',
        ),
    );

then you can implement beforeValidate callback in this model:

    public function beforeValidate($options = array()) {
        $allowEmpty = empty($this->data[$this->alias]['details']);
        foreach (array('street', 'postcode', 'city') as $field) {
            $this->validate[$field]['allowEmpty'] = $allowEmpty;
        }
        return parent::beforeValidate($options);
    }

and in contact form where contact details are required just

echo $this->Form->input('Contact.details', array('type' => 'hidden', 'value' => '1'));

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: