I came up with a “solution” (I think this approach is dirty, but it works) for another question (very similar to this). However, this other question worked with elements and views. I will post the entire solution here to find out if this helps someone (although I prefer someone else to come up with a different approach).
So first: change the creation names for the two forms.
//for the registration <?php echo $this->Form->create('Registration', array('url' => array('controller' => 'users', 'action' => 'add'))); ?> //for the login <?php echo $this->Form->create('Login', array('controller' => 'users', 'action' => 'login'))?>
Forms should work, watch and publish the same actions, so there was no harm.
Second step: I do not have an action code, so I will explain what needs to be done at all
public function login() { if ($this->request->is('post')) { //we need to change the request->data indexes to make everything work if (isset($this->request->data['Login'] /*that the name we gave to the form*/)) { $this->request->data['User'] = $this->request->data['Login']; unset($this->request->data['Login']); //clean everything up so all work as it is working now $this->set('formName', 'Login'); //we need to pass a reference to the view for validation display } //if there no 'Login' index, we can assume the request came the normal way //your code that should work normally } }
The same is for registration (you only need to "Login" to "Registration").
Now the actions should behave normally, because they have no idea that we changed the names of the forms in the view (we made sure that we change the indexes in the action). But, if there are validation errors, the view will validate them in
$this->validationErrors['Model_with_errors']
And that "Model_with_errors" (in this case, "User") will not be displayed in the corresponding forms, because we changed the names. Therefore, we also need to customize the view. Oh! I assume that these two forms are in a view called index.ctp , for example, but if they are in separate files (if you use an element or similar), I recommend adding lines of code for all files
//preferably in the first line of the view/element (index.ctp in this example) if (!empty($this->validationErrors['User']) && isset($formName)) { $this->validationErrors[$formName] = $this->validationErrors['User']; }
In doing so, we copy the user model check into a fake form and only one. Please note: if you have a third form in this view for the same model, and you use the typical $this->form->create('User') , then validation errors will be displayed for this, if you do not change the form name for this third.
Doing this should work and only validate the form with the correct name.
I find this useless approach because it includes changes in the form of a controller. I think that everything should be done by the controller, and the view should not even blink about validation problems ... The problem is that the render Controller.php function needs to be replaced ... This can be done in the AppController , but for each Cakephp update you you need to be careful when copying the new rendering function Controller.php to the one that replaces it in AppController . The advantage of this approach is that the “function” will be available for each form without worrying about changing attitudes.
Well, it's just not that maintainable anyway, so it’s better to leave it alone if it’s only for this one case ... If someone is interested in how to handle this only on the controller side, though, comment, and I will post it.