简体   繁体   English

选择列表上的Aurelia验证

[英]Aurelia Validation on Select List

I have a simple select list in my Aurelia view which I'm trying to set a default value on of 'Select...'. 我在Aurelia视图中有一个简单的选择列表,我试图在'Select ...'上设置默认值。 I'm also using the aurelia-validation plugin to ensure that the value is changed before the form is submitted. 我还使用aurelia-validation插件来确保在提交表单之前更改值。 The plugin works great for other field types in my project. 该插件非常适合我项目中的其他字段类型。

<div class="form-group">
                <label for="agencies" class="control-label">Agency</label>
                <select value.bind="agencyId" class="form-control">
                  <option value="">Select..</option>
                  <option repeat.for="agency of agencies" value.bind="agency.id">${agency.name}</option>
                </select>
              </div>

In the VM: 在VM中:

constructor(validation) {
    this.agencies = null;
    this.agencyId = 0;
    this.validation = validation.on(this)
        .ensure('agencyId')
          .isNotEmpty();
}
activate() {
    //call api and populate this.agencies 
}

After the page initially loads I get my agencies in the list and my default value is correct, but it shows the validation error message: 页面最初加载后,我在列表中获取我的代理,我的默认值是正确的,但它显示验证错误消息: 在此输入图像描述

Other form fields, like text boxes don't do this and show no error message until the user interacts with the form controls. 其他表单字段(如文本框)不会执行此操作,并且在用户与表单控件交互之前不会显示任何错误消息。

Is there something special I need to do for a select list to hide validation errors on the initial loading of the view? 我是否需要为选择列表做一些特殊操作来隐藏视图初始加载时的验证错误? I suspect that binding the select list in the view is somehow triggering a change event. 我怀疑在视图中绑定选择列表会以某种方式触发更改事件。

Thanks to a kind Aurelia user on Gitter, the problem was solved by setting the initial value of this.agencyId to "". 感谢Gitter上的Aurelia用户,通过将this.agencyId的初始值设置为“”来解决问题。 Originally I had the this.agencyId = null. 最初我有this.agencyId = null。 That was my mistake. 那是我的错。 Because it was null and not "" (as was the default value in the select list) the values didn't match so the select list was invalid when the view loaded. 因为它为null而不是“”(因为是选择列表中的默认值),所以值不匹配,因此加载视图时选择列表无效。 At least, that's my understanding. 至少,这是我的理解。

The lesson is, if you want to validate a select list, make sure you VM property is initialized to the same value as your select list's default value. 本课程是,如果要验证选择列表,请确保将VM属性初始化为与选择列表的默认值相同的值。

constructor() {
    this.agencyId = ""; **//must match the bound property's initial value**
}

And in the view: 在视图中:

<div class="form-group">
  <label for="agencies" class="control-label">Agency</label>
    <select value.bind="agencyId" class="form-control">
     <option value="" **<!-- this value must match the VM initial value -->** selected="true">Select...</option>
      <option repeat.for="agency of agencies" value.bind="agency.id">${agency.name}</option>
    </select>
</div> 

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

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