简体   繁体   English

使用 ember-cp-validations 验证可选电子邮件

[英]Validate optional email with ember-cp-validations

I would like users to optionally insert an email using ember-cp-validations :我希望用户可以选择使用ember-cp-validations插入电子邮件:

const Validations = buildValidations({
  managerEmail: {
    validators: [
      validator('presence', null), // means it can be optional when used alone
      validator('format', { type: 'email' })
    ]
  }
});

But it still requires an email and won't accept an empty field.但它仍然需要电子邮件并且不会接受空字段。 How can I make it optional?我怎样才能使它成为可选的?

Your definition should be as follows to allow blank (to make it optional):您的定义应如下所示以允许空白(使其可选):

const Validations = buildValidations({
  managerEmail: validator('format', { type: 'email', allowBlank:true})
});
  const Validations = buildValidations({
  managerEmail: 
      [validator('presence', {
             presence: true,
             disabled:computed(
                    //your condition here
                    //return true/false
         )}
     )),
      validator('format', { type: 'email' })
    ]  
   });

you can add disabled property and control the behavior of the field您可以添加禁用属性并控制字段的行为

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

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