indexiatech / ember-forms

Smart, Intuitive forms for Ember.js styled with Bootstrap, Multi layouts and Validation support.
http://indexiatech.github.io/ember-forms
Apache License 2.0
218 stars 45 forks source link

Validation from documentation example doesn't work #99

Open Skivmag opened 9 years ago

Skivmag commented 9 years ago

I'm trying to implement validation example from here http://indexiatech.github.io/ember-forms/quickexample

Model:


`import DS from 'ember-data'`

User = DS.Model.extend
    username: DS.attr('string')
    email: DS.attr('string')

User.reopen
    validations:
        username:
            presence: true
            length:
                minimum: 5

`export default User`

Template:

{{#em-form model=model form_layout="inline"}}
            {{em-input property="username" value=model.username}}
            {{em-input property="email" type='email' value=model.email}}
        {{/em-form}}

But when I try to enter invalid text to username field - nothing happened

danconnell commented 9 years ago

You have to import and mixin EmberValidations onto the model

Skivmag commented 9 years ago

Generally, I did so

`import DS from 'ember-data'`;
`import EmberValidations from 'ember-validations'`;

User = DS.Model.extend(EmberValidations,
    username: DS.attr('string')
    email: DS.attr('string')
)

User.reopen
    validations:
        username:
            presence: true
            length:
                minimum: 5

`export default User`

But validation still doesn't work

danconnell commented 9 years ago

If you're on the latest ember-validations, you have to extend EmberValidations.Mixin

cimtico commented 9 years ago

I'm running into this issue also. I have the following definition

import DS from 'ember-data';
import EmberValidations from 'ember-validations';

var User = DS.Model.extend(EmberValidations.Mixin, {
  firstName: DS.attr('string'),
});

User.reopenClass({
  FIXTURES: [],
  validations: {
    firstName: {
      presence: true,
      length: {minimum: 5}
    }
});

export default User;

however it seems validations are being ignored completely. I'm using:

"ember-cli": "~0.2.5",
"ember-idx-forms": "0.6.0",
"ember-data": "1.0.0-beta.16.1",
"ember-validations": "^2.0.0-alpha.3"
cimtico commented 9 years ago

I found what the problem was. It seems that adding the validations on the reopenClass doesn't work. It should be done on reopen instead.

var User = DS.Model.extend(EmberValidations.Mixin, {
  firstName: DS.attr('string'),
});

User.reopenClass({
  FIXTURES: [],
});

User.reopen({
  validations: {
    firstName: {
      presence: true,
      length: {minimum: 5}
    }
});