ember.jsember-octaneember-changeset

ember-changeset-validations not validating properly


I'm on Ember 3.15 (Octane) and trying to get ember-changeset-validations working by following the examples on their github but having a hard time getting it to validate. There are no (code related) errors in the console but changeset.isValid always returns true.

{{! application/controller.js}}
import {action} from "@ember/object";
import MyValidationClass from '../Validations/myValidations';

export default class MyController extends Controller {
  MyValidationClass;

  @action
  submit(changeset) { 
    changeset.save()
  }
}

--

{{! application/template.hbs}}
<MyComponent
  @changeset={{changeset this.model this.MyValidationClass}}
  @onSubmit={{this.submit}}
/>

--

{{! application/components/mycomponent.hbs}}

<BsForm @formLayout="horizontal" {{on 'submit' (fn this.submit @changeset)}} @model={{@changeset}} as |form|>
  <form.element
    @controlType="text"
    @label="Title"
    @placeholder="Title"
    @property="title"
    @required={{true}}
  />
</BsForm>

--

{{! application/components/mycomponent.js}}
export default class MyComponent extends Component {
    async submit(changeset) {
      await changeset.validate();

      if(changeset.isValid) // returns true even when the validation should fail
        this.args.onSubmit(changeset);
    }
}

--

{{! application/Validations/myValidations.js}}
import {
  validateLength,
  validatePresence
} from 'ember-changeset-validations/validators';

export default {
  title: [
    validatePresence(true),
    validateLength({ min: 44 })
  ]
};

Solution

  • Okay, found help through a github issue

    Basically in the controller, change

    MyValidationClass;
    

    to something like

    MyValidationClass = MyValidationClass;
    

    without that, MyValidationClass was being set to undefined