Skip to content

Feature Request: A way to remove errors without rolling back #282

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

Closed
erichaus opened this issue Feb 2, 2018 · 14 comments · Fixed by #307
Closed

Feature Request: A way to remove errors without rolling back #282

erichaus opened this issue Feb 2, 2018 · 14 comments · Fixed by #307

Comments

@erichaus
Copy link

erichaus commented Feb 2, 2018

In our form component and app we have a concept of non_field_errors which get returned from the api (default django-rest-framework behavior). These errors are "form-level" and not tied to a specific field.

To keep things simple we use our changeset form to display these if they occur by doing something like:

        try {
            yield bankcard.save();
        } catch (err) {
            err.errors.forEach(({ attribute, message }) => {
                changeset.pushErrors(attribute, message[0]);
            });
            return undefined;
        }

which if err === non_field_errors, our form template will render at the top of the form like:

    {{cool-form/error changeset "non_field_errors"}}

The problem is if a user submits and receives a non_field_error, we have to then handle clearing just that one specific error before they can submit again. While this is easy to do, to delete just that specific error with no related input attached we have to do delete changeset._errors.non_field_errors

Proposal: Can we add a public interface to remove a single error without relying on private properties?

e.g. changeset.removeError('non_field_errors') or changeset.clearError('non_field_errors')

OR just a public way to clear all errors e.g. changeset.clearErrors() without rolling the form back

@erichaus erichaus changed the title Feature Request: changeset.removeError() Feature Request: A way to remove errors Feb 2, 2018
@erichaus erichaus changed the title Feature Request: A way to remove errors Feature Request: A way to remove errors without rolling back Feb 2, 2018
@nucleartide
Copy link
Contributor

Thanks for the detailed writeup @erichonkanen, this sounds reasonable to me.

My only concern is that I don't remember (off the top of my head) if changes and errors are mutually exclusive. Here's what I mean:

const c = new Changeset(...);
c.set('phoneNumber', '123-123-1234'); // valid, internal changes map has 'phoneNumber'
c.set('phoneNumber', 'asdfhjkl'); // invalid, internal errors map has 'phoneNumber'
// but does the internal changes map still have 'phoneNumber'?

If changes and errors are mutually exclusive, then feel free to open a PR for this! Maybe something like changeset.rollback({ onlyErrors: true });?

@snewcomer
Copy link
Collaborator

I'll try to get this one done this week...

@snewcomer
Copy link
Collaborator

snewcomer commented Aug 8, 2018

@erichonkanen Does either of these two rollback methods work for your use case? Or do you need to clear the error but not the changes?

https://github.com/poteto/ember-changeset#rollbackinvalid
https://github.com/poteto/ember-changeset#rollbackproperty

@erichaus
Copy link
Author

erichaus commented Aug 8, 2018

@snewcomer hey thanks for the update, the use case is to clear an error but don't roll back the form.

@erichaus
Copy link
Author

erichaus commented Aug 8, 2018

@snewcomer I hadn't seen those before, let me try those out and get back to you.. but basically we handle form-level errors in our changeset form by doing changeset.pushError({ non_field_error: <error> }) which displays the error as part of our normal form component machinery, the problem is we now need to clear that non_field_error before user hits submit again, but there wasn't a way to do it w/o resetting the form (we ended up just putting a delete changeset._errors.non_field_error before submit is called)

@snewcomer
Copy link
Collaborator

@erichonkanen Yeah it seems like rollbackInvalid is close to what you want. But perhaps want to rollback a specific key but keep the changes, correct? In which case, we need to add a bit more flavor to rollbackInvalid.

@hornetnz
Copy link

hornetnz commented Feb 14, 2019

...the problem is we now need to clear that non_field_error before user hits submit again, but there wasn't a way to do it w/o resetting the form (we ended up just putting a delete changeset._errors.non_field_error before submit is called)

@erichonkanen - Were you doing a notifyProperty or something else on changeset to get isValid to recompute? I did a removeObject on the error I manually pushed in (leaving an empty array), but changeset.isValid still resolves false. (usving v1.4.2)

@snewcomer
Copy link
Collaborator

@hornetnz If you don't mind, try 2.0 and lmk if that fixes the issues you are seeing. Otherwise, definitely open to a PR!

@erichaus
Copy link
Author

@hornetnz I think I actually had to do something like changeset.set('non_field_errors', null) to get it to clear, but I'd have to dig that up again :) also just tried the latest 3.0 and seems to be working finally so thanks @snewcomer :)

@hornetnz
Copy link

Ok, thanks for the reply @erichonkanen! I'm unable to upgrade currently because we have some monkey-patches in this changeset to work with our unique data structure.

@snewcomer
Copy link
Collaborator

@hornetnz or @erichonkanen is there an opportunity to extend ember-changeset to enable some of these features? Do you mind sharing those monkey patches? Looking at improving the internals.

@erichaus
Copy link
Author

@snewcomer I think in the latest version it's no longer necessary since rollbackProperty was added

@snewcomer
Copy link
Collaborator

snewcomer commented Feb 24, 2019

Cool. Sry what do you mean...

latest version it's no longer necessary

Is everything fixed for you?

@erichaus
Copy link
Author

yeah, the monkey patch I was referencing was for 1.3, no longer necessary

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

Successfully merging a pull request may close this issue.

4 participants