c# - Using CustomValidationAttribute with Entity Framework -
i want implement conditional validation logic on 1 of entities. basically, want validate field (using regular expression) if field in entity has value.
i tried adding customvalidationattribute
on entity doesn't anything
[metadatatype(typeof(templateactionmetadata))] [customvalidation(typeof(templateaction), "validateactionvalue")] public partial class templateaction { public static validationresult validateactionvalue(string pvalue, validationcontext vcontext) { //this never gets called return validationresult.success; } }
i tried adding attribute on field
public partial class templateactionmetadata { [customvalidation(typeof(templateaction), "validateactionvalue")] public string actionvalue { get; set; } }
now call validateactionvalue
method validationcontext
parameter null, have no way of getting value of other field need use implement validation logic.
in msdn documentation, can see customvalidationattribute class has readonly requiresvalidationcontext property.
this property overridden base class validationattribute.
since don't have context, suppose property return false, design.
so can't context attribute. may have create custom attribute fulfill needs
i found microsoft guideline you: https://msdn.microsoft.com/en-us/library/cc488527.aspx
Comments
Post a Comment