Reputation: 1204
i have a strange problem, i have a page where in top right there is a login panel,
and somewhere on the middile of the page there is a register form.
Both login form and register form has fields from User Model which username and password.
My problem is when register form submits the validation rules are activated but its shown
both in register form and login form which i dont want.
I want it to work this way, when i submit register form validate only register form and vice versa..
i couldnt find a workaround for this...plz guide me in the right direction.
Upvotes: 1
Views: 160
Reputation: 8540
I've had a similar issue with a login and registration form on the same page.
The way I get round the validation errors issue is to relabel the login form elements so that they're labelled as 'Login' rather than 'User'. For example, instead of $this->Form->input('User.email')
I have $this->Form->input('Login.email')
. I then include a hidden 'step' field in each form and check which is present when the form is submitted.
If the login form is being submitted I switch the Login form elements to the correct model before proceeding:-
$this->request->data['User'] = $this->request->data['Login'];
This way the logic is in the controller rather than the view.
Upvotes: 0
Reputation: 1419
To show validation errors only in form which is posted and not in other form, you can add check in your view where errors are displayed with the submit button name as in both forms submit buttons will have different names say submit1 and submit2.
// In Form1
if(isset($_POST['data']['form1']['submit1']))
{
//display validations for form1
}
// In Form2
if(isset($_POST['data']['form2']['submit2']))
{
//display validations for form2
}
Upvotes: 1