Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate presence BelongsTo relations on change #355

Open
ixxvivxxi opened this issue Feb 19, 2025 · 0 comments
Open

Validate presence BelongsTo relations on change #355

ixxvivxxi opened this issue Feb 19, 2025 · 0 comments

Comments

@ixxvivxxi
Copy link

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();
    }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant