Tech-Today
validation-summary-errors div showing even if there is no error binded to a property
I'm working on a change password form when I encountered this strange behavior from mvc3. I created a simple form and change password model with 3 fields (old, new, confirm password). Then I extended ValidationAttribute to validate the password's length. ValidatePasswordLengthAttribute
public class ValidatePasswordLengthAttribute : ValidationAttribute
{
private readonly int _minCharacters = Membership.Provider.MinRequiredPasswordLength;
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, name, _minCharacters);
}
public override bool IsValid(object value)
{
var valueAsString = value as string;
return (valueAsString != null && valueAsString.Length >= _minCharacters);
}
}
Then on my form, I'm showing the error as: Field errors beside the field and page errors below. For the readers information field and page error can be added by:
//Field Error
ModelState.AddModelError("Password", "Field Error(Password)");
//Page Error
ModelState.AddModelError(string.Empty, "Page Error");
Note that I've styled my validation-summary-errors div with red background. On form submit, I'm surprised to see a red div but with no content, a bug? So my solution is to simply check if there are visible li element under validation-summary-errors. Because after I submit it's css property display=none. Solution:
<script type="text/javascript">
//work around to the validation summary bug
$(document).ready(function () {
if ($(".validation-summary-errors li:visible").length == 0) {
$(".validation-summary-errors").hide();
}
});
</script>
-
How To Create A Custom Bean Validation In Javaee6
Bean validation has been mentioned on many articles online but I found few that explain the actual class that implements the constraint validator. For example: the bean validation article from oracle javaee6 documentation: http://docs.oracle.com/javaee/6/tutorial/doc/gkfgx.html....
-
How To Setup Your Datasource In Jboss7.x
Below are sample configurations you can use to setup postgresq, mysql data source in your jboss7.x. Where to add: 1.) If you added Jboss Tools plugin to eclipse, in server view expand Jboss Server->Filesets->Configuration->standalone.xml 2.)...
-
How To Change Jquery's Dialog Button Label At Run Time
Recently I've used a 3rd party jquery library that pops up a jquery dialog with form content. However if has a default button Save and Cancel, which most of the time is ok, but sometimes you have need to localized or change the label depending on...
-
Mvc3 Ajax Validation Doesn't Work On A Jquery Served Form
To make automatic validation using data annotation on a model on mvc3 c# we need: 1.) A model with data annotation (ex. required attribute). 2.) Form with jquery declaration (jquery1.7, validate, validate unobtrusive, and unobtrusive-ajax. All are available...
-
How To Upload A File In An Mvc3 C# Ajax Form
It's easy to upload file in a synchronous form, where you just post the file and read a HttpPostedFile variable in the server side: In plain html form, take note of the form enctype property: //the view (index.cshtml) @using (Html.BeginForm("Upload",...
Tech-Today