Skip to content

Validate presence BelongsTo relations on change #355

@ixxvivxxi

Description

@ixxvivxxi

ember-data 3.28, ember-model-validator 4.8

export default class MyModel extends Model {
  @attr('string') name;
  @belongsTo('category') category;

  validations = {
    name: {
      presence: { message: 'Please enter name' },
    },
    category: {
      presence: { message: 'Please select a category' },
    },
  };
}

If you run validation when nothing is entered, errors will appear on 2 input fields. When you start entering a name, the error on the name will disappear automatically. But when you select a category, you need to run validation again for the error to disappear.

Is it possible to validate automatically?

Now I'm using notifyPropertyChange function to solve this issue in my common model like this:

  notifyPropertyChange(key) {
    super.notifyPropertyChange(key);
    
    if (this.get(`errors.${key}`)) {
      this.validate();
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions