I have tried and done this so far in a few ways, but have no clue if there is a more Ember-way to do this.
On of the ways I did is create a controller and use for explicit input validation calls. Error would be set (properties of this controller) and previously bound to views. This way the errors would be displayed instantly.
The other way was similar to this, only I validated model values.
But none of these are actually nice enough. A great way of doing this would be to have it done in a bit more automated way. Maybe just before commit, or by observing values and have it done when they are invalid.
Is there an expected/proposed way of doing this with Ember?
I’d be curious what the core team has to say about this. I’ve turned my “view” into a form of sorts when I need to do basic input validation.
In this way the template is still bound to the model through the controller, but I found the javascript view itself didn’t do much (aside from holding the templateName that I never needed to override) so I found adding input validation was a good fit.
To validate the inputs / reset them after a new item is added I do the work in the view itself (leaving the controller to handle other computed properties / model helpers)
I’ve found that my server side platform of choice has a similar feel where html templates are the equivalent to handlebars but a special “form” is used to handle html inputs and validation separate from the controller and model. (django)
This type of separation also makes it easy to unit test the inputs and events that bubble up from the view.
I haven’t really thought about it and created it to solve my problems at the time. But here are some pros/cons that are obvious for me:
PROS:
You can easily subclass error(), valid() and clearErrors() to use your html structure, in this example it uses the bootstrap html. You could easily add all errors on the top of the form by doing something alike in your error() method: