On my search results page each result has a contact form, with fields: name, phone. (So basically the search results page can have for example 10 forms on the same page.)
The form is set to POST to itself, so the user gets taken back to the same results page. Now when I submit the form on one of the results how can I make it identify which form was submitted (and output confirmation/error message on that result only)? Is there an easy way of doing this in Yii?
Ideally I want to be using the active field types so that I can retain the values if there are any errors.
Add a hidden field with the search result’s id in the form. Then after a failed validation, pass the validated form back to the view and you could do something like this:
foreach($results as $r) {
$form = $r->id === $validatedForm->resultId ? $validatedForm : new ContactForm;
// display your active input fields for $form
}
If you do this, all other contact forms will be on an empty model and have no errors, only the one that was posted will be marked in red.
Cheers for that Sander. I’ll have a go at that but I’m havin some difficulty understand what needs to go where. Could you provide some sample view/controller code so I can get a better idea?
The idea is that the view acts on an empty ContactForm except when a ContactForm was previously submitted and it’s id matches the one of the current result, in which case the validated ContactForm is used to generate the appropriate input fields and errorSummary().
Hi Sander. Thanks for that, I’ve got it almost working now apart from the flash message. Any idea how to make that display on the correct result? (refer to my code above to see this)
Also have you got any idea how I can get unique IDs on my input fields/labels and then set these attribute labels in the Model?
I mean the ID attribute of the input field / FOR attribute of the label. Obviously these need to be unique for each result. I can of course set these manually by appending $model->id to the end but then I don’t think I can get the attribute labels from the Model.