ember.js - cp-validation not working as expected -


i integrating cp-validaiton ember app. not getting response. 1 me here.

my route js :

import ember 'ember'; import validations '../validation'  export default ember.route.extend(validations, {   num:null,     message: '',   model(){     return { num:null}   },   actions:{      check(){         this.set('message','');        this.validate().then(({model, validations})=>{         if(validations.get('isvalid')){           console.log('statge', validations.get('isvalid') )                     this.set('message','');                 }else{         if(model.get('validations.attrs.num.isinvalid')){                         this.set('message',model.get('validations.attrs.num.messages'));                     }         }       })     }   } }); 

my validation.js :

import { validator, buildvalidations } 'ember-cp-validations';  export default buildvalidations({     num: [         validator('number',{         allowstring: true,         integer: true,         message: 'error! not integer!' }),     validator('presence', true) ] }); 

template :

<h1>this route</h1>  enter age:<br> {{input value=model.num}}<br> <div style='color: red'>{{message}}<br></div> <button {{action 'check'}}>check</button><br> 

live in twiddle

validations must defined upon data object, which, in case, not route's attribute underlying controller's model. controller's model set ember in setupcontroler() hook of route.

thus use nested keys approach target validations set upon correct data:

import { validator, buildvalidations } 'ember-cp-validations';  export default buildvalidations({     'controller.model.num': [         validator('number',{         allowstring: true,         integer: true,         //message: 'error! not integer!' }),     validator('presence', true) ] });  

note: attribute definition

export default ember.route.extend(validations, {   num:null, // not used, confusing :)   message: '',   .... } 

is unnecessary , confusing since not used, feel free remove it.

checking validity / errors etc. on attribute basis can achieved follows:

 model.get('validations.attrs.controller.model.num.isvalid');  model.get('validations.attrs.controller.model.num.errors');  model.get('validations.attrs.controller.model.num.messages'); 

your route this:

import ember 'ember'; import validations '../validation'  export default ember.route.extend(validations, {   model(){     return { num:null}   },   actions:{      check(){         this.set('controller.message','');        this.validate().then(({model, validations})=>{         if(validations.get('isvalid')){            this.set('controller.message','');         }else{      if(model.get('validations.attrs.controller.model.num.isinvalid'){             this.set(               'controller.message',                model.get('validations.attrs.controller.model.num.messages.firstobject'));          }         }       })     }   } }); 

however, recommend migrating validations-related code model instead of building them in route. it's more logical, readable, lets decrease size of code, , fits find in ember-cp-validations docs.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -