Multiple form with same model name on single page cakephp -


i have 2 form on single page: login form , register form. when submit register form, validates both: form fields in login , registeration. how can handle if both form have same model (user model)

register form

<?php echo $this->form->create('user', array('url' => array('controller' => 'users', 'action' => 'add'))); ?> <?php echo $this->form->input('username', array('label' => false, 'div' => false, 'class' => 'reg_input'));?> <?php echo $this->form->input('email', array('label' => false, 'div' => false, 'class' => 'reg_input'));?> <?php echo $this->form->input('password', array('label' => false, 'div' => false, 'class' => 'reg_input'));?> <?php echo $this->form->input('confirm_password', array('type' => 'password', 'label' => false, 'div' => false, 'class' => 'reg_input'));?> <?php echo $this->form->submit(__('submit', true), array ('class' => 'reg_button', 'div' => false));    echo $this->form->end();?> 

and login form below

  <?php echo $this->form->create('user', array('controller' => 'users', 'action' => 'login'))?>   <?php echo $this->form->input('user.username',array('label'=>false,'div'=>false, 'class' => 'reg_input'));?>   <?php echo $this->form->input('user.password',array('label'=>false,'div'=>false, 'class' => 'reg_input'));?>   <?php echo $this->form->submit(__('log in', true), array ('class' => 'reg_button', 'div' => false)); ?>   <?php echo $this->form->end();?> 

when submit registration form validates both forms, want validate registration form.

how can handle that?

enter image description here

i've come "solution" (i find approach dirty, works) different question (very similar this). other question worked elements , views, though. i'll post entire solution here see if helps (though rather else comes different approach).

so, first: change creation names 2 forms.

//for registration <?php echo $this->form->create('registration',        array('url' => array('controller' => 'users', 'action' => 'add'))); ?> //for login <?php echo $this->form->create('login',        array('controller' => 'users', 'action' => 'login'))?> 

the forms should work, , post same actions, no harm done.

second step: don't have action code, i'm going explain needs done in general

public function login() {     if ($this->request->is('post')) {         //we need change request->data indexes make work         if (isset($this->request->data['login'] /*that's name gave form*/)) {             $this->request->data['user'] = $this->request->data['login'];             unset($this->request->data['login']); //clean work working             $this->set('formname', 'login'); //we need pass reference view validation display      } //if there's no 'login' index, can assume request came normal way      //your code should work     } } 

same thing registration (only need change 'login' 'registration').

now, actions should behave normally, since has no idea changed form names on view (we made sure of changing indexes in action). but, if there validation errors, view check them in

$this->validationerrors['model_with_errors'] 

and 'model_with_errors' (in case 'user') won't displayed in respective forms because we've changed names. need tweak view. oh! i'm assuming these both forms in view called index.ctp, example, if on separate files (if you're using element or similar) recommend add lines of code files

//preferably in first line of view/element (index.ctp in example) if (!empty($this->validationerrors['user']) && isset($formname)) {     $this->validationerrors[$formname] = $this->validationerrors['user']; } 

with that, copy model validation of user fake-named form, , one. note if have third form in view same model, , use typical $this->form->create('user'), validation errors show 1 unless change form name third one.

doing should work , validate form correct name.

i find messy approach because involves controller-view changes. think should done controller, , view shouldn't blink validation issues... problem render function of controller.php needs replaced... can done in appcontroller, every updgrade of cakephp, you'll have careful of copying new render function of controller.php 1 replacing in appcontroller. advantage of approach, though, "feature" available every form without having worry changing views.

well, it's not maintainable anyway, better leave alone if it's 1 case... if interested on how handle in controller side, though, comment , i'll post it.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -