1

In general, what should be the best way to repopulate a form when submission fails due to invalid inputs.

Here, invalid would mean something did not pass the check or some validation methods server side. So upon failure the form is reloaded and user would like to see his data repopulated.

How do we do this in general, what is the most effecient way (resource wise, session should not be a resource effecient way IMO)?

  • If your web app does synchronous request (w.o ajax) where the form submits causes a request to the server and a page reloading, nothing is stopping you from making server returns back the same input data along with the extra messages (validation messages). Everything will happen in a single request. – Laiv Nov 16 '16 at 8:23
1

You are looking for Data Binding (Wikipedia):

Data binding is a general technique that binds data sources from the provider and consumer together and synchronizes them...

In a data binding process, each data change is reflected automatically by the elements that are bound to the data. The term data binding is also used in cases where an outer representation of data in an element changes, and the underlying data is automatically updated to reflect this change. As an example, a change in a TextBox element could modify the underlying data value.

Many web frameworks in multiple languages offer some kind of data binding. It's a little different for web applications than it is for installed applications due to the asynchronous and stateless nature of HTTP connections.

A quick example using the ASP.NET MVC Framework:

public class BlogPostsController : Controller
{
    // [1]
    public ActionResult Create()
    {
        BlogPostModel model = new BlogPostModel();

        return View(model);
    }

    // [2]
    [HttpPost]
    public ActionResult Create(BlogPostForm model)
    {
        if (ModelState.IsValid)
        {
            BlogPost post = blogPostService.CreateNewPost(model);

            return RedirectToAction("Details", new { id = post.Id });
        }

        return View(model);
    }
}
  1. Above is the Controller handling the user interaction. The Create() method is executed when a GET request is sent to the URL /BlogPosts/Create.

  2. The Create(BlogPostForm) method is executed when the <form> is submitted. Here we check if (ModelState.IsValid) which runs the validations on the form fields. If it returns false, then we have validation errors. The return View(model) line re-renders the "Create Blog Post" form so it can display error messages to the user. If the model state is valid, then we save to the database and issue an HTTP redirect response back to the browser.

Now, the data binding part comes in to play in the template used to create the HTML of the web page (in the Views/BlogPosts/Create.cshtml file):

@model BlogPostForm

@using (Html.BeginForm())
{
    <p>
        <label>
            Title:
            <!-- "Binds" the Title property to the text box -->
            @Html.EditorFor(model => model.Title)
        </label>

        <!-- Shows validation messages for the Title property -->
        @Html.ValidationMessageFor(model => model.Title)
    </p>

    <p>
        <button type="submit">Save</button>
    </p>
}

Two method calls represent the actual "data binding" process:

@Html.EditorFor(model => model.Title)

This creates an <input type="text" value="..."> HTML tag, where the value property gets filled with the current value of the Title property in the model. The framework does this for the programmer.

Now, the validation messages:

@Html.ValidationMessageFor(model => model.Title)

This displays all validations messages for the Title property. Again, the programmer doesn't have to do much. The framework does most of the work for you.

Using AJAX and/or JavaScript for form validations

JavaScript can be disabled. A malicious user can always get around JavaScript validations, even AJAX validations. Always. Always, always, always, always, always, always, always run all validations on the server when processing the form submit. Malicious users can't bypass the server to put data in the database (SQL injection flaws not withstanding).

Client side validations are just a convenience, and nothing more.

0

There is nothing wrong with storing the submitted values in the session and using them to repopulate the form.

You could, however, use Javascript to submit the data and notify of any validation errors without actually leaving the form.

  • 1
    I don't know what the rest of the world thinks but to me form validation has to be server side, JS is just a fancy layer of web which user can control all the way, so I won't depend on any such thing. And session is memory constraint resource. I was expecting any better actually :) – edam Oct 25 '16 at 10:15
  • 2
    @edam I meant using Javascript to pass the form to the server for validation--AJAX or something similar. But is there a specific reason why you're concerned about the session being an inefficient method? I don't see the problem there. – user82096 Oct 25 '16 at 10:23
  • Isn't session a limited resource memorywise? – edam Oct 26 '16 at 11:44
  • @edam, there is a limit, since you have to have enough memory to store all users' sessions. But I think you are probably worrying about this prematurely. Storing all the form values in the session would be a quite typical thing to do and is unlikely to be problematic unless you have a gigantic form or huge number of concurrent users. – user82096 Oct 26 '16 at 14:31
0

You could implement this using an AJAX solution. Pass the form via AJAX to a PHP script that would validate your inputs and store them (if they pass) then return either a true or false.
If your script returns false, you can simply stop the page from doing anything leaving all the inputs intact, the user wouldn't see any change to the page and nice elegant message could then be displayed (using something like sweet alerts), or even better the offending item flagged to the user, so they can rectify the issue.

  • what is the most efficient way (resource wise, session should not be a resource efficient way IMO)` I think your solution adds some complexity to the system. It also does extra server calls to the server: the 1st for validations, 2nd for data submit. – Laiv Nov 16 '16 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.