An extra validator for conditional validations with ember-changeset-validations
.
ember install ember-changeset-conditional-validations
Let's say you want to validate a user's settings. Only if the payment method is a credit card should the credit card number validations be applied.
import { validatePresence, validateLength } from 'ember-changeset-validations/validators';
import validateSometimes from 'ember-changeset-conditional-validations/validators/sometimes';
export default {
creditCardNumber: validateSometimes([
validatePresence(true),
validateLength({ is: 16 })
], function(changes, content) {
return this.get('paymentMethod.isCreditCard');
})
};
validateSometimes()
takes 2 arguments. The first is a validator or an array of validators you want applied to the attribute. The second argument is a callback function which represents the condition. If the condition callback returns true
, the rules will be added. This callback function will be invoked with the changeset's changes and content. The callback will also be invoked with its this
value set to an object that has a get()
method for accessing a property. this.get(property)
first proxies to the changes and then the underlying content, and has the same semantics as Ember.get()
.
import Changeset from 'ember-changeset';
import lookupValidator from 'ember-changeset-validations';
import Validations from './../validations/settings';
let settings = {};
let changeset = new Changeset(settings, lookupValidator(Validations), Validations);
console.log(changeset.get('isValid')); // true
changeset.set('paymentMethod', {
isCreditCard: true
});
changeset.validate();
console.log(changeset.get('isValid')); // false
console.log(changeset.get('errors')); // [{key: 'creditCardNumber', validation: ['Credit card number can't be blank', 'Credit card number must be a number']}]
changeset.set('creditCardNumber', '1234567890123456');
changeset.validate();
console.log(changeset.get('isValid')); // true
changeset.set('creditCardNumber', '1234');
changeset.validate();
console.log(changeset.get('isValid')); // false
console.log(changeset.get('errors')); // [{key: 'creditCardNumber', value: '1234', validation: ['Credit card number must be equal to 16']}]
changeset.set('paymentMethod', {
isCreditCard: false
});
changeset.validate();
console.log(changeset.get('isValid')); // true
You can also have a combination of validations that will always run and conditional validations. For example, say you wanted to validate that a property is a number, but conditionally validate that the number is greater than 5. You could do something like the following:
import { validateNumber } from 'ember-changeset-validations/validators';
import validateSometimes from 'ember-changeset-conditional-validations/validators/sometimes';
export default {
someProperty: [
validateNumber({ integer: true }),
validateSometimes(validateNumber({ gt: 5 }), function() {
// condition
})
]
};
Let's say in the previous example that you also wanted to conditionally validate that the number is less than 10. You could do something like the following:
import { validateNumber } from 'ember-changeset-validations/validators';
import validateSometimes from 'ember-changeset-conditional-validations/validators/sometimes';
export default {
someProperty: [
validateNumber({ integer: true }),
...validateSometimes([
validateNumber({ gt: 5 }),
validateNumber({ lt: 10 })
], function() {
// condition
})
]
};
git clone <repository-url>
this repositorycd ember-changeset-conditional-validations
npm install
ember serve
npm test
(Runs ember try:each
to test your addon against multiple Ember versions)ember test
ember test --server
ember build
For more information on using ember-cli, visit https://ember-cli.com/.
This demo shows how to use the cell type renderer feature to make some conditional formatting: first row is read-only, and formatted in green bold text, all cells in the Nissan column are written in i
条件运算符? :是C中唯一的三元运算符。 ? :条件运算符语法 expression1 ? expression2 : expression3 首先评估Expression1。 如果其值为true,则计算expression2并忽略expression3。 如果expression1被评估为false,则表达式3计算并忽略expression2。 结果将是expression2或expressi
Exp1 ? Exp2 : Exp3; 其中Exp1,Exp2和Exp3是表达式。 注意结肠的使用和放置。 一个值? 表达式的确定方式如下:评估Exp1。 如果是,那么Exp2会被评估并成为整个值吗? 表达。 如果Exp1为false,则计算Exp3,其值将成为表达式的值。 的? 被称为三元运算符,因为它需要三个操作数,可以用来替换if-else语句,它们具有以下形式 - if(conditio
HTTP具有条件请求 的概念,通过比较受影响的资源和验证器的值,可以更改结果,甚至请求的成功。这些请求可用于验证缓存的内容,避免无用的控制,验证文档的完整性(例如恢复下载时),或防止在上载或修改服务器上的文档时丢失更新。 原则 HTTP条件请求是执行不同的请求,具体取决于特定标头的值。这些头文件定义了一个先决条件,如果前提条件匹配,请求的结果将会不同。 不同的行为由所使用的请求的方法以及用于前提条
这是使用 PixelCNN 解码器生成条件图像的 Tensorflow 实现,其引入了最初在像素周期性神经网络中提及的基于 PixelCNN 架构的门控 PixelCNN 模型。 该模型可以基于标签或图像的潜在表示来相应地生成图像。图像也可以无条件地建模。 它也可以作为一个强大的解码器,并可以在自动编码器和 GANs 中取代反卷积(转置卷积)。这篇文章的详细摘要可以在这里找到。 示例: 架构: 这
问题内容: 我对Spring注释配置有疑问。我有一个bean: 我有一个属性文件: 在属性文件中,我想要一个特殊的boolean property 这标志着是否应该创建一个bean ObservationWebSocketClient。如果属性值为false,我根本不想建立Web套接字连接。 有没有实现这一目标的技术可能性? 问题答案: 尽管我没有使用过此功能,但似乎可以使用spring 4的注解