vtfuture / BForms

Bootstrap Forms for ASP.NET MVC
MIT License
62 stars 33 forks source link

Request for example on using remote validation #223

Closed jstuardo closed 9 years ago

jstuardo commented 9 years ago

Hello,

I have a form that contains a field called "code". I need to validate if that code already exists in database, so that I need to use remote valitation.

I saw bforms.validate.unobtrusive.js file and it shows a remote validation, so I know it is possible, but, how to use it?

Thanks Best regards

Jaime

alessandromuresan commented 9 years ago

@jstuardo you have to do this in the controller:


if (codeExists)
{
    // Add a new error to the ModelState, referencing your "Code" property
    ModelState.AddModelError("Code", "This code looks strikingly familiar");
}

// Return a BsJsonResult containing the model errors
// and signal the client that the things you are returning are actually errors

return new BsJsonResult(
new
{
   Errors = BForms.Mvc.ModelStateExtensions.GetErrors(ModelState)
},
BsResponseStatus.ValidationError);

This ofcourse implies that, on the client side, you are using one of the Javascript widgets which support AJAX-enabled forms (such as bsForm or bsPanel).

Let us know if you can manage with only this information. If not, me or somebody else will find some time to write you a propper example (client + server).

Good luck :D

jstuardo commented 9 years ago

Thanks for your reply. Yes, i have thought about doing that in server side, however, i am using bsform. With it,when I press submit button, client side validation occurs, for example, by validating required fields, e-mail fields and so on. If some field is incorrect, the field is marked in red with an icon appears and actually never a submit occurs to the server.

I was planning to do somethng similar with remote validation, so, when submit button is pressed, the validator checks the code by mean of ajax, and if the code already exists, the code field is marked.

I know about bootstrap validator, http://bootstrapvalidator.com/validators/remote/, but I don't know how to integrate it with bsform validator.

I will thank if you can guide me on this. If there is no an easy solution, I will do it server side.

regards, Jaime

alessandromuresan commented 9 years ago

Okay, this process of remote validation doesn't quite make sense for your current application design. Consider the fact that every time the form is submitted, there will be two requests to the server: one for validating the code and one for saving the actual form. Instead, why not combine the two processes into one and always make only one request?

An example of when you would need remote validation is when your validation logic is, well ... remote (i.e it isn't on the same domain as your server). For instance, codes that need to be validated using an API provided by some random state authority. You don't rely on third-party validation, because you have all the information you need in your own database :trollface:

I hope I've convinced you about the redundancy of remote validation in your case.

Best regards, Alessandro :godmode:

jstuardo commented 9 years ago

Never mind.. I could finally integrate bootstrap remote valdator with bforms and it worked, however, I think examples should be more complete in the webpage, including something like remote validation, which I am sure it is a very common task.

I am not convinced not to do it, because server posts is no actually problem, and in fact, posting all the form data to the server is slow, however, by just validating the code is very fast. The code validation is carried out when the user changes the input field and validation last less than a second and it is made without user notice.

On the other hand, when user presses the submit button, having all correct data in the form, the actual post last about 1 second. it would be annoying for the user to wait, the first post, then the whole page rendering again with an error message. Furthermore, user will need to scroll down to the code field in order to change it.

From user point of view, remote validation is the best choice, and in fact, this is seen everywhere, for example, when validating username existence before posting a registration data.

Cheers Jaime