Multiple form with same model name on single page cakephp

前端 未结 3 1251
长情又很酷
长情又很酷 2020-12-19 10:49

I have two form on a single page: login form and register form. When I submit the register form, it validates both: form fields that are in login and registeration. How can

3条回答
  •  渐次进展
    2020-12-19 11:07

    You can duplicate your model and change his name and define $useTable as the same table name.

    Example :

    class Registration extends AppModel {
    
    public $useTable = 'users';
    

    You define the action in form->create like Nunser for your login form

     Form->create('User',array(
            'url' => array(
                'controller' => 'Users',
                'action' => 'login',
                'user' => true
            ),
            'inputDefaults' => array(
                'div'   => false,
                'label' => false
            ),
            'novalidate'=>true,
        ));
    ?>
    

    and your registration form

    Form->create('Registration',array(
            'url' => array(
                'controller' => 'Users',
                'action' => 'validation_registration',
                'user' => false
            ),
            'inputDefaults' => array(
                'div'   => false,
                'label' => false
            ),
            'novalidate'=>true,
        ));
    ?>
    

    In your controller define a method for registration validation and the most important define the render

    public function validation_registration(){
        $this->loadModel('Registration');
    
                if($this->request->is('post')){
    
                    if($this->Registration->save($this->request->data)){
    
                        --- code ---
                    }else{
                        --- code ---
                    }
                }
    
                $this->render('user_login');
    }
    

    Sorry for my english ! Have a nice day ! :D

提交回复
热议问题