1
votes

I have an Ember object that has as attribute a boolean field that I want to validate with ember-changeset-validations.

It is the typical "Agree to terms" checkbox.

Here is the hbs code:

{{one-way-checkbox changeset.agree_terms 
class="form-control" 
update=(action (mut changeset.agree_terms))}}

{{#each changeset.error.agree_terms.validation as |error|}}
    <span class="text-danger help-block m-l-3">{{t (concat 'bookings.error.' error)}}</span>
{{/each}}

I have a validations file, where the particular validation for this member of changeset is:

agree_terms: validateFormat({
regex: /^(true)$/,
message: 'You need to agree on Terms!'
})

Thing is, for some reason this never validates to TRUE correctly, I always get the error message...any ideas what I'm doing wrong here?

1

1 Answers

2
votes

This is a bit tricky; but I figured that out. ember-change-set-validations do make use of ember-validators underneath. Since; you are using validateFormat; the following code is being run at the end. If you look at the link I have provided; there is a check !canInvoke(value, 'match') causes you the problem. Since the checked value of a checkbox is a boolean and you cannot invoke match on booleans you always end up having an invalid validation.

What can you do? Well; I am not an expert at ember-change-set-validations and ember-validators but it is pretty easy to write a custom validator. This is what I did in the following twiddle. It seems to be working pretty fine.

What I understood is; it is not a good idea to use boolean values with format type validator (it clearly does not work); I only wished it was documented. I hope this helps you.