Posted
about 11 years
ago
by
dmontooth
I am using a resource file for validation messages and need to replace a token in this message.
RuleFor(x => x.Date).NotNull().GreaterThanOrEqualTo(DateTime.UtcNow).WithLocalizedName( () => Resx.ValidationText );
In this case the resx value
... [More]
will have a token, {{Date}}. How can I replace this using a localized message? I know that the default validator will do this, however, there is a requirement that the message not say "greater than or equal to".
I've tried a custom PropertyValidator as such:
public class DateValidator : PropertyValidator
{
public DateValidator() : base(Resx.ValidationText) {}
protected override bool IsValid(PropertyValidatorContext context)
{
var val = (DateTime) context.PropertyValue;
if(val != null && val >= DateTime.UtcNow){ return true; }
context.MessageFormatter.AppendArgument("{Date}", DateTime.Now);
return false;
}
}
This works as expected, however, the validator gets cached, so when I switch between languages, I get the previous validation message until that cache is cleared.
Any thoughts on how to accomplish this?
[Less]
|
Posted
about 11 years
ago
by
pgaule
I have a "complex" view model that has two other view models as properties:
[FluentValidation.Attributes.Validator(typeof(ApplicationInformationViewModelValidator))]
public class ApplicationInformationViewModel
{
public string
... [More]
SomeField { get; set; }
public PersonViewModel Person { get; set; }
public AddressViewModel Address { get; set; }
public ApplicationInformationViewModel()
{
this.Address = new AddressViewModel();
this.Person = new PersonViewModel();
}
}
[FluentValidation.Attributes.Validator(typeof(PersonViewModelValidator))]
public class PersonViewModel
{
[Display(Name="Prefix")]
public string Prefix { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Suffix")]
public string Suffix { get; set; }
}
[FluentValidation.Attributes.Validator(typeof(AddressViewModelValidator))]
public class AddressViewModel
{
[Display(Name="Street 1")]
public string Street1 { get; set; }
[Display(Name="Street2")]
public string Street2 { get; set; }
[Display(Name = "City")]
public string City { get; set; }
[Display(Name = "State")]
public string State { get; set; }
[Display(Name = "Zip")]
public string Zip { get; set; }
}
The validators for these view models:
internal class ApplicationInformationViewModelValidator : AbstractValidator<ApplicationInformationViewModel>
{
public ApplicationInformationViewModelValidator()
{
RuleFor(x => x.SomeField).NotEmpty();
RuleFor(x => x.Address).NotEmpty().WithMessage("Address Is Required");
RuleFor(x => x.Person).NotEmpty().WithMessage("Person Is Required");
}
}
internal class PersonViewModelValidator : AbstractValidator<PersonViewModel>
{
public PersonViewModelValidator()
{
RuleFor(x => x.Prefix).Length(1, 10);
RuleFor(x => x.FirstName).NotEmpty().Length(1, 50);
RuleFor(x => x.LastName).NotEmpty().Length(1, 50);
RuleFor(x => x.Suffix).Length(1, 10);
}
}
internal class AddressViewModelValidator : AbstractValidator<AddressViewModel>
{
public AddressViewModelValidator()
{
RuleFor(x => x.Street1).NotEmpty().Length(1, 100);
RuleFor(x => x.Street2).Length(1, 100);
RuleFor(x => x.City).NotEmpty().Length(1, 50);
RuleFor(x => x.State).NotEmpty();
RuleFor(x => x.Zip).NotEmpty().Length(5, 10).Matches(@"^\d{5}(-\d{4}){0,1}$");
}
}
This works fine under normal circumstances. However, if the form/post data is manipulated and the form elements for the Person and Address are removed from the HTTP request, then the validation rules for Person and Address are not run. If I debug the request, the ApplicationInformationViewModel data looks like the following when it comes into my controller action:
SomeField : "text"
Address : instantiated, but empty:
Address.Street1 : null
Address.Street2 : null
Address.City : null
Address.State : null
Address.Zip : null
Person: instantiated, but empty:
Person.Prefix : null
Person.FirstName : null
Person.LastName : null
Person.Suffix : null
In spite of Address and Person not having valid values (all null), the Model State is reported as valid. So, the rules defined in PersonViewModelValidator and AddressViewModelValidator are not running even though I have applied the Validator attribute to the respective classes. Additionally, the Address "NotEmpty" and Person "NotEmpty" rules defined in the top-level ApplicationInformationViewModel do not catch this because the default constructor has instantiated Address and Person (although they are blank).
I realize this is an edge case, but I'm just wondering if there is some way to catch when this happens...
[Less]
|
Posted
about 11 years
ago
by
blake05
I have a string property that I use as an id. It's valid if it's null or between 8 and 16 characters. Is there a way to do this without using a Must?
|
Posted
about 11 years
ago
by
JeremyS
I've pushed a new build out to nuget.
|
Posted
about 11 years
ago
by
Jeremy Skinner
Update changelog
|
Posted
about 11 years
ago
by
Jeremy Skinner
Better webapi tests
|
Posted
about 11 years
ago
by
Jeremy Skinner
Update test projects
|
Posted
about 11 years
ago
by
Jeremy Skinner
Update package dependencies
|
Posted
about 11 years
ago
by
Jeremy Skinner
Consolidate build numbers
|
Posted
about 11 years
ago
by
shapper
Hello,
Asp.net MVC 5.2 was just released.
Will FV have a version for Mvc 5.
Thank you,
Miguel
|