huzzah
huzzah

Reputation: 1805

Error messages not working on one model, when validation fails

I have a form that calls on two separate models. My validation works correctly in that incorrectly entered data fails validation; however, the error messages only show on the RELATED model data. Here is a snippet of my form with both models:

echo $this->Form->input('Location.exchange', array('size'=>'3', 'error' => array('class' => 'error')));
echo $this->Form->input('Location.sln', array('size'=>'4', 'error' => array('class' => 'error')));
echo '<br />';
echo $this->Form->input('unit_website', array('size'=>'65', 'label'=>'Your unit\'s website', 'error' => array('class' => 'error')));
echo '<br />';

echo $this->Form->input('specials', array('size'=>'65', 'label'=>'Your website\'s Specials page', 'error' => array('class' => 'error')));
echo '<br />';

Error messages will be display whenever validation fails on Location, but not the other (which is Unit), which is ironic, since I'm in my UnitsController. Here is the controller code:

function edit($id) {
 $this->set('title', 'Edit your property');
 $this->Unit->id = $id;    
 if (empty($this->request->data)) {        
$this->request->data = $this->Unit->read();    
} else { 

    if ($this->Unit->saveAll($this->request->data)) {            
        $this->Session->setFlash('Your property has been updated.', 'success');            
    } else {
        Set::merge($this->Unit->read(), $this->request->data);
    } 
}
}

and here is a snippet of the validation arrays from both my Location model and my Unit model: (from model Unit):

public $validate=array(
    'type'=>array(
        'rule'=>'notEmpty',
        'message'=>'You must choose what type of property this is.'
    ),
    'unitnum'=>array(
        'rule'=>array('custom', '/^[a-z0-9 -\'.\/&]*$/i'),
        'message'=>'Must be the name or number of your unit.'
    )
 );

(from model Location):

public $validate = array(
        'area_code'=> array(
            'ac1'=> array(
                'rule'=>'numeric',
                'message'=>'Must be a number'
            ),
            'ac2'=>array(
                'rule'=>array('comparison', '>=',100),
                'message'=>'You must enter a valid area code'
            )
     );

Upvotes: 1

Views: 106

Answers (1)

nIcO
nIcO

Reputation: 5001

If you look at the Model->read() function, you will see that it starts with

$this->validationErrors = array();

So the line

Set::merge($this->Unit->read(), $this->request->data);

clears the validation errors

Upvotes: 1

Related Questions