9

I'm using RemoteAttribute for a particular field on my form. The purpose of it is not important. What is important is that it needs to fire the validation action whenever the field is changed. This is working fine for me except when the field is changed to be blank.

I've Googled this but found no results. Does anybody know if RemoteAttribute does actually trigger for blank fields and if not, how it can be forced?

Alternatively, can the remote validator be customised/modified to trigger for blank values?

Paul Fleming
  • 24,238
  • 8
  • 76
  • 113
  • While not a direct answer to your question, MVC's remote validator merely calls into jQuery Validation's "remote" validator, so I'd say this is a bit more of a jQuery Validation question and not as much an MVC question. – Eilon Feb 05 '13 at 18:36
  • @Eilon. You're absolutely right. I've re-tagged with `jQuery`. – Paul Fleming Feb 05 '13 at 21:22
  • I'm not familiar with RemoteValidator, but it must be triggered by something...change? Can you not trigger a change event if the value is blank? Or alternatively pass a falsy value to RemoteValidator on any change that ends up with a blank input? – dgo Apr 13 '13 at 13:43
  • @user1167442 That would be useful if you could advise further... – Paul Fleming Apr 13 '13 at 14:13
  • What triggers RemoteValidator? What event? – dgo Apr 13 '13 at 14:53
  • @user1167442. I don't know, that's why I'm asking this question! – Paul Fleming Apr 13 '13 at 15:59
  • Lol. Try it out in the browser console. Add some lines to your code, these are guesses, but something like: $('input').on('change',function (e){console.dir(e);console.log(e.data);console.log(e.result);}). Then you could also do something like $("*").bind('RemoteValidator $.fn.RemoteValidator',function(e){console.dir(e)}). A good place to look for the second example is in the e.originalEvent, that may reveal the event that triggered the validator. None of these might shed any light, but if you play with that type of thinking, eventually it will yield fruit. – dgo Apr 13 '13 at 17:30

1 Answers1

2

jQuery validation will not run any rules if the value of an element is empty. If you have a minLength = 2 rule but the input field is left empty the rule will not fail. Only if the user enters 1 character it will fail.

The same idea applies to a remote validator. If the value is empty then it does not go to the server.

I don't think there is an option in the plugin to change this behavior. You will need to create a custom async rule to get the behavior you are expecting. This might turn a little tricky. Are you sure there is no other way of doing it? Does it make sense to validate an empty string in the server? Can you add the required rule?

epignosisx
  • 6,152
  • 2
  • 30
  • 31
  • 4
    The server is deciding whether or not that particular field needs to be required. The required validator triggers for empty fields. Why would the remote validator be any different? I'm effectively trying to create a remote IsRequired validator, based on information available only to the server. – Paul Fleming Feb 07 '13 at 20:13
  • The remote validator is not any different. It has the same behavior than other validators. The Required validator has to trigger if the field is empty, because that's its behavior. All others validators will not trigger if the field is empty. – epignosisx Feb 07 '13 at 20:48
  • Aren't you likely seeing `Required` validators execute on the client, whereas a `RemoteValidator` is going to call the server by definition (except for your `empty` situation). I'm just saying I that they are different. – David Tansey Apr 13 '13 at 19:34
  • 1
    I need this behaviour as well - when valid() is called it must do the remote validation regardless. Reasons: 1) Only the server knows if the field is required in the given situation at the given time. 2) Through this I can define all validation rules in one place which becomes VERY important when you are dealing with a big complex system! So this must be an option in the settings of the plug-in. – TheStoryCoder Nov 26 '14 at 11:07
  • 1
    Found a work-around. See solution here: [jquery validation remote - validate whether data in field or not](http://stackoverflow.com/a/19429305/2404541) – TheStoryCoder Nov 26 '14 at 12:32