mercredi 18 janvier 2017

How to set DS.Errors to ember-cp-validator in component

I have validation rule on component:

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

const Validations = buildValidations({
    email: {
        validators: [
            validator('ds-error'),
            validator('format', {
                type: 'email'
            })
        ]
    }
});

export default Ember.Component.extend(Validations,{
    store: Ember.inject.service(),
    didValidate: false,
    actions: {
        register: function() {
            this.validate().then(({ model, validations }) => {
                if (validations.get('isValid')) {
                    var user = this.get('store').createRecord('user',this.getProperties('email'));
                    user.save().catch((error) => {
                       // error --> ErrorClass
                   });
                }
                this.set('didValidate', true);
            });
        }
    }
});

If fields in component is valid I create user in store and save. Client validation works great, but server validation based on ds-error don't works.

It's happend becouse ds-errors goes to user model, not component. (Validations mixed to model works)

Error response format: (422)

{  
   "errors":[  
      {  
         "title":"Invalid Attribute",
         "source":{  
            "pointer":"/data/attributes/email"
         },
         "detail":"has already been taken"
      }
   ]
}

Question: How to make it work




Aucun commentaire:

Enregistrer un commentaire