简体   繁体   English

如果数据链接值不变,则在jsViews中强制重新验证

[英]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: 使用jsViews进行验证(使用jsviews.com/#samples中的代码)和jQuery UI Autocomplete时遇到问题:
I have a converter (convertBack) on the field that transforms the entered text back into a GUID based on a dictionary. 我在字段上有一个转换器(convertBack),可将输入的文本转换回基于字典的GUID。 It returns null if the field is empty or invalid. 如果该字段为空无效,则返回null。

The issue is that jsViews doesn't notice the update from one null value to the other (ie blank to invalid, and vice versa). 问题是jsViews不会注意到从一个空值到另一个空值的更新(即空白到无效,反之亦然)。 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. 我尝试通过将对验证标签上的refreshValidates()的调用手动添加到DOM onChange来解决此问题,但是输入的任何无效值都将被删除。

Question: Is there a way to achieve re-validation in jsViews natively? 问题:有没有一种方法可以在jsViews中本地实现重新验证?

I changed the jsViews validation code to allow checking displayed value: 我更改了jsViews验证代码以允许检查显示的值:
[End of onAfterLink ]: It passes the current (displayed) value, not only the converted (which is null in both cases): [ onAfterLink ]:它传递当前(显示的)值,不仅传递已转换的值(在两种情况下均为null ):

 (...)
    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). tag.linkedElem是您要在其上进行双向数据绑定(和验证)的HTML元素。

So if you just want to get the current value of the input element, yes, tag.linkedElem.val() is good. 因此,如果您只想获取输入元素的当前值,可以, tag.linkedElem.val()很好。

Additional response added following your comment below: 在下面的评论后添加了其他回复:

Looking at your jsfiddle: http://jsfiddle.net/w43hD/1/ 查看您的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. DictionaryValue绑定到更改时,它将触发验证。 But you are mapping both invalid user entry strings and the empty string to the same DictionaryValue of null. 但是,您会将无效的用户输入字符串和空字符串都映射到相同的DictionaryValue为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. 您可以更改getKey转换器以将空字符串映射到非null以外的其他内容,例如“”-然后它可以工作。 See updated version: http://jsfiddle.net/w43hD/2/ 查看更新的版本: http : //jsfiddle.net/w43hD/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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