asp.net mvc - Custom Regular Expression Validation in MVC3 -
i on way implement custom regular expression validator 1 of mvc3 project.
i have been able perform validation correctly using custom atrribute.
the view model property decorated below custom attribute
[regularexpressionif("dependantproperty","targetvalue","pattern","errormessage")]
but requirement , need match property1 values regular expressions based on property2 value. ex. property 2 dropdown , has values 1,2,3 . if user selects 1, regular expresion pattern {regx1}, 2 {regx2} that.
i tried modify attribute passing variables contain these values. on build .net insists should "constant" passed. able hard code value in attribute, in no way can pass parameter below
[regularexpressionif("dependantproperty","targetvalue",patterndictionary,"errormessage")]
if me out in great..
you'll need use reflection value of property @ runtime.
protected override validationresult isvalid( object value, validationcontext context) { // dependantproperty string name of dependant property propertyinfo property = context.objecttype.getproperty(this.dependantproperty); object dependant = property.getvalue(context.objectinstance, null); // dependant }
Comments
Post a Comment