<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="/rss.xsl"?><rss version="2.0"><channel><title>FluentValidation Discussions Rss Feed</title><link>http://www.codeplex.com/FluentValidation/Thread/List.aspx</link><description>FluentValidation Discussions Rss Description</description><item><title>New Post: Reuse rule set validator</title><link>http://fluentvalidation.codeplex.com/discussions/390446</link><description>&lt;div style="line-height: normal;"&gt;Did you figure out if this worked?&lt;br /&gt;
&lt;/div&gt;</description><author>colemike</author><pubDate>Fri, 17 May 2013 18:50:49 GMT</pubDate><guid isPermaLink="false">New Post: Reuse rule set validator 20130517065049P</guid></item><item><title>New Post: Validating Object Graphs</title><link>http://fluentvalidation.codeplex.com/discussions/443916</link><description>&lt;div style="line-height: normal;"&gt;It appears the StackOverflow is happening in the constructor while wiring up the validators instead of in the validators themselves.&lt;br /&gt;
&lt;br /&gt;
Is it possible to mark an entity as validated so it doesn't wire up the validator a second time? Then I could wrap all the validators in a When condition?&lt;br /&gt;
&lt;br /&gt;
Just some context: I'm generating my POCOs from Entity Framework and I'm also generating the Validation classes. In my case I can't really remove one of the calls from SetValidator. In a perfect world the validation would walk the entire object graph but not validate the same instance more than once.&lt;br /&gt;
&lt;/div&gt;</description><author>colemike</author><pubDate>Fri, 17 May 2013 14:41:11 GMT</pubDate><guid isPermaLink="false">New Post: Validating Object Graphs 20130517024111P</guid></item><item><title>New Post: Count invalid items in a model</title><link>http://fluentvalidation.codeplex.com/discussions/443799</link><description>&lt;div style="line-height: normal;"&gt;Hi&lt;br /&gt;
&lt;br /&gt;
This message isn't coming from FluentValidation, but rather from MVC itself. Before ASP.NET MVC invokes validation, it will first check to see if the property can be set to the value. If it can't, then this error is generated, so this is happening before the value even gets to FluentValidation.&lt;br /&gt;
&lt;br /&gt;
As far as I'm aware, this isn't something that can be overridden. Your best bet is to add some client-side logic to ensure that the value being entered isn't too long for the type of the property.&lt;br /&gt;
&lt;/div&gt;</description><author>JeremyS</author><pubDate>Fri, 17 May 2013 09:17:15 GMT</pubDate><guid isPermaLink="false">New Post: Count invalid items in a model 20130517091715A</guid></item><item><title>New Post: Validating Object Graphs</title><link>http://fluentvalidation.codeplex.com/discussions/443916</link><description>&lt;div style="line-height: normal;"&gt;Hi&lt;br /&gt;
&lt;br /&gt;
Yes, this will indeed cause a stackoverflow. Your best bet is either to remove one of the calls to SetValidator, and perform the validation manually when necessary or put one of the SetValidator calls inside a RuleSet and then explicitly choose when you want to execute it. &lt;br /&gt;
&lt;br /&gt;
Jeremy&lt;br /&gt;
&lt;/div&gt;</description><author>JeremyS</author><pubDate>Fri, 17 May 2013 07:53:02 GMT</pubDate><guid isPermaLink="false">New Post: Validating Object Graphs 20130517075302A</guid></item><item><title>New Post: Validating Object Graphs</title><link>http://fluentvalidation.codeplex.com/discussions/443916</link><description>&lt;div style="line-height: normal;"&gt;Update: I just tried this and got a StackOverflow exception like I expected. Is there a feature/setting that I'm missing that makes this work? Maybe by using the When method? It seems like a person would need to keep track of what has been validated and use the When method to not validate a 2nd time. However, I'm not sure how one would capture what has already been validated. It's almost as if a &amp;quot;When Validating&amp;quot; event needs to be available.&lt;br /&gt;
&lt;/div&gt;</description><author>colemike</author><pubDate>Thu, 16 May 2013 16:14:36 GMT</pubDate><guid isPermaLink="false">New Post: Validating Object Graphs 20130516041436P</guid></item><item><title>New Post: Idea for integration</title><link>http://fluentvalidation.codeplex.com/discussions/433299</link><description>&lt;div style="line-height: normal;"&gt;You could probably use T4 templates to generate your validation classes. Look at the Reverse Engineer Code First in Entity Framework Power Tools. It gives you the option to generate your model, mappings, and context from the DB. It also gives you the option to customize the T4 templates. I am actually using this to generate my models, mappings, and context - and I was able to copy a lot of the mapping functionality and translate it into FluentValidation syntax.&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;namespace &amp;lt;#= code.EscapeNamespace(efHost.Namespace) #&amp;gt;.Validation
{
    public partial class &amp;lt;#= efHost.EntityType.Name #&amp;gt;Validator : AbstractValidator&amp;lt;&amp;lt;#= efHost.EntityType.Name #&amp;gt;&amp;gt;
    {
        public &amp;lt;#= efHost.EntityType.Name #&amp;gt;Validator()
        {
&amp;lt;#
foreach (var prop in efHost.EntityType.Properties)
{
    var isKey = efHost.EntityType.KeyMembers.Contains(prop);

    if (!isKey)
    {
        var validationLines = new List&amp;lt;string&amp;gt;();
        var formattedPropertyName = System.Text.RegularExpressions.Regex.Replace(prop.Name, &amp;quot;((?&amp;lt;=[a-z])[A-Z]|[A-Z](?=[a-z]))&amp;quot;, &amp;quot; $1&amp;quot;).Trim();

        if (!prop.Nullable)
        {
            validationLines.Add(String.Format(&amp;quot;.NotNull().WithMessage(\&amp;quot;{0} cannot be empty.\&amp;quot;)&amp;quot;, formattedPropertyName));
        }

        var isString = prop.TypeUsage.ToString().Equals(&amp;quot;Edm.String&amp;quot;);

        if (!prop.Nullable &amp;amp;&amp;amp; isString)
        {
            validationLines.Add(String.Format(&amp;quot;.NotEmpty().WithMessage(\&amp;quot;{0} cannot be empty.\&amp;quot;)&amp;quot;, formattedPropertyName));
        }

        var maxLengthFacet = (Facet)prop.TypeUsage.Facets.SingleOrDefault(f =&amp;gt; f.Name == &amp;quot;MaxLength&amp;quot;);
        if (maxLengthFacet != null &amp;amp;&amp;amp; !maxLengthFacet.IsUnbounded)
        {
            validationLines.Add(string.Format(&amp;quot;.Length(0, {0}).WithMessage(\&amp;quot;{1} cannot be over {0} characters in length.\&amp;quot;)&amp;quot;, maxLengthFacet.Value, formattedPropertyName));
        }


        if (validationLines.Any())
        {
#&amp;gt;
            RuleFor(m =&amp;gt; m.&amp;lt;#= prop.Name #&amp;gt;)
                &amp;lt;#= string.Join(&amp;quot;\r\n                &amp;quot;, validationLines) #&amp;gt;;

&amp;lt;#
        }
    }
}
#&amp;gt;
        }
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;</description><author>colemike</author><pubDate>Thu, 16 May 2013 14:09:59 GMT</pubDate><guid isPermaLink="false">New Post: Idea for integration 20130516020959P</guid></item><item><title>New Post: Validating Object Graphs</title><link>http://fluentvalidation.codeplex.com/discussions/443916</link><description>&lt;div style="line-height: normal;"&gt;I see how you can re-use a validator for Complex Properties and Collections, but how will it work when the reference is bi-directional? Will the example below produce an infinite loop, and if so what is the best way to handle this?&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;public class Customer {
  public string Name { get; set; }
  public Address Address { get; set; }
}

public class Address {
  public string Line1 { get; set; }
  public string Line2 { get; set; }
  public string Town { get; set; }
  public string County { get; set; }
  public string Postcode { get; set; }
  public Customer Customer { get; set; }
}

public class AddressValidator : AbstractValidator&amp;lt;Address&amp;gt; {
  public AddressValidator() {
    RuleFor(address =&amp;gt; address.Postcode).NotNull();
    RuleFor(address =&amp;gt; address.Customer).SetValidator(new CustomerValidator());
    //etc
  }
}

public class CustomerValidator : AbstractValidator&amp;lt;Customer&amp;gt; {
  public CustomerValidator() {
    RuleFor(customer =&amp;gt; customer.Name).NotNull();
    RuleFor(customer =&amp;gt; customer.Address).SetValidator(new AddressValidator());
  }
} &lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;</description><author>colemike</author><pubDate>Thu, 16 May 2013 14:01:44 GMT</pubDate><guid isPermaLink="false">New Post: Validating Object Graphs 20130516020144P</guid></item><item><title>New Post: Count invalid items in a model</title><link>http://fluentvalidation.codeplex.com/discussions/443799</link><description>&lt;div style="line-height: normal;"&gt;Simple validation, loops over all the boxes and makes sure you enter something into at least one of them.  
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;            RuleFor(model =&amp;gt; model.AnyInvoicesEntered)
                .NotEmpty()
                .When(AtLeastOneValueEntered)
                .WithMessage(&amp;quot;No Invoices entered&amp;quot;);

        private bool AtLeastOneValueEntered(PaymentViewModel model)
        {
            int countOfInvocies = model.InvoiceViewModels.Count(x =&amp;gt; x.Number != null);

            if (countOfInvocies == 0)
            {
                return true;
            }
            return false;
        }&lt;/code&gt;&lt;/pre&gt;

Problem is if you enter an invalid number in the box, the validation does not count it as something being entered and throws the error saying there is nothing entered.  I need to get it to only show the one error.&lt;br /&gt;
&lt;br /&gt;
See the full code and screen shot with both errors:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;namespace WS.App.Internet.ViewModels
{
    [Validator(typeof(PaymentValidator))]
    public class PaymentViewModel
    {
        public string AnyInvoicesEntered { get; set; }

        [DisplayName(&amp;quot;Payment Method&amp;quot;)]
        public int? PaymentMethod { get; set; }

        [DisplayName(&amp;quot;Confirmation Email Address&amp;quot;)]
        [Email]
        public string ConformationEmailAddress { get; set; }

        [DisplayFormat(DataFormatString = &amp;quot;{0:F2}&amp;quot;, ApplyFormatInEditMode = true)]
        [DisplayName(&amp;quot;Sub-Total&amp;quot;)]
        public Decimal SubTotal { get; set; }

        [DisplayFormat(DataFormatString = &amp;quot;{0:F2}&amp;quot;, ApplyFormatInEditMode = true)]
        [DisplayName(&amp;quot;Tax&amp;quot;)]
        public Decimal TaxTotal { get; set; }

        [DisplayFormat(DataFormatString = &amp;quot;{0:F2}&amp;quot;, ApplyFormatInEditMode = true)]
        [DisplayName(&amp;quot;Total Payment&amp;quot;)]
        public Decimal? PaymentTotal { get; set; }

        public List&amp;lt;InvoiceViewModel&amp;gt; InvoiceViewModels { get; set; }
        public IEnumerable&amp;lt;SelectListItem&amp;gt; PaymentMethods { get; set; }
    }

    public class PaymentValidator : AbstractValidator&amp;lt;PaymentViewModel&amp;gt;
    {
        public PaymentValidator()
        {
            RuleFor(model =&amp;gt; model.ConformationEmailAddress)
                .NotEmpty()
                .When(model =&amp;gt; model.PaymentTotal &amp;gt; 0)
                .WithMessage(&amp;quot;Confirmation Email Address is required&amp;quot;);
            RuleFor(model =&amp;gt; model.PaymentMethod)
                .NotEmpty()
                .When(model =&amp;gt; model.PaymentTotal &amp;gt; 0)
                .WithMessage(&amp;quot;Payment Method is required&amp;quot;);
            RuleFor(model =&amp;gt; model.PaymentTotal)
                .NotEqual(0)
                .WithMessage(&amp;quot;Total Payment must be greater than $0.00&amp;quot;);
            RuleFor(model =&amp;gt; model.AnyInvoicesEntered)
                .NotEmpty()
                .When(AtLeastOneValueEntered)
                .WithMessage(&amp;quot;No Invoices entered&amp;quot;);
        }

        private bool AtLeastOneValueEntered(PaymentViewModel model)
        {
            int countOfInvocies = model.InvoiceViewModels.Count(x =&amp;gt; x.Number != null);

            if (countOfInvocies == 0)
            {
                return true;
            }
            return false;
        }
    }
}&lt;/code&gt;&lt;/pre&gt;

In case the image doesn't show, it is here:&lt;br /&gt;
&lt;br /&gt;
&lt;a href="https://www.dropbox.com/sh/4dzsm56os5dlkba/PWH18VZt11" rel="nofollow"&gt;https://www.dropbox.com/sh/4dzsm56os5dlkba/PWH18VZt11&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;img src="https://www.dropbox.com/sh/4dzsm56os5dlkba/PWH18VZt11#f:1.png" alt="Image" /&gt;&lt;br /&gt;
&lt;/div&gt;</description><author>pwensel</author><pubDate>Wed, 15 May 2013 19:35:16 GMT</pubDate><guid isPermaLink="false">New Post: Count invalid items in a model 20130515073516P</guid></item><item><title>New Post: Documentation: DisplayNameAttribute</title><link>http://fluentvalidation.codeplex.com/discussions/443491</link><description>&lt;div style="line-height: normal;"&gt;I've added this to the docs. Thanks for the suggestion.&lt;br /&gt;
&lt;/div&gt;</description><author>JeremyS</author><pubDate>Mon, 13 May 2013 16:01:28 GMT</pubDate><guid isPermaLink="false">New Post: Documentation: DisplayNameAttribute 20130513040128P</guid></item><item><title>New Post: Documentation: DisplayNameAttribute</title><link>http://fluentvalidation.codeplex.com/discussions/443491</link><description>&lt;div style="line-height: normal;"&gt;I accidentally discovered this trying to create a PropertyNameResolver, but FluentValidation already check for System.ComponentModel.DisplayNameAttribute in properties and use that in error messages.&lt;br /&gt;
&lt;br /&gt;
The documentation has no mention of this anywhere.&lt;br /&gt;
&lt;br /&gt;
I think this should be documented as one of the simplest ways of customizing the PropertyName in error messages.&lt;br /&gt;
&lt;/div&gt;</description><author>nvivo</author><pubDate>Mon, 13 May 2013 15:27:45 GMT</pubDate><guid isPermaLink="false">New Post: Documentation: DisplayNameAttribute 20130513032745P</guid></item><item><title>New Post: Best way to override default validator messages?</title><link>http://fluentvalidation.codeplex.com/discussions/443466</link><description>&lt;div style="line-height: normal;"&gt;Good idea - I'll get that changed.&lt;br /&gt;
&lt;/div&gt;</description><author>JeremyS</author><pubDate>Mon, 13 May 2013 13:34:28 GMT</pubDate><guid isPermaLink="false">New Post: Best way to override default validator messages? 20130513013428P</guid></item><item><title>New Post: Best way to override default validator messages?</title><link>http://fluentvalidation.codeplex.com/discussions/443466</link><description>&lt;div style="line-height: normal;"&gt;Thanks, that is exactly what I was looking for!&lt;br /&gt;
&lt;br /&gt;
I didn't think about localization. Maybe its a good idea to add a link to that page from the &amp;quot;Overriding the Default Error Message&amp;quot; page in the wiki saying &amp;quot;if you want to override all default messages, click here&amp;quot; or something like that.&lt;br /&gt;
&lt;/div&gt;</description><author>nvivo</author><pubDate>Mon, 13 May 2013 13:33:54 GMT</pubDate><guid isPermaLink="false">New Post: Best way to override default validator messages? 20130513013354P</guid></item><item><title>New Post: Best way to override default validator messages?</title><link>http://fluentvalidation.codeplex.com/discussions/443466</link><description>&lt;div style="line-height: normal;"&gt;Hi&lt;br /&gt;
&lt;br /&gt;
You can override FluentValidation's default error messages by replacing the default resource provider. See the section on &amp;quot;using a custom resource provider type&amp;quot; at the bottom of this page: &lt;a href="https://fluentvalidation.codeplex.com/wikipage?title=Localization&amp;amp;referringTitle=Documentation" rel="nofollow"&gt;https://fluentvalidation.codeplex.com/wikipage?title=Localization&amp;amp;referringTitle=Documentation&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Essentially, you need to create a resource file to include your new error messages and then tell FluentValidation to use this file by setting ValidatorOptions.ResourceProviderType to your resource provider type.&lt;br /&gt;
&lt;br /&gt;
Jeremy&lt;br /&gt;
&lt;/div&gt;</description><author>JeremyS</author><pubDate>Mon, 13 May 2013 13:28:13 GMT</pubDate><guid isPermaLink="false">New Post: Best way to override default validator messages? 20130513012813P</guid></item><item><title>New Post: Best way to override default validator messages?</title><link>http://fluentvalidation.codeplex.com/discussions/443466</link><description>&lt;div style="line-height: normal;"&gt;In a project I work, we have some custom messages for standard validators like &amp;quot;notempty&amp;quot; or &amp;quot;length&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Today, we change them with &amp;quot;WithMessage&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The problem is that we have a standard error message for &amp;quot;NotEmpty&amp;quot;, its just different then what comes with the package, so today all of our validation must have a WithMessage in all validations pointing to the same variable, which makes the code quite ugly.&lt;br /&gt;
&lt;br /&gt;
Is there any better way to handle this that is not change FluentValidation source and recompile ? The ideal thing would be to somehow say in my project &amp;quot;the default message for NotEmpty now is this: ...&amp;quot; and just use the .NotEmpty();&lt;br /&gt;
&lt;/div&gt;</description><author>nvivo</author><pubDate>Mon, 13 May 2013 13:18:06 GMT</pubDate><guid isPermaLink="false">New Post: Best way to override default validator messages? 20130513011806P</guid></item><item><title>New Post: How to associate a rule with more than one property</title><link>http://fluentvalidation.codeplex.com/discussions/442836</link><description>&lt;div style="line-height: normal;"&gt;Yes, please feel free to post your solution.&lt;br /&gt;
&lt;/div&gt;</description><author>JeremyS</author><pubDate>Fri, 10 May 2013 08:02:52 GMT</pubDate><guid isPermaLink="false">New Post: How to associate a rule with more than one property 20130510080252A</guid></item><item><title>New Post: How to Check the validation for the Combo box with Enum in mvc razor</title><link>http://fluentvalidation.codeplex.com/discussions/443132</link><description>&lt;div style="line-height: normal;"&gt;Hi Experts,&lt;br /&gt;
&lt;br /&gt;
In my Model there is a Property Genders as Selectlist that value i am assigning in selectedgenderid which is int.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href="mailto:@Html.DropDownListFor(modelItem" rel="nofollow"&gt;@Html.DropDownListFor(modelItem&lt;/a&gt; =&amp;gt; Model.SelectedGenderid, Model.Genders)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
then i need to check the validation for the SelectedGenderid .&lt;br /&gt;
&lt;br /&gt;
RuleFor(x =&amp;gt; x.SelectedGenderid).NotNull().WithLocalizedMessage(() =&amp;gt; ValidationMessage.TRAVELERTITLENOTNULL);&lt;br /&gt;
&lt;/div&gt;</description><author>Anubhava</author><pubDate>Thu, 09 May 2013 16:17:11 GMT</pubDate><guid isPermaLink="false">New Post: How to Check the validation for the Combo box with Enum in mvc razor 20130509041711P</guid></item><item><title>New Post: How to associate a rule with more than one property</title><link>http://fluentvalidation.codeplex.com/discussions/442836</link><description>&lt;div style="line-height: normal;"&gt;It's useful in frameworks like MVC that will highlight all the properties for you if you use IValidatableObject. I'm working on a solution for this on my end that uses a combination of Extension Methods and a ConditionalWeakTable to make it easy to add IValidatableObject support using your fluent framework. If your interested in what I come up with let me know and I can let you review it when I'm done.&lt;br /&gt;
&lt;/div&gt;</description><author>RyanVice</author><pubDate>Tue, 07 May 2013 16:52:46 GMT</pubDate><guid isPermaLink="false">New Post: How to associate a rule with more than one property 20130507045246P</guid></item><item><title>New Post: How to associate a rule with more than one property</title><link>http://fluentvalidation.codeplex.com/discussions/442836</link><description>&lt;div style="line-height: normal;"&gt;Ah, I see what you mean.&lt;br /&gt;
&lt;br /&gt;
No, this isn't supported with FluentValidation I'm afraid - you always have to choose a single property to be the target of your RuleFor declaration, and this is the property that will end up with the error associated with it. With something like Password/ConfirmPassword, I'd tend to associate the error with the Password property, but I appreciate this may not be ideal in all scenarios. I'll have a think about how practical this would be to add in a future version.&lt;br /&gt;
&lt;br /&gt;
Jeremy&lt;br /&gt;
&lt;/div&gt;</description><author>JeremyS</author><pubDate>Tue, 07 May 2013 16:28:35 GMT</pubDate><guid isPermaLink="false">New Post: How to associate a rule with more than one property 20130507042835P</guid></item><item><title>New Post: How to associate a rule with more than one property</title><link>http://fluentvalidation.codeplex.com/discussions/442836</link><description>&lt;div style="line-height: normal;"&gt;Wow, thanks for the quick reply!&lt;br /&gt;
&lt;br /&gt;
That's not the scenario that I'm trying to solve. My scenario is where you have two properties that are not valid. For example, say you need to validate that Password and ConfirmPassword properties are equal. In the IValidatableObject model you would return a ValidationResult like below&lt;br /&gt;
&lt;br /&gt;
 return new ValidationResult(&amp;quot;Password and Confirm Password must match.&amp;quot;, new List&amp;lt;string&amp;gt;{ &amp;quot;UserName&amp;quot;, &amp;quot;Password&amp;quot;});&lt;br /&gt;
&lt;br /&gt;
this will allow the client (like MVC) to be able to know which properties are involved in the complex validation failure.&lt;br /&gt;
&lt;br /&gt;
Is this supported or possible?&lt;br /&gt;
&lt;br /&gt;
-Ryan&lt;br /&gt;
&lt;/div&gt;</description><author>RyanVice</author><pubDate>Tue, 07 May 2013 16:24:55 GMT</pubDate><guid isPermaLink="false">New Post: How to associate a rule with more than one property 20130507042455P</guid></item><item><title>New Post: How to associate a rule with more than one property</title><link>http://fluentvalidation.codeplex.com/discussions/442836</link><description>&lt;div style="line-height: normal;"&gt;Hi Ryan&lt;br /&gt;
&lt;br /&gt;
Many of the built-in validators in FluentValidation can work with multiple properties. For example, if you wanted to check that Property1 is not equal to Property2 you could do this:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;RuleFor(x =&amp;gt; x.Property1).NotEqual(x =&amp;gt; x.Property2)&lt;/code&gt;&lt;/pre&gt;

Here the rule uses multiple properties, but the error message is associated with Property1.&lt;br /&gt;
&lt;br /&gt;
Similarily, you can use the Must validator to access all of the properties in the object being validated:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;public CustomerValidator() {
  RuleFor(x =&amp;gt; x.UserName).Must(NotEqualFirstAndLastName)
}

private bool NotEqualFirstAndLastName(Customer customer, string username) {
  return username != customer.Surname &amp;amp;&amp;amp; username != customer.LastName;
} &lt;/code&gt;&lt;/pre&gt;

Jeremy&lt;br /&gt;
&lt;/div&gt;</description><author>JeremyS</author><pubDate>Tue, 07 May 2013 16:17:34 GMT</pubDate><guid isPermaLink="false">New Post: How to associate a rule with more than one property 20130507041734P</guid></item></channel></rss>