mardi 10 novembre 2015

Validating computed property in model

So far, I have an 'address' model set as the following:

import Ember from 'ember';
import DS from 'ember-data';
import { validator, buildValidations } from 'ember-cp-validations';

const { attr } = DS;
const { computed } = Ember;

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

export default DS.Model.extend(Validations, {
  /* Properties */

  streetName: attr(),
  streetNumberNum: attr('number'),
  streetNumberAlpha: attr(),
  addressDetails: attr(),
  municipalityName: attr(),
  zip: attr(),

  /* computed */

  streetNum: computed('streetNumberNum', 'streetNumberAlpha', function() {
    return this.get('streetNumberNum') + '' + this.get('streetNumberAlpha');
  })
});

I created the computed property streetNum so that I can use it in the template. The idea is to allow the user to input, in a field, his address street number (including an alpha) within a unique field (i.e. 1000a).

I would like to use the ember-cp-validations addon for validations purposes; and I was wondering how can I make ember understand this usecase.

Aucun commentaire:

Enregistrer un commentaire