ember-changeset-validations icon indicating copy to clipboard operation
ember-changeset-validations copied to clipboard

Initial changset validation state

Open utilityboy opened this issue 7 years ago • 9 comments

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

utilityboy avatar Apr 20 '17 20:04 utilityboy

Could you call validate on the init of whatever it is that's being instantiated, like in your snippet above?

poteto avatar Apr 21 '17 01:04 poteto

@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?

utilityboy avatar Apr 21 '17 13:04 utilityboy

@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 avatar May 17 '17 16:05 marxsk

@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.

utilityboy avatar May 17 '17 17:05 utilityboy

@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 avatar Mar 31 '20 14:03 MrChocolatine

@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.

kpfefferle avatar Mar 31 '20 16:03 kpfefferle

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 avatar Mar 31 '20 16:03 MrChocolatine

@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

snewcomer avatar Apr 02 '20 04:04 snewcomer

Thank you @snewcomer 👍 .

MrChocolatine avatar Apr 02 '20 06:04 MrChocolatine