c# - How to validate textbox based on checkbox value -
iam trying validate textbox based on checkbox value. please view model class , isvalid override method.
public class product { //below property value(haveexperiance) [mustbeproductentered(haveexperiance)] public string productname { get; set; } public bool haveexperiance { get; set; } } public class mustbetrueattribute : validationattribute { //here need value of haveexperiance property //i passed [mustbeproductentered(haveexperiance)] in product class above. public override bool isvalid(object value) { return value bool && (bool)value; } }
you can see above in productname
property in product
class iam trying pass haveexperiance
class property value, if checked user must have fill productname
textbox.
so orignal question how can validate productname
textbox based on haveexperiance
value, in advance.
edit:
using system; using system.collections.generic; using system.componentmodel.dataannotations; using system.data.entity; using system.linq; using system.reflection; using system.web; using system.web.mvc; namespace mvc.affiliates.models { public class myproducts { [key] [required(errormessage = "please insert product id.")] public string productid { get; set; } [requiredif("haveexperiance")] public string productname { get; set; } public bool haveexperiance { get; set; } public list<myproducts> prolist { get; set; } } public class requiredifattribute : validationattribute { private requiredattribute _innerattribute = new requiredattribute(); public string property { get; set; } public object value { get; set; } public requiredifattribute(string typeproperty) { property = typeproperty; } public requiredifattribute(string typeproperty, object value) { property = typeproperty; value = value; } public override bool isvalid(object value) { return _innerattribute.isvalid(value); } } public class requiredifvalidator : dataannotationsmodelvalidator<requiredifattribute> { public requiredifvalidator(modelmetadata metadata, controllercontext context, requiredifattribute attribute) : base(metadata, context, attribute) { } public override ienumerable<modelclientvalidationrule> getclientvalidationrules() { return base.getclientvalidationrules(); } public override ienumerable<modelvalidationresult> validate(object container) { propertyinfo field = metadata.containertype.getproperty(attribute.property); if (field != null) { var value = field.getvalue(container, null); if ((value != null && attribute.value == null) || (value != null && value.equals(attribute.value))) { if (!attribute.isvalid(metadata.model)) { yield return new modelvalidationresult { message = errormessage }; } } } } }
controller
public class homecontroller : controller { // // get: /home/ mvcdbcontext _db = new mvcdbcontext(); public actionresult index() { return view(); } [httppost] public actionresult index(myproducts model) { string productid = model.productid; string productname = model.productname; //bool remember = model.haveexperiance; return view(); } }
view
@model mvc.affiliates.models.myproducts @{ viewbag.title = "index"; layout = "~/views/shared/_layout.cshtml"; } <h2>details</h2> <br /> <div style="height:200px; width:100%"> @using (html.beginform("index","home", formmethod.post)) { <h2>details</h2> @html.labelfor(model => model.productid) @html.textboxfor(model => model.productid) @html.labelfor(model => model.productname) @html.editorfor(model => model.productname) @html.validationmessagefor(model => model.productname, "*") @html.checkboxfor(model => model.haveexperiance) @html.validationmessagefor(model => model.haveexperiance, "*") <input type="submit" value="submit" /> } </div>
so far have tried above code, needed when click on checkbox should start validate productname textbox, if uncheck not. iam missing little thing in above code, please , rectify me.
this complete example of how create custom validation attribute based on attribute:
public class requiredifattribute : validationattribute { private requiredattribute _innerattribute = new requiredattribute(); public string property { get; set; } public object value { get; set; } public requiredifattribute(string typeproperty) { property = typeproperty; } public requiredifattribute(string typeproperty, object value) { property = typeproperty; value = value; } public override bool isvalid(object value) { return _innerattribute.isvalid(value); } } public class requiredifvalidator : dataannotationsmodelvalidator<requiredifattribute> { public requiredifvalidator(modelmetadata metadata, controllercontext context, requiredifattribute attribute) : base(metadata, context, attribute) { } public override ienumerable<modelclientvalidationrule> getclientvalidationrules() { return base.getclientvalidationrules(); } public override ienumerable<modelvalidationresult> validate(object container) { propertyinfo field = metadata.containertype.getproperty(attribute.property); if (field != null) { var value = field.getvalue(container, null); if ((value != null && attribute.value == null) || (value != null && value.equals(attribute.value))) { if (!attribute.isvalid(metadata.model)) { yield return new modelvalidationresult { message = errormessage }; } } } } }
in global.asax
file in application_start
add part:
dataannotationsmodelvalidatorprovider.registeradapter(typeof(requiredifattribute), typeof(requiredifvalidator));
this part needed registate requiredifvalidator
otherwise mvc use requiredifattribute
ignoring requiredifvalidator
.
if want validate productname
if haveexperiance
have value:
[requiredif("haveexperiance")] public string productname { get; set; }
if want validate productname
if haveexperiance
false:
[requiredif("haveexperiance", false)] public string productname { get; set; }
if want validate productname
if haveexperiance
true:
[requiredif("haveexperiance", true)] public string productname { get; set; }
in requiredifattribute
class created requiredattribute
object validate value passed isvalid
method.
property
field used save name of property activate validation. used in class requiredifvalidator
field current value using reflection (field.getvalue(container, null)
).
when call validation in code (for example when if(tryvalidatemodel(model))
) first call requiredifvalidator
class call requiredifattribute
(through attribute.isvalid(metadata.model)
) class if conditions valid ((value != null && attribute.value == null) || (value != null && value.equals(attribute.value))
).
one last thing. because requiredifattribute
inherits validationattribute
can use error messages in same way of other validation attribute.
[requiredif("haveexperiance", true, errormessage = "the error message")] public string productname { get; set; } [requiredif("haveexperiance", true, errormessageresourcename = "resourcename", errormessageresourcetype = typeof(yourresourcetype))] public string productname { get; set; }
Comments
Post a Comment