I have an HTML form in Rails like
<form name="input">
Title: <input type="text" name="title">
<input type="button" value="Submit">
</form>
I want title to be a required field (i.e. the user cannot leave it blank.) How can I verify that the user filled something in? Should I do it on the client side, or on the server side? My feeling is that doing on the client side would save communication to the server in the case that the user didn’t fill it in.
You can do both, but should at least do it on the server. Remember that any client can easily circumvent any client-side validation, which leads to your code potentially breaking.
You can do it easily in the model with:
On the client there are various ways of how to do this. The simplest is perhaps using the
requiredhtml5 attribute on the input tag. You can also use a javascript library like validatious, for which you can use this Rails plugin to automatically generate the client side validations based on your server-side validations.