Welcome Guest, Not a member yet? Register   Sign In
Form submission, validation, re-display
#1

[eluser]Joey Dendron[/eluser]
Hi there

I've been using CI for a while and I want to refine the way I handle form submission, $_POST validation and re-display. This is in the context of editing an existing record. I was wondering if there were any established best practices I should be aware of, or if anyone had any thoughts.

What I want to achieve is:

* User views form containing current record details (SELECTed from database).
* User edits form content
* User clicks Submit button.
* Form submitted by straight HTTP POST, to the same URL (I'd like to avoid separate "Save" URLs if possible).
* Form validation runs
* If validation fails, user sees the form again, with errors highlighted and user input overwriting the values from the database record (so they don't need to re-type anything that was correct)
* If form validation succeeds, the record is UPDATEd, and user sees the form again, with a success message.

The way my controller works at the moment is:

* Looks in userdata for ID of database record (I don't want the ID visible in the URL, so I'm recording it in userdata; I can rely on cookies and javascript, because those requirements are in the application specification)
* Relevant model returns array of field values in $data['form_values'].
* If there's a $_POST, it's filtered, XSS-cleaned, then validated.
* If $_POST passes validation, $this->save() is called, which tells the model to save data to database. $data['feedback'] is set to "success" or "failed saving to DB" based on the result of $this->save().
* If $_POST fails validation, $data['feedback'] is set to validation_errors(), and $data['form_values'] is array_merged with $_POST so that the (invalid) $_POST values overwrite the original values from the database record. This is so that, if the user changed 5 fields and only 1 failed validation, they benefit from not having to re-enter valid input.
* Display view showing the form and feedback.

What I like about this technique is that, if user input fails validation, they can see what they entered: no re-typing required for any correct values they changed, and they can see invalid values, which should help them check and change them. I also like that they don't have to click away from a special feedback screen, or wait for a delayed redirect to kick in.

What annoys me slightly is that I lose standard HTML reset button functionality: the form doesn't reset to the original record values, but to the last-entered form input; so I need to replace my reset button either with a button + some redirect javascript, or a link that looks like a button, to re-load the page (with no $_POST, thus re-loading original values from the database).

Is this a reasonable standard for my application? It's a pretty fundamental part of the user experience in web applications generally... are there any established best practices, either in CodeIgniter or more generally, that I'm missing?

Sorry for the long post - it's just such a basic aspect of my work that I wanted to try to explain the issue fully.

Thanks for reading!

JD




Theme © iAndrew 2016 - Forum software by © MyBB