Aurelia验证

编程入门 行业动态 更新时间:2024-10-25 01:25:19
Aurelia验证 - 在aurelia-validation@0.9.2中替换isValid(Aurelia Validation - Replacement for isValid in aurelia-validation@0.9.2)

在aurelia-validation版本0.6.8中,您可以使用isValid标志检查html中的验证是否已通过或失败。

像这样设置验证属性:

import {Validation} from 'aurelia-validation';
@inject(Validation)

@customElement('base-content')
export class BaseContent {
    this.validation = validation;
    this.validator = this.validation.on(this).ensure('baseContent.Name').isNotEmpty();
}
 

然后像这样使用isValid:

<button class="btn btn-success" type="submit" disabled.bind="!validation.result.isValid">Sign In</button>
 

这曾经工作,但我最近升级了aurelia验证,所以我现在有以下包。 - aurelia-validation@0.9.2 - aurelia-validatejs@0.5.2

我正在寻找的是一种复制我以前做过的方法,但是使用新的包。 这是我能得到的最接近但是我不想在以前有一个不错的选择时进行脏检查。

import {inject, NewInstance} from 'aurelia-framework';

@inject(NewInstance.of(ValidationController))
export class Login {
    this.controller = controller;
}

get isValid(){
    if (this.controller.validate().length == 0) {
        return true;
    }
    return false;
}
 
 <button class="btn btn-success" type="submit" disabled.bind="!.isValid">Sign In</button>
 

有任何想法吗?

In aurelia-validation version 0.6.8 you could check if the validation had passed or failed in the html using the isValid flag.

Set up the validation property like this:

import {Validation} from 'aurelia-validation';
@inject(Validation)

@customElement('base-content')
export class BaseContent {
    this.validation = validation;
    this.validator = this.validation.on(this).ensure('baseContent.Name').isNotEmpty();
}
 

And then use the isValid like this:

<button class="btn btn-success" type="submit" disabled.bind="!validation.result.isValid">Sign In</button>
 

That used to work but I have recently upgraded the aurelia validation so I now have the following packages. - aurelia-validation@0.9.2 - aurelia-validatejs@0.5.2

What I'm looking for is a way to replicate what I used to do but with the new packages. This is the closest I could get but I don't want to be dirty checking when there used to be a nice alternative.

import {inject, NewInstance} from 'aurelia-framework';

@inject(NewInstance.of(ValidationController))
export class Login {
    this.controller = controller;
}

get isValid(){
    if (this.controller.validate().length == 0) {
        return true;
    }
    return false;
}
 
 <button class="btn btn-success" type="submit" disabled.bind="!.isValid">Sign In</button>
 

Any ideas?

最满意答案

您可以使用validation-errors自定义属性来获取对当前errors数组的引用。 此数组中的对象形状如下: { error: ValidationError, target: Element } 。

<form validation-errors.bind="errors">
  ...
  ...
  <button class="btn btn-success" type="submit" disabled.bind="errors.length">Sign In</button>
</form>

You could use the validation-errors custom attribute to get a reference to the current errors array. Objects in this array are shaped like this: { error: ValidationError, target: Element }.

<form validation-errors.bind="errors">
  ...
  ...
  <button class="btn btn-success" type="submit" disabled.bind="errors.length">Sign In</button>
</form>

                    
                     
          

更多推荐

本文发布于:2023-08-06 17:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1453864.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Aurelia

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!