mercredi 13 janvier 2016

Component validation Ember-Validations

I am trying to validate a field in a single component using ember-validations but giving the possibility of passing a validations object to the component instead of hardcoding it inside. This code:

EditDefaultPropertyComponent = Ember.Component.extend EmberValidations.Mixin,

validations:
  value:
    numericality: true

onValueObserver: Ember.observer('value', ->
  @validate()
    .then(() =>
      ...
      console.log 'good'
      @set 'error', null
    )
    .catch((err) =>
      ...
      console.log 'bad'
      @set 'error', err
    )
  )

setupFlags: (->
  ...
).on('init')

Will work just fine, but if I want to setup at component's initialization time the validations object with some arbitrary validation passed as a parameter to the component, say for example in setupFlags:

  setupFlags: (->
    @setProperties(
        ...
        @set 'validations', Ember.copy ( { value: { numericality: true }}))
).on('init')

Won't work. It will always print "good" no matter the input. If I access

console.log (@get 'validations')

inside the then() branch of the @validate method it shows however the object. Am I missing some control flow here?




Aucun commentaire:

Enregistrer un commentaire