0

I ran into an issue while using jsViews with validation (using code from jsviews.com/#samples) and jQuery UI Autocomplete:
I have a converter (convertBack) on the field that transforms the entered text back into a GUID based on a dictionary. It returns null if the field is empty or invalid.

The issue is that jsViews doesn't notice the update from one null value to the other (i.e. blank to invalid, and vice versa). I tried to fix this by adding a call to refreshValidates() on the validation tag to the DOM onChange manually, but any invalid value entered gets deleted.

Question: Is there a way to achieve re-validation in jsViews natively?

I changed the jsViews validation code to allow checking displayed value:
[End of onAfterLink]: It passes the current (displayed) value, not only the converted (which is null in both cases):

 (...)
    tag.validate(tagCtx.args[0], tag.linkedElem.val()); // Validate initial data
neo post modern
  • 2,262
  • 18
  • 30

1 Answers1

1

tag.linkedElem is the HTML element on which you are doing 2-way data-binding, (and validation).

So if you just want to get the current value of the input element, yes, tag.linkedElem.val() is good.

Additional response added following your comment below:

Looking at your jsfiddle: http://jsfiddle.net/w43hD/1/

You are using

data-link="{validate DictionaryValue inDictionary='dictionary' convert='fromGuid' convertBack='toGuid'}"

which will trigger validation when the DictionaryValue it is binding to changes. But you are mapping both invalid user entry strings and the empty string to the same DictionaryValue of null. So of course switching from invalid to empty string does not trigger validation.

You can change your getKey converter to map the empty string to something other than null, e.g. "" - and then it works. See updated version: http://jsfiddle.net/w43hD/2/

BorisMoore
  • 8,444
  • 1
  • 16
  • 27
  • Thanks, I updated my question accordingly - any idea how to achieve re-validation without property change? – neo post modern Dec 03 '13 at 12:18
  • I'm not sure if I fully understand the scenario/issue. Not sure what you mean by blank/invalid. Can you put a simple example/sample on a jsfiddle? I'll see if I can help... – BorisMoore Dec 05 '13 at 03:31
  • I had to write a good bit of code because I couldn't paste the original, but here it goes: http://jsfiddle.net/w43hD/1/ – neo post modern Dec 06 '13 at 20:01
  • I added a response to your jsfiddle, above. – BorisMoore Dec 08 '13 at 02:55
  • Thanks! That was easier than I thought... Luckily our JSON interface treats `""` and `null` just the same. But would there be a way to achieve this behavior in jsViews? I am very happy with this solution, but other people facing this issue, might have more complicated scenarios... – neo post modern Dec 08 '13 at 16:06
  • 1
    The important thing to understand is that the validation is validating the DATA, not the user value in the input text box. That way the same validation code is agnostic of whether the UI element is a select, text box, check box, radio button, textarea etc. So validation applies the test of the validation to the data (so after your convertBack). You therefore have to allow invalid data. But you don't send invalid data to the server, you prevent submission until the validation all passes. – BorisMoore Dec 08 '13 at 18:59