0

If I am not mistaken, only primitive types can be passed as parameters type to a custom made ValidationAttribute (string for example):

public class AttributeNameValidation : ValidationAttribute
{
    public string AttributeName { get; set; }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (MyProperty == value.ToString())
        {
            return new ValidationResult($"Attribute with the name '{value.ToString()}' already exist.", new[] { validationContext.MemberName });
        }
        return null;
    }
}

and using it like this:

[Required]
[AttributeNameValidation(AttributeName = "MyAttribute")]
public string Name { get; set; }

But what if we want to pass a non-primitive type to the custom made ValidationAttribute? For example how I can pass an instance of this object into AttributeNameValidation? (While keeping the outer property non static)

public class Attribute
{
    public string Name { get; set; }
    public string Type { get; set; }
}

public Attribute myAttrib = new Attribute {Name = "Price", Type = "int"};

supposing that a property of type Attribute is created in the AttributeNameValidation with the name: Attrib (public Attribute Attrib { get; set; }), the IntelliSense complains that:

[Required] 
[AttributeNameValidation(Attrib = myAttrib)]
public string Name { get; set; }

enter image description here

wiki
  • 1,877
  • 2
  • 31
  • 47
  • If you would like to use FluentValidation in Blazor instead of only the DataAnnotations, my library will do that for you. You might be able to achieve what you need using that - https://github.com/mrpmorris/blazor-validation – Peter Morris Jun 14 '20 at 14:03

1 Answers1

0

It is not Blazor issue, but the restriction of Attributes.

So you need multiple properties in the attribute:

public class AttributeNameValidation : ValidationAttribute
{
    public string AttributeName { get; set; }
    public string AttriuteType {get;set;}
}

And in case you need them to be always set simultaneously just use constructor:

public class AttributeNameValidation : ValidationAttribute
{
    public string AttributeName { get;}
    public string AttriuteType {get;}

    AttributeNameValidation(string attrName, string attrType)
    {
        AttributeName = attrName;
        AttrType = attrType;
    }
}
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
  • Using this way will result in breaking the passing object into its properties; which if the object has many properties will be very ugly and cumbersome. – wiki Jun 14 '20 at 13:01
  • But you still need to somehow create the object, and do it "statically", so you still have same problem, I guess. You may also [use DI capabilities](https://andrewlock.net/injecting-services-into-validationattributes-in-asp-net-core/) to get registered singleton, for example. – Pavel Voronin Jun 14 '20 at 13:11
  • Or you can define a static dictionary with the keys and objects as values, and provide attribute with the corresponding key of the object you need. – Pavel Voronin Jun 14 '20 at 13:43