简体   繁体   English

如何在ember-cp-validations中验证checkbox,selectbox和radiobutton

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

I am using ember-cp-validation in ember js application for validation. 我在ember js应用程序中使用ember-cp-validation进行验证。 I want to validate checkbox , selectbox and radiobutton .I can't find any sample code to validate these controls. 我想验证checkbox ,选择selectboxselectbox radiobutton 。我找不到任何示例代码来验证这些控件。

Can anybody give any ideas/sample on how to validate checkbox , selectbox and radiobutton using ember-cp-validation plugin. 任何人都可以使用ember-cp-validation插件提供有关如何验证checkbox ,选择selectboxselectbox radiobutton任何想法/示例。

Validate a checkbox like this: 验证如下的复选框:

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

ember-cp-validations validates the value of variables (whether on a model, controller, etc). ember-cp-validations验证变量的值(无论是在模型,控制器等)。 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 见这里: 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}}

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

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