You can see in the pic above, that there are 3 error messages. The top error message is what AddFormError is used for. And the bottom two error messages, which are referring to particular input fields, is what you would use AddError for.
Example:
If you want to validate against an input field, you would not use:
ModelState.AddFormError("Incorrect username");
Instead, you would use something like this:
ModelState.AddFormError("We could not sign you in. Please verify your details, and try again.");
ModelState.AddError("passwordField", "You entered an incorrect password.");
ModelState.AddError("usernameField", "You entered an incorrect username.");
Basically, AddFormError is more of a generalised summary of the error details, usually displayed at the top of the form. And AddError is used to include errors for each individual input field contained within the form.
I hope this helped explain the difference between AddFormError and AddError.
Jason Pezzimenti
Please "Mark As Answer" if the post helped you.
It may be a bug. But first, are you using your Private and Public Keys in the right places? I know that the WebMatrix comments provided in the StarterSite template are not correct (unless they have been updated in recent versions.) - Try switching them around,
like:
if (!ReCaptcha.Validate("PRIVATE_KEY"))
{
ModelState.AddError("recaptcha", "Oh, no! You entered an incorrect Captcha. Please, try again.");
}
And then down below, in your HTML (where you want it displayed):
@ReCaptcha.GetHtml("PUBLIC_KEY", "clean")
If this isn't working - can you please provide more details, i.e. the html you have for the captcha?
Edit:
Also, have you tried reverting back to your original code (the one that didn't work) and wrapping it in a Try/Catch block, and outputting the exception details?
@{
var excMsg = "";
try
{
if (!ReCaptcha.Validate("---------------KEY---------------"))
{
ModelState.AddError("recaptcha", "Captcha response was not correct");
}
}
catch(Exception exception)
{
excMsg = exception.Message + " " + exception.StackTrace;
}
<div>
@excMsg
</div>
}
Jason Pezzimenti
Please "Mark As Answer" if the post helped you.
Yes and I think you should also ckeck this CAPTCHA issue in Register.cshtml in the
starter site template of the Web Mtarix 2
It seems as though WebMatrix doesn't feel up to working tonight. After several attempts to Create a StarterSite - it had stopped responding. I will get back to you in the morning.
satya_87
Member
9 Points
34 Posts
Problem in CAPTCHA verification in web matrix 2
Aug 18, 2012 03:55 AM|LINK
Hello,
I have created a site using starter site template in web matrix 2.
In the Register.cshtml page there is this code:
if (!ReCaptcha.Validate("---------------KEY---------------")) { ModelState.AddError("recaptcha", "Captcha response was not correct"); }However, even when ReCaptcha.Validate is false and ModelState.AddError is run it passes Validation.IsValid.
But if i replace the code by
if (!ReCaptcha.Validate("-------------key-----------------")) { ModelState.AddFormError("Captcha response was not correct"); }it works perfectly.
Why this problem is occouring?
What is the difference between ModelState.AddFormError and ModelState.AddError?
JTS-V
Member
29 Points
19 Posts
Re: Problem in CAPTCHA verification in web matrix 2
Aug 18, 2012 04:40 AM|LINK
the pic
You can see in the pic above, that there are 3 error messages. The top error message is what AddFormError is used for. And the bottom two error messages, which are referring to particular input fields, is what you would use AddError for.
Example:
If you want to validate against an input field, you would not use:
ModelState.AddFormError("Incorrect username");Instead, you would use something like this:
ModelState.AddFormError("We could not sign you in. Please verify your details, and try again."); ModelState.AddError("passwordField", "You entered an incorrect password."); ModelState.AddError("usernameField", "You entered an incorrect username.");Basically, AddFormError is more of a generalised summary of the error details, usually displayed at the top of the form. And AddError is used to include errors for each individual input field contained within the form.
I hope this helped explain the difference between AddFormError and AddError.
Please "Mark As Answer" if the post helped you.
satya_87
Member
9 Points
34 Posts
Re: Problem in CAPTCHA verification in web matrix 2
Aug 18, 2012 04:57 AM|LINK
But why this is happening in Starter Site Template in web matrix 2.
Is this a bug?
JTS-V
Member
29 Points
19 Posts
Re: Problem in CAPTCHA verification in web matrix 2
Aug 18, 2012 05:05 AM|LINK
It may be a bug. But first, are you using your Private and Public Keys in the right places? I know that the WebMatrix comments provided in the StarterSite template are not correct (unless they have been updated in recent versions.) - Try switching them around, like:
if (!ReCaptcha.Validate("PRIVATE_KEY")) { ModelState.AddError("recaptcha", "Oh, no! You entered an incorrect Captcha. Please, try again."); }And then down below, in your HTML (where you want it displayed):
@ReCaptcha.GetHtml("PUBLIC_KEY", "clean")If this isn't working - can you please provide more details, i.e. the html you have for the captcha?
Edit:
Also, have you tried reverting back to your original code (the one that didn't work) and wrapping it in a Try/Catch block, and outputting the exception details?
@{ var excMsg = ""; try { if (!ReCaptcha.Validate("---------------KEY---------------")) { ModelState.AddError("recaptcha", "Captcha response was not correct"); } } catch(Exception exception) { excMsg = exception.Message + " " + exception.StackTrace; } <div> @excMsg </div> }Please "Mark As Answer" if the post helped you.
satya_87
Member
9 Points
34 Posts
Re: Problem in CAPTCHA verification in web matrix 2
Aug 18, 2012 05:44 AM|LINK
I have placeed the Private & Public key at their correct poisitions.
As i said before the below code (which is the replacement of original code)works perfectly,.
I have not tried ttry catch block though.
if (!ReCaptcha.Validate("-------------key-----------------")) { ModelState.AddFormError("Captcha response was not correct"); }GmGregori
Contributor
5448 Points
736 Posts
Re: Problem in CAPTCHA verification in web matrix 2
Aug 18, 2012 09:48 AM|LINK
Have you followed all the steps given in the "Preventing Automated Programs from Joining Your Website" chapter of the Adding Security and Membership to an ASP.NET Web Pages (Razor) Site tutorial?
satya_87
Member
9 Points
34 Posts
Re: Problem in CAPTCHA verification in web matrix 2
Aug 18, 2012 10:52 AM|LINK
Yes.
JTS-V
Member
29 Points
19 Posts
Re: Problem in CAPTCHA verification in web matrix 2
Aug 18, 2012 12:06 PM|LINK
Have you added the ASP.NET Web Helpers Library to your website as described in Installing Helpers in an ASP.NET Web Pages Site?
Also, this may be of help: http://go.microsoft.com/fwlink/?LinkId=251967
Please "Mark As Answer" if the post helped you.
satya_87
Member
9 Points
34 Posts
Re: Problem in CAPTCHA verification in web matrix 2
Aug 18, 2012 01:30 PM|LINK
Yes and I think you should also ckeck this CAPTCHA issue in Register.cshtml in the starter site template of the Web Mtarix 2
JTS-V
Member
29 Points
19 Posts
Re: Problem in CAPTCHA verification in web matrix 2
Aug 18, 2012 02:34 PM|LINK
It seems as though WebMatrix doesn't feel up to working tonight. After several attempts to Create a StarterSite - it had stopped responding. I will get back to you in the morning.
https://skydrive.live.com/redir?resid=E9A2E4D64D4F2CEA!140&authkey=!AO7dxqfumZg5kl0
Please "Mark As Answer" if the post helped you.