ember-changeset-validations
ember-changeset-validations copied to clipboard
Initial changset validation state
Given a changeset reacts to changes, the default state is always valid until a change is made on each property.
Is there a way to force a validation to run on the entire changeset on construction?
e.g.
let model = {};
let validations = Ember.Object.create({
myKey: validatePresence(true)
});
let changeset = new Changeset(model, lookupValidator(validations), validations);
console.log(changeset.get('isValid')); // true, but the changeset is invalid
changeset.validate();
console.log(changeset.get('isValid')); // false, as expected
Could you call validate
on the init of whatever it is that's being instantiated, like in your snippet above?
@poteto For sure, but it does limit building validated changesets in a template. Should this be a default behaviour in your mind? Perhaps an option that can be passed to the constructor?
@poteto The validation that is proposed works nice but it expects you to define changeset outside of templates. When (changeset data validation) is used I'm not able to find a way how to do it and I have to modify your module.
It is enough to extend function changeset([model, validationMap]) with changset.validate(). This should be configurable, so adding a new attribute (e.g. initValidation with false as a default) looks sensible. If I will prepare PR, are you willing to merge it?
@marxsk That's what I ultimately ended up doing and just setting a controller or component property for the template to bind to the pre-validated changeset object.
@poteto could we bump this issue ? This is badly needed.
I have the same issue and using validate()
right after the Changeset has been initialised is not the proper solution I think.
For instance, by doing that it forces all the error messages to show up on the first render of a form.
@MrChocolatine I often use the suggested pattern with much success. I call validate()
right after initializing the Changeset, but then I hide validation errors in the form until other events occur such as an individual input blur (show errors for that field) or the user tries to submit the form (show errors for all fields). I really like the flexibility and control that this approach gives me.
To me it sounds more like an incorrect implementation. We should not to have to do this manually and a first validation should occur internally, when the Changeset is created. Or at least via an option as already suggested.
@MrChocolatine Appreciate the input! I certainly think an optional initValidation boolean approach is worthwhile and will help give users visibility into the initial state of the changeset without hooking into the JS side of things. I generally wait until user input to perform any of this logic (submit/blur) but I can see that limited use cases may require this API. I created an issue in ember-changeset to track and implement.
https://github.com/poteto/ember-changeset/issues/451
Thank you @snewcomer 👍 .