Skip to content

support multiple validation errors #29

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

Merged
merged 4 commits into from
Aug 13, 2020
Merged

Conversation

basz
Copy link
Contributor

@basz basz commented Aug 3, 2020

Prevents get errors() to return an array-in-array with messages.

This results in all validation messages to be rendered even when showMultipleErrors is false...

ps. I've tried a unit test, but failed... :-/

@basz
Copy link
Contributor Author

basz commented Aug 10, 2020

does this seem legit to you?

Copy link
Collaborator

@jelhan jelhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ps. I've tried a unit test, but failed... :-/

I guess you meant a component integration test? Could you share what you have tried? I can not recall when I have seen validation to be an array of validation messages. It would be helpful if you can provide an example for that one.

let error = get(this, `model.error.${this.property}.validation`);
return error ? [error] : [];
let errors = get(this, `model.error.${this.property}.validation`);
return errors ? errors : [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the documentation of validated-changeset correctly, model.error.${this.property}.validation could be either a string or an array: https://github.com/validated-changeset/validated-changeset/#error

Ember Bootstrap expects errors property of FormElement to be an array of validation messages: https://www.ember-bootstrap.com/api/classes/Components.FormElement.html#property_errors

Of course model.error.[this.property] could also be undefined if the property is valid. So it seems like we need to handle three cases here:

  1. model.error.${this.property}.validation is undefined -> return empty array
  2. model.error.${this.property}.validation is a string -> wrap that string into an array and return that one
  3. model.error.${this.property}.validation is an array -> return that array

Can you please update the code accordingly? Some in-source comments about the different states may also be helpful for future readers.

@basz
Copy link
Contributor Author

basz commented Aug 10, 2020

will do...

you are correct;

arrays are returned when multiple (an array of) validators have been defined.
strings when a single validator (I assumed ember-bootstrap already handled those)
and undefined for none.

@basz
Copy link
Contributor Author

basz commented Aug 10, 2020

as for testing. I think i need to do this

ember generate component-test bs-form/element --unit

to create a unit test for the component. However it (empty) fails to run with 'TypeError: Cannot convert undefined or null to object'

@jelhan
Copy link
Collaborator

jelhan commented Aug 10, 2020

as for testing. I think i need to do this

ember generate component-test bs-form/element --unit

to create a unit test for the component. However it (empty) fails to run with 'TypeError: Cannot convert undefined or null to object'

I think unit tests are not supported for @glimmer/component. But I also don't think we want a unit test. As the consumer will only use the addon through the template, all tests should be component integration tests, which actually renders the component. I think it should be just an additional test in ember-bootstrap-changeset-validations/tests/integration/components/bs-form-element-test.js.

@basz
Copy link
Contributor Author

basz commented Aug 10, 2020

hmmm... ok. how do we test undefined then. Simply no rendered message?

@jelhan
Copy link
Collaborator

jelhan commented Aug 10, 2020

hmmm... ok. how do we test undefined then. Simply no rendered message?

Yes. undefined means that there isn't a validation error. So it should not show a validation message. But I think for your use case the multiple validation messages is more important.

@basz
Copy link
Contributor Author

basz commented Aug 12, 2020

there added a test for each case

Copy link
Collaborator

@jelhan jelhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. Thanks a lot.

If you have some time left an additional test case that multiple validation errors are actually shown if showMultipleErrors argument of <BsForm:: Element> is true would be great. But I totally understand if you don't have any more bandwidth to work on this topic. Would merge as-is in that case.

@jelhan jelhan added the bug label Aug 12, 2020
@jelhan jelhan changed the title prevent array in array support multiple validation errors Aug 12, 2020
@basz
Copy link
Contributor Author

basz commented Aug 12, 2020 via email

@jelhan jelhan merged commit c88e2eb into ember-bootstrap:master Aug 13, 2020
@basz
Copy link
Contributor Author

basz commented Aug 13, 2020

will you do a release or are you waiting on other stuff?

@jelhan
Copy link
Collaborator

jelhan commented Aug 13, 2020

Will try to do a release later today.

@jelhan
Copy link
Collaborator

jelhan commented Aug 13, 2020

Released v3.1.2 containing this fix just right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants