I'm writing a web app using PHP, and running into several situations where I need similar code on the server and browser. For example:
- I want to validate user input, and generate the same error messages on both sides.
- I want to format data using certain rules (e.g. if a given field is less than 1, show it with two decimal places, otherwise none), and have it appear the same regardless of which side renders it.
It seems like this should be a common problem, as people are moving more logic from the server to the browser. But are there any common patterns or libraries for dealing with it (particularly for PHP)?