简体   繁体   中英

Force re-validation in jsViews if data-linked value doesn't change

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 (ie 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

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, eg "" - and then it works. See updated version: http://jsfiddle.net/w43hD/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM