0

please help me to solve a simple issue.

I use MVC4 + bootstrap validation.

I have a form [Login, Password] auth. When user presses submit button ajax form sends its input data into the server and than it checks if user is authenticated etc. In case of server error (password or username are wrong) server returns json object "error=message".

After submitting an ajax form it calls the js function onSuccess where I check if the return data is error or not.

So, here I want to show the message to user with the server-side data (in this example, that his mail or password were wrong) somehow using bootstrap, e.g. in the input email field. How can I do that? Moreover how can I add custom messages to the form after submitting it and getting messages from server?

Thank you.

cvrebert
  • 9,075
  • 2
  • 38
  • 49
DolceVita
  • 2,090
  • 1
  • 23
  • 35

1 Answers1

0

Generally we follow the standard that the field for username & password should just be checked for other validations like length/required/character type etc, not to show messages like "Incorrect Username or password" or other messages.

Actually these are form specific messages not field specific. So to show form messages follow below steps:

Add validation summary html helper (at preferred location in your view), with overload function to show only non-property errors (razor), (this will create, by default unordered list structure) with each error message in list item:

@Html.ValidationSummary(true)

In your controller add the custom messages to the ModelState Error messages as below (c#):

ModelState.AddModelError("", "The user name or password provided is incorrect.");

This will show you the error messages as unordered list in your html, not in your field/property specific validation control.

You can create custom Validation Summary html helper to render html according to your needs.

G J
  • 477
  • 9
  • 23
  • hm... but how can I use ModelState in Ajax.Form? I mean, my Ajax.Form call a OnLogin function specified in OnSuccess property. So After that I want to make bootstrap show that user was not authorized. – DolceVita Oct 17 '14 at 08:39
  • Refer link below for ajax call: http://stackoverflow.com/questions/13093186/displaying-model-state-errors-after-ajax-call-on-razor-views – G J Oct 17 '14 at 11:32
  • This link me to use standart jquery validation engine ($.validate() method), but how can I initiate bootstrap validator and add custom error with message from server after submiting the form? – DolceVita Oct 17 '14 at 20:32