How to migrate URL parameters when CakePHP form validation fails

I am new to cakephp and trying to write a simple application with it, however I am stuck with some form validation issues.

I have a model called "Man" that has many "PersonSkill" objects. To add PersonSkill to a person, I set it to call a URL, for example:

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

I went through person_id because I want to display the name of the person for whom we are adding skills.

My problem is that if the check fails, the person_id parameter is not saved for the next request, so the username is not displayed.

The add method on the controller is as follows:

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());        
    }
}   

person_skill add.ctp , person_id, :

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

URL- person_id, , , ?

.

+3
2

FormHelper:: create() URL- .

, URL- , URL-, , named_ID - .

- :

echo $form->create('PersonSkill', array('url' => $this->params['named']));

CakePHP , .. ('person_id' = > 3) , URL-, .

, , $this- > data , else :

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
            ));
        }       
    }
    $this->Person->id = $this->params['named']['person_id'];
    $this->set('person', $this->Person->read());        
}   
+6

, URL. .

if( !empty( $this->data ) ) {
 if( $this->PersonSkill->validates() ) {
  if( $this->PersonSkill->save( $this->data ) {
   ...
  }
  else { // invalid data
   $this->redirect( array( 'action' => 'view', 'id' => $this->PersonSkills->id ) );
  }
 }
}

http://book.cakephp.org/view/410/Validating-Data-from-the-Controller

0

Source: https://habr.com/ru/post/1737325/


All Articles