How to add client side validation errors in Ember Data? - ember.js

What is correct way for handling client side validation errors? DS.Model class has methods add and clear that work fine for me, but both are marked as deprecated. What is replacement for those methods?

Not sure that about correct, but there is a library that can save you some time. If you look at this project you will see how it is used with ember data.
https://github.com/esbanarango/ember-model-validator#user-content-usage-example
In short you have mixin import Validator from '../mixins/model-validator';
that you import to your model and define validations rules.
People from DockYard also wrote a great add on for EmberObjects which you can check here
https://github.com/DockYard/ember-validations
For me personally this is too much. I prefer to do validation on server side - I am using ActiveModelSerializer and responding to for example 200 or 201 when success and 422 or 401 when unable to process entity or permission denied. This path will use DS.Error object which you can check here...http://emberjs.com/api/data/classes/DS.Errors.html. The sample easy code would be
model.save().then ( (data)=>, (error)=>...
Then in your template code you could use following
{{#each model.errors.MYFIELD as |error|}}
I can provide more specific example but your question is generic one.
Hope it helps

Related

Not possible to use shorthand route handlers if RestSerializer is used? (ember-cli-mirage)

I set up a simple Ember Twiddle to show you my error that is occurring when trying to update a model.
It's considerable that I'm using ember-cli-mirage for mocking the data.
According to the docs, I created a shorthand route that should handle the PUT request.
It does, but with the error: Your handler for the url /api/shops/1 threw an error: Cannot convert undefined or null to object
When using the JSONAPISerializer, everything is working with shorthands (mirage/config.js) and I'm able to update models, but in my case I have to use the RESTSerializer with serialized IDs in the responses.
The request payload when I'm sending the model's attrs are without Id at the end of the property name, f.e.:
// attrs object in PUT request
{
name: "Shop 1",
city: "1" // belongsTo relationship,
}
Now Mirage is trying to find those properties on the respective database model that has to be updated, but cannot find it, because in the database it's cityId and not just city...
I also found this issue report and it’s working, but I was hoping I could avoid something like this. As far as I can remember, in previous versions of ember-cli-mirage (v0.1.x) it was also not needed to override the normalize method in the serializer to be able to make use of the RestSerializer with serializedIds…
My question is:
Is there a way to stick to shorthand route handlers only, or do I really have to write a helper or other custom solution only because I have to use the RestSerializer?
That would be really sad, but at least I would know then.
Thanks for your support!
Short answer: it looks like you need the custom serializer for now until the bug fix for it is merged.
Long answer: that issue looks to be an issue that occurred in the 0.2 -> 0.3 upgrade for Mirage, likely because of underlying DB changes made in Mirage. It'll probably get fixed, but for now you'll need to work around it.

Using UniqueEntity outside of Symfony stack

I have an app built on top of Silex and I'm using Doctrine as my ORM.
I have a problem where I'm trying to get a clean error for when a user tries to reuse an email, I found the validator UniqueEntity but it seems to be designed for the full stack version of Symfony.
My question is, is this true? I'm going a limb and assuming it as I haven't found anyone who successfully used it outside of Symfony.
My second question is, if I'm not able to use UniqueEntity, whats my next best option? I'm using the Symfony Validator component and would like to use something that's plugged into that to keep it all in the same block of code.
You can use UniqueEntity with Silex.
Here's the service provider package with the Doctrine ManagerRegistry implementation - saxulum/saxulum-doctrine-orm-manager-registry-provider. Also you can find the instructions how to use it with the UniqueEntity validator in README.
But you may want to implement you own UniqueEntity validator.
For example, if you want to validate DTO object (or any non-entity object), because it's not supported by Symfony's UniqueEntity validator (see issue on GitHub).

CookieAuthenticator restlet

I have built some RESTful api's with REstlet 2.3.4. I've been using HTTP_BASIC which let the browser prompt for credentials but it's time for a proper login form. I figure the easiest way to implement this is CookieAuthenticator. I can't find full working examples on github/google. I am sure i'm over looking them can someone provide a working example implementing CookieAuthenticator in Restlet?
I did get this to work after all. I have a longer answer here with some code examples. First, i was missing the fact that CookieAuthenticator is a filter and HAS the logic to handle login and logout. You need to create EMPTY ServerResources with a method annotated with #Post that has nothing in the body. Second, extend CookieAuthenticator and overwrite isLoggingIn(..) and isLoggingOut(..) with the code found in the link.
Cheers,
-ray

Ember-Data 1.13.X : How to request included resources?

I'm converting my app over to the new version of ember-data (1.13.5).
I'd like to be able to request a set of resources (e.g. this.store.findAll('post')), maintain the background reloading behaviour that comes out of the box, but also request that a set of related resources be included from the server.
i.e. something like:
this.store.findAll('post', { include: ["comments"] }
This can obviously be done with a query, but I'm assuming that kills all of the background reloading stuff?
I think what you are trying to do is sideload your comments together with the post.
I don't know how your server code for that is written, for instance in my case where I use laravel for the serverside,
I would check for the include parameter in the controller code for retrieving the post. If include comments directive has been specified, then I would take advantage of a builtin mechanism that laravel provides where you specify whether you want a model with its related models, i.e return post->with('comments');
And then with the help of a custom json serializer, you can extract the comments and add to the json for the post .
But outside of laravel, an algorithm like this would do in the serverside code for retrieving posts
Retrieve all the posts.
Check for the include parameter. If it's specified, retrieve the comments that belong to each post.
Then write code that creates a json result merging the json result for post with its comments together.

Can a Custom DataProvider class expose Custom Templates?

I am currently in the process of writing a custom DataProvider. Using the Intergrate External Data documentation.
I've managed to show the external data in the Sitecore back end. However whenever I try to view the data in the items I created, I am getting an error
Null ids are not allowed. <br> Parameter name: displayName
There seems to be precious little on the subject on how to create a custom DataProvider on the Sitecore Developer Network.
The example on their website seems to only show how to import a SINGLE item into a static database. However I am simply trying to merge some items into the hierarchy and I can't find any useful documentation.
It seems that one of your methods that should return an ID doesn't. It might be GetChildIds and/or GetParentId.
Nick Wesselman wrote a good article about it gathering all the information including an example on the Marketplace. I think that is your best start. You can read it here.
Turns out I needed to include at the very least, the Fields->Section->Template in the GetParent method. To be on the safe side I included the Fields/Sections/Templates into my implementations of
GetChildIDs
GetItemDefinition
GetParentID
It wasn't obvious that this was the case, since I had in fact implemented the GetTemplates method correctly, and I had expected that should be enough.