How to validate checkbox, selectbox and radiobutton in ember-cp-validations

846 Views Asked by At

I am using ember-cp-validation in ember js application for validation. I want to validate checkbox, selectbox and radiobutton.I can't find any sample code to validate these controls.

Can anybody give any ideas/sample on how to validate checkbox, selectbox and radiobutton using ember-cp-validation plugin.

2

There are 2 best solutions below

0
On

Validate a checkbox like this:

const Validations = buildValidations({
  acceptedTerms: validator("inclusion", {
    allowBlank: false,
    in: [true],
  }),
})
0
On

ember-cp-validations validates the value of variables (whether on a model, controller, etc). As long as the checkbox is bound to a variable of the same name as specified in the validator, it should work regardless of widget type. (whether the value is connected via 2-way binding or changed when an action fires)

See here: http://offirgolan.github.io/ember-cp-validations/docs/modules/Basic.html#objects

So, a pseudocode example... First define the validations in your controller...

const Validations = buildValidations({
  bar: validator('presence', true)
});

export default Ember.Component.extend(Validations, {
    bar: null
})

And in your template, bind the value of the checkbox to the value:

{{input type=checkbox value=bar}}