0

I've got a validation like this:

import Ember from 'ember';
import EmberValidations from 'ember-validations';


export default Ember.Controller.extend(EmberValidations, {
  validations: {
    age: {
      presence: {message: 'Enter your age'}
    }
  },
  isAgeValid: function () {
    //some chek
  }.property('age'),

  canValidateAge: function () {
    //some check to determine if to render icons
  }.property('age')
    }

In my template:

  {{input id="age" placeholder="Age" value=age  class="form-control input-md"}}
                <span class="help-block">help</span>
              {{#if canValidateAge}}
                  <span class="{{if isAgeValid 'glyphicon-ok' 'glyphicon-remove'}} form-control-feedback glyphicon"></span>
                  <span class="field-error text-danger help-block">{{errors.age.[0]}}</span>
              {{/if}}

In the debugger I of course have a property called validations but I don't see any way how to use it. Also is there a way to avoid having to write this methods like is AgeValid and canValidateAge(shows no icons if in "pristine" state).

bower:

  "devDependencies": {
    "broccoli-asset-rev": "^2.0.2",
    "broccoli-merge-trees": "^0.2.1",
    "broccoli-static-compiler": "^0.2.1",
    "ember-cli": "0.2.5",
    "ember-cli-app-version": "0.3.3",
    "ember-cli-babel": "^5.0.0",
    "ember-cli-content-security-policy": "0.4.0",
    "ember-cli-dependency-checker": "^1.0.0",
    "ember-cli-htmlbars": "0.7.6",
    "ember-cli-ic-ajax": "0.1.1",
    "ember-cli-inject-live-reload": "^1.3.0",
    "ember-cli-less": "^1.3.3",
    "ember-cli-qunit": "0.3.13",
    "ember-cli-uglify": "^1.0.1",
    "ember-data": "1.0.0-beta.17",
    "ember-disable-proxy-controllers": "^0.7.0",
    "ember-export-application-global": "^1.0.2",
    "ember-validations": "^2.0.0-alpha.3"
  }
SuperUberDuper
  • 9,242
  • 9
  • 39
  • 72

1 Answers1

0

You can observe the errors property.

isAgeValid: Em.computed.gt('errors.age.length', 0)

If you want to show the validations only after touching the input field, I also suggest this blog post where you simply show errors only after focusOut: http://www.teamrarebit.com/blog/2015/02/14/ember-inline-validation-errors

Update:

The missing part, by @SuperUberDuper:

extend(EmberValidations.Mixin)

enspandi
  • 663
  • 4
  • 6
  • thanks but my errors object is undefined in the controller. Has ember validations failed to be initialised? – SuperUberDuper May 18 '15 at 14:29
  • Hmm... try to output the errors object in the template like this, {{errors.age}} – enspandi May 18 '15 at 14:31
  • If no errors occure in the developer tools and the age property can be printed using {{age}}, I suggest to debug into the presence validator of ember-validations both init and call method, to see if it is even called: https://github.com/dockyard/ember-validations/blob/master/addon/validators/local/presence.js – enspandi May 18 '15 at 14:37
  • I had to add extend(EmberValidations.Mixin – SuperUberDuper May 18 '15 at 15:36
  • I already thought of this, but then I checked the documentation and didn't find the Mixin Usage anywhere ... https://github.com/dockyard/ember-validations#usage ... good that it's working now :-) – enspandi May 18 '15 at 16:04