Sunday, 15 August 2010

c# - Using remote validation to check that a field has a value not firing if field has no value -



c# - Using remote validation to check that a field has a value not firing if field has no value -

i have received directive superior utilize remote validation instead of requiredif attribute checking field has value if able user edited.

right now, code looks -

view model

public class foomodel { // snip: unimportant variables public int? deviceid; public int? providerid; [remote("ismessagerequired", "foo", additionalfields="deviceid,carrierid", errormessage="(required other")] public string messageaddress { get;set; } // snip: unimportant other details }

controller

public class foocontroller { // snip: unimportant details public jsonresult ismessagerequired(string messageaddress, int? device, int? carrier) { if(!string.isnullorempty(messageaddress)) homecoming json(true, jsonrequestbehavior.allowget); // conditions: // a) device = "samsung" / carrier = "other" // b) device = "other" if(device = foomodel.getdeviceidbyname("samsung") && carrier = foomodel.getprovideridbyname("other") { homecoming json(! string.isnullorempty(pageaddress), jsonrequestbehavior.allowget); } if(device = foomodel.getdeviceidbyname("other")) { homecoming json(! string.isnullorempty(pageaddress), jsonrequestbehavior.allowget); } // default status occurs on first-run scenarios. homecoming json(true, jsonrequestbehavior.allowget); } // snip: other unimportant details }

...and finally, view:

@model fooproject.models.foomodel @html.dropdownfor(x => x.deviceid) @html.dropdownfor(x => x.providerid) @html.textboxfor(x => x.messageaddress) @html.validationmessagefor(x => x.messageaddress)

question: validation fires when value entered, never when textbox contains no value. remote validation work when there value in textbox? if not, how approaching setup incorrectly?

it fires when $.trim returns value - nonempty , non-whitespace values.

c# asp.net-mvc-3 remote-validation

No comments:

Post a Comment