How to Persist URL parameters when CakePHP form validation fails

Posted by am2605 on Stack Overflow See other posts from Stack Overflow or by am2605
Published on 2010-03-17T12:09:12Z Indexed on 2010/03/17 12:11 UTC
Read the original article Hit count: 357

Filed under:
|

Hi,

I'm new to cakephp and trying to write a simple app with it, however I'm stuck with some form validation issues.

I have a model named "Person" which hasMany "PersonSkill" objects. To add a "PersonSkill" to a person, I have set it up to call a url like this:

http://localhost/myapp/person_skills/add/person_id:3

I have been passing through the person_id because I want to display the name of the person we are adding the skills for.

My issue is if the validation fails, the person_id parameter is not persisted to the next request, so the person's name is not displayed.

The add method on the controller looks like this:

function add() {        
    if (!empty($this->data)) {          
        if ($this->PersonSkill->save($this->data)) {
            $this->Session->setFlash('Your person has been saved.');
            $this->redirect(array('action' => 'view', 'id' => $this->PersonSkill->id));
        }       
    } else {
        $this->Person->id = $this->params['named']['person_id'];
        $this->set('person', $this->Person->read());        
    }
}   

In my person_skill add.ctp I set a hidden field which holds the person_id, eg:

echo $form->input('person_id', array('type'=>'hidden','value'=>$person['Person']['id']));

Is there a way to persist the person_id url parameter when form validation fails, or is there a better way to do this that I'm missing completely?

Any advice would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about cakephp

Related posts about validation