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
Allow committing a record whilst in invalid state #1876
Allow committing a record whilst in invalid state #1876
Conversation
This would be really helpful. Our team has run into issues with this numerous times that we've had to work around. As Alex mentioned, it seems like the API should ultimately be responsible for ensuring validity of data. |
@tchak I think you may be interested in this? |
@alexspeller I understand your point, but I do not like the idea of adding a new api to |
@tchak short answer: that doesn't work Long answer: calling clear on the errors triggers Would you be happier with something like: clear: function(keepState) {
if (get(this, 'isEmpty')) { return; }
get(this, 'content').clear();
if (!keepState) {
this.trigger('becameValid');
}
} |
If the server returns a 422 response with errors, the record enters the invalid state. This means that it is impossible to save the record again without setting all of the invalid properties at least once. Sometimes this makes sense, but if the server is the arbiter of validity then often it does not, especially if validity of properties is interdependant or based on external conditions. Example: Server validates firstName + lastName must be at least 10 characters combined. Server returns {errors: {firstName: "too short", lastName; "too Short"}} Now firstName to be 20 characters long. The record is valid according to the server, but calling record.save() results in an error Attempted to handle event `willCommit` on <DS.Model> while in state root.loaded.updated.invalid This patch allows save() to be called on an invalid record, meaning the server can decide if it's valid or not.
@tchak I just changed it to that because I think it's a better api anyway - does this assuage your concerns? |
@alexspeller I am not sure I get your point. Why the record is in |
@tchak are you saying that in every application you should always do this in your code: record.errors.clear();
record.save(); That seems like a bad api. If we're going by rails you can always call In short, I think this should "just work" and the impact is so minimal that I don't understand why you wouldn't want it to work for such a common use case. Any time you do validation, the UI is likely to make it easy to submit the record multiple times and this will be an edge case that you have to educate new developers about making the library harder to use. |
@tchak here's where I read that, State errors that happen today are because of bugs in Ember Data itself. So according to @wycats apparently |
@alexspeller I agree with you that |
@alexspeller just tried the following change in
Your tests included in this PR passes... Am I missing something ? |
@tchak It's more likely that I've missed something - I'm going to have another look |
@tchak yes that's much nicer, sorry I misunderstood what you were saying before. Closing in favour of tchak/data@950f8cf |
@alexspeller sorry for not being more clear :) and thank's a lot for pointing the problem. |
If the server returns a 422 response with errors, the record enters
the invalid state. This means that it is impossible to save the record
again without setting all of the invalid properties at least once.
Sometimes this makes sense, but if the server is the arbiter of
validity then often it does not, especially if validity of properties
is interdependant or based on external conditions.
Example:
Server validates firstName + lastName must be at least 10 characters
combined.
Server returns {errors: {firstName: "too short", lastName; "too Short"}}
Now set firstName to be 20 characters long. The record is valid according
to the server, but calling record.save() results in an error:
This patch allows save() to be called on an invalid record, meaning
the server can decide if it's valid or not.