Validate optional email with ember-cp-validations

447 Views Asked by At

I would like users to optionally insert an email using 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?

2

There are 2 best solutions below

0
On BEST ANSWER

Your definition should be as follows to allow blank (to make it optional):

const Validations = buildValidations({
  managerEmail: validator('format', { type: 'email', allowBlank:true})
});
0
On
  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