I have a strange problem with my published asp.net MVC4 Solution. Until now, everything goes well about validation. Then things go wrong only on my published IISTEST server (locally through VS2012 no problem, even 'start without debugging' so bundles are active).
What's wrong? I noticed 2 or 3 things but let's talk about the first one.
I have some client side validation on different textboxes, options, ... One of them does not manage validation anymore on my published server. To check that, I navigate to the page, press F12, click on the textbox and copy the html.
Here is the local version (running through VS2012):
<input name="EmergencyReason" class="input-validation-error" id="EmergencyReason" type="text" data-val="true" hideRow="true" data-val-requiredifloadingtoday="Le champ {0} n'est pas valide." value=""/>
As you can see, there is some validation.
Here is the published version:
<input name="EmergencyReason" id="EmergencyReason" type="text" jQuery17205969379292482464="55" hiderow="true"/>
No more validation. But some of my textboxes have validation, some not.
I noticed everytime I have a textbox which validation does not work anymore I have this: jQuery17205969379292482464 in place of input-validation-error... I have bundling on this solution for script and css. Maybe it can help you to understand.
Any idea? I'm on this problem since half a day so any help is greatly appreciated.
Thanks.
UPDATE ------------------------
After doing some tests locally and manipulating the F12 / 'browser mode' >> IE7 I discover that the jQuery17205969379292482464 appears locally and on server so this is not related to my problem. One thing for sure: some basic client validation like [Required] attributes works. Validation who don't work anymore is as follow:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class RequiredIfLoadingTodayAttribute : ValidationAttribute, IClientValidatable
{
public RequiredIfLoadingTodayAttribute() { }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
///////////////
// IsLoading //
///////////////
var propertyIsLoadingInfo = validationContext.ObjectType.GetProperty("IsLoading");
var propertyIsLoadingValue = propertyIsLoadingInfo.GetValue(validationContext.ObjectInstance, null);
//////////
// Date //
//////////
var propertyDateInfo = validationContext.ObjectType.GetProperty("Date");
var propertyDateValue = propertyDateInfo.GetValue(validationContext.ObjectInstance, null);
////////////////
// Validation //
////////////////
Boolean isLoading = Convert.ToBoolean(propertyIsLoadingValue);
DateTime dateLoading = Convert.ToDateTime(propertyDateValue);
DateTime dateToday = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy"));
// Validation uniquement pour le chargement
if (isLoading)
{
// Si la date de chargement = date du jour et on a pas de raison indiquée -> déclencher une erreur de validation
if ((DateTime.Compare(dateLoading, dateToday)==0) && (value==null))
{
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
}
return ValidationResult.Success;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule {
ErrorMessage = this.ErrorMessageString,
ValidationType = "requiredifloadingtoday"
};
yield return rule;
}
}
And the associated js file:
$.validator.unobtrusive.adapters.add(
'requiredifloadingtoday', '', function (options) {
options.rules['requiredifloadingtoday'] = options.params;
options.message['requiredifloadingtoday'] = options.message;
});
$.validator.addMethod('requiredifloadingtoday', function (value, element, params) {
// Cette validation côté client n'est utilisée que sur le tab chargement
// pour s'assurer qu'une raison est indiquée dans le cas ou la date
// de chargement est le jour même.
return (($Date.val() == DateToday) && (value != ''))
}, '');
And the viewModel:
[RequiredIfLoadingToday()]
public string EmergencyReason { get; set; }
And the view:
<script type="text/javascript" src="@Url.Content("~/Scripts/Loading/When.js")"></script>
<span class="editor-field">
@Html.TextBoxFor(m => m.EmergencyReason, Model.Date != DateTime.Today ? new {hideRow="true"} : null)
</span>
...
I don't know where is the problem on this a little 'more advanced' validation. Ajax problem?