Exchanger13
Exchanger13

Reputation: 337

Two Forms in a view Cakephp

two forms in one view Login,Register

the register :

<?php echo $this->Form->create('User',array('class'=>'box','action'=>'register')); ?>   

the login :

<?php echo $this->Form->create('User',array('class'=>'box','action'=>'login')); ?> 

The code is written in the Login view , and i'm using custom validation , the login works perfectly because I'm in the Login view (i guess) , But when i submit the Register Form it takes me away to another page : /users/register how can i stop that.

Secondly , since both of the Forms have username and password fields they are affecting each other , I mean when I write a wrong username and password , they don't disappear after validation and that's ok but they appears also in the register Form , and that's not !

Upvotes: 0

Views: 542

Answers (1)

CoolLife
CoolLife

Reputation: 1479

Still another approach

Just add a hidden field to your forms to indicate which form has been sent;

echo $this->Form->create('User',array('class'=>'box','action'=>'register')); 
echo $this->Form->hidden('formsent', array('value' => 'register'));
echo $this->Form->end('Register');


echo $this->Form->create('User',array('class'=>'box','action'=>'register'));
echo $this->Form->hidden('formsent', array('value' => 'login'));
echo $this->Form->end('Login');

And inside your controller, just one controller;

    if ($this->request->is('post')) {
        if ('register' === $this->request->data['Tblforumuser']['formsent']) {
            //register

        } else {
            // login
            if ($this->User->validates(array('fieldList' => array('email', 'password')))) {

            } else {

            }
        }
    }

on The ‘on’ key can be set to either one of the following values: ‘update’ or ‘create’. This provides a mechanism that allows a certain rule to be applied either during the creation of a new record, or during update of a record.

If a rule has defined ‘on’ => ‘create’, the rule will only be enforced during the creation of a new record. Likewise, if it is defined as ‘on’ => ‘update’, it will only be enforced during the updating of a record.

example in your validation:

'email' => array(
    'required' => array(
        'on'         => 'create',
        'rule'       => 'notEmpty',
        'message'    => 'Enter your email address',
        'required'   => true,
        'last'       => true
    )

Upvotes: 2

Related Questions