简体   繁体   English

有没有一种方法可以在语义ui上使用promise和自定义验证

[英]Is there a way to use promise with custom validation on semantic ui

Is there any way to use promise with a custom rule on semantic ui? 有什么方法可以将诺言与语义ui上的自定义规则一起使用? I want to validate a field with my custom rule. 我想用我的自定义规则来验证一个字段。 If response of server is false field must be red. 如果服务器的响应为假,则字段必须为红色。 Here's what I'm trying to do. 这就是我想要做的。

$.fn.form.settings.rules.myCustomRule = function(param) {
let myPromise = this.get('api').r_get(allUrls.urlName, model);
  myPromise.then(
    response => {
        return true;
    }
  ).catch(
    error => {
        return false;
    }
  );
}

You can add error class to a div or input when the promise returns false. 当promise返回false时,可以将error类添加到div或输入。 Would that work? 那行得通吗? Like this: 像这样:

 function simulateServer(){ $('#serverresponse').addClass('error'); } 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.3.3/semantic.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="serverresponse" class="ui input"> <input type="text" placeholder="Search..."> </div> <br><br> <button onclick="simulateServer()">Contact Server</button> 

It wouldn't be possible with a custom rule promise, as it expects an immediate response. 自定义规则承诺将不可能实现,因为它期望立即做出响应。

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

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