Thursday, November 1, 2012

How to pass HABTM validation errors to FormHelper?

Hi!
I read many topics about HABTM validation in Cake but I can't figure out how to solve my problem.

I have my main model – Restaurants, it has many HABTM relations to additional models.
Now I'm building admin area, and I'm working with new controller in which I have

    public $uses = array('Restaurant');

This admin area controller have an edit action: 
     
    ... else {
        if ($this->Restaurant->save($this->request->data)) {
            $this->Session->setFlash('Ok!');
            $this->redirect(array('action' => 'edit', $id));
        } else {
            $this->Session->setFlash('Error');           
            $this->redirect(array('action' => 'edit', $id));
        }
    }

In my Restaurant model I have:

    function beforeValidate() {                      
foreach($this->hasAndBelongsToMany as $k=>$v) {
if(isset($this->data[$k][$k]))
{
$this->data[$this->alias][$k] = $this->data[$k][$k];
}
}
    }
I can't comment this code because it's from HABTM validation guide.

And also in Restaurant model I have some validation rules:

    var $validate = array(
        'engcompanyname' => array(
        'rule' => 'notEmpty',
        'message' => 'Test message'
    )
    );

In edit.ctp I have standard form builded with FormHelper:

     <?php echo $this->Form->create(); 
    ...
    echo $this->Form->input('engcompanyname', array('label' => 'Test label'));
    ...
    echo $this->Form->end(array('label' => 'Save');
    ?>

The problem is that validation itself works very well, I got my session message back for success and error conditions, the data from the form correctly passes to database etc.  But FormHelper does not show any error messages.

CakePHP version is 2.2.2

Thanks in advance!

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: