I’ve been bouncing back and forth lately on different client-side JavaScript libraries/frameworks. I like Backbone. Not a fan of ExtJs. Etc.
Anyway, they all seem to have one giant problem in that I have to define validation logic in both the server side (Rails 3) and client side. Plus, I have to do the same with my model definitions (AR Objects and `JS Objects’). Then I have to define the business rules in both places too.
Seems like I’m always developing two concurrent applications.
I know this is a subjective question, but for us small one-man teams who can’t afford dedicated JS guys and dedicated Ruby guys, what are my solutions?
I’m racking my brain and maybe I’m missing something but I can’t find a single solution to this problem.
I thought about writing a Ruby gem that would generate local JS objects. So at least my business objects would be the same. But this sounds scary. Especially since I may not want all attributes client-side.
What are your thoughts on this problem? Do I just have to live with it?
I think it’s something you just have to live with. If you think about the nature of the problem, and WHY we do both client and server data validation, you can come to the conclusion that there isn’t currently a way around it without degrading the user experience or putting your application at risk.
Think about it like you are sending a shipment of goods across the country on a train. At the source location, somebody checks the logs to make sure that everything in the order has been included on the train, and that none of the goods are damaged. At the destination, another person checks that they’ve received everything they ordered in the shipment, and that nothing is damaged.
What happens if you skip one of those validation steps? You run the risk of sending an incomplete shipment without your “server” side validation. Without validating the incoming shipment on the other end, if someone were to hijack the train and swap out a bunch of counterfeit goods, you wouldn’t find out about it until the goods had been sold and the cops were at your door.
The time (and expense) it takes the train to get from one location to the other creates an incentive to validate both incoming and outgoing goods, simply because an error on either end requires another train to be sent.
Admittedly, this metaphor is kind of a stretch, but hopefully you get the picture. We need validation on both ends.