Permalink
Browse files

Add validation to the fields

  • Loading branch information...
corrspt committed Nov 20, 2016
1 parent 735c5bc commit a8ab06664a997f71176ce7849fa9be6bdd5050f3
Showing with 28 additions and 2 deletions.
  1. +5 −0 app/controllers/application.js
  2. +19 −1 app/models/person.js
  3. +4 −1 app/templates/components/attribute-text.hbs
@@ -16,10 +16,15 @@ const {
export default Controller.extend({
i18n: service(),
+ validateField(field) {
+ this.get('model').validate({ only: [field] });
+ },
+
setField(field, value) {
assert(`Must pass a valid attribute - ${field}`, !isEmpty(field));
let model = get(this, 'model');
model.set(field, value);
+ this.validateField(field);
},
actions: {
View
@@ -1,11 +1,29 @@
import DS from 'ember-data';
+import Validator from '../mixins/model-validator';
const {
Model,
attr
} = DS;
-export default Model.extend({
+export default Model.extend(Validator, {
name: attr('string'),
age: attr('number'),
+
+ validations: {
+ name: {
+ presence: true,
+ length: {
+ minimum: 4
+ }
+ },
+ age: {
+ presence: true,
+ numericality: {
+ allowBlank: true,
+ onlyInteger: true,
+ greaterThanOrEqualTo: 18
+ }
+ }
+ }
});
@@ -1,4 +1,4 @@
-<div class="form-group">
+<div class="form-group {{if (gt (get model (concat 'errors.' attribute '.length')) 0) 'has-error'}}">
<label title="{{placeholder}}" class='control-label' for='{{attribute}}'>
{{label}}
</label>
@@ -10,6 +10,9 @@
type='text'
id={{attribute}}
>
+ {{#each (get model (concat 'errors.' attribute)) as |error|}}
+ <span class="help-block">{{error.message}}</span>
+ {{/each}}
{{#if hasHelp}}
<span class="help-block">{{helpText}}</span>
{{/if}}

0 comments on commit a8ab066

Please sign in to comment.