Skip to content
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

Documentation/support for 422 field level validation errors #8

Closed
zyllorion opened this issue Feb 20, 2015 · 6 comments
Closed

Documentation/support for 422 field level validation errors #8

zyllorion opened this issue Feb 20, 2015 · 6 comments
Assignees

Comments

@zyllorion
Copy link

Ember Canary
Ember Data beta 15

I retrieve errors back from the server in what seems to be the recommended way:

        res.status(422).send({
            "errors": {
                "field_one": ["error 1", "error 2"],
                "field_two": ["error 1"],
                "serviceNotification": ["A problem with serviceNotification"]
            }
        });

... along with an implementation for RESTAdapter.ajaxError from http://alexspeller.com/server-side-validations-with-ember-data-and-ds-errors/ which populates the records errors object. In inspector for the record that's put, $E.get('errors.content') returns an array of 4 error objects with an attribute and message for each.

My template has something like:

{{fm-checkbox label=(t 'some_translation') checked=model.myModel.theField errors=model.myModel.errors.theField.message}}

This example doesn't work, but model.myModel.errors.theField does (however it displays [object Object] as the error). In the example above there would be two errors for one of the fields, held as two separate objects in the errors.content.

Not sure if you have a different implementation for errors or I'm just addressing it incorrectly within the template?

I'll reference emberjs/data#2028 because it has related information.
Also http://emberjs.jsbin.com/sidarufuco/1/edit?html,css,js,output as an example that shows multiple errors.

Hope that's enough information!

Thanks

@petermumford
Copy link

@zyllorion did you find a solution to this? I am getting the same issue as you.

@zyllorion
Copy link
Author

@petermumford I've actually removed any dependency on this npm for this and ie8 reasons. I wrote my own components in the end.

@Emerson
Copy link
Owner

Emerson commented Mar 23, 2015

Sorry for missing this guys - Was not subscribed to the issues. I'll be addressing this as soon as possible and have turned on notifications!

@Emerson Emerson self-assigned this Mar 23, 2015
@petermumford
Copy link

@Emerson To get this working for me I changed the error function in fm-errortext.js to the below:

if (Ember.isArray(this.get('errors'))) {
  return this.get('errors.firstObject');
} else {
  return this.get('errors');
}

Originally the code was looking for an array and I was passing in an object, hence the strange output. So now I'm simply checking if the object is an array.

@Emerson
Copy link
Owner

Emerson commented Mar 24, 2015

Really appreciate the feedback and code. I've gone ahead and added this (and another error case) to core and it's available as an update through NPM now.

Let me know if you try to update - I recently upgraded the project to use Ember-CLI latest, which moved to HTML bars.

Thanks again!

@Emerson Emerson closed this as completed Mar 24, 2015
@petermumford
Copy link

Looks great. Thank you.

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

No branches or pull requests

3 participants