I am using the jQuery validate plugin which is awesome for straightforward form validation. What i especially like is that it offers the user a better user experience, because there is no need for the round-trip to the server.
I still need to do the validation on the server-side (rails) as well. I guess there should be a way to extract the validations from the model into the view transparently, but for now i just add a class "required" myself.
Now i did bump into a seemingly difficult problem, when the user wanted my to validate to having either one of both fields filled in. Luckily google came up with a beautiful answer quick enough!
I needed to adapt it somewhat, because my form-styling places each label-field combination inside a div, so i search for the second surrounding ‘div’ (containing the group of fields for which only one field is required).
jQuery.validator.addMethod('required_group', function(val, el) {
var $module = $j(el).parent('div').parent('div');
return $module.find('.required_group:filled').length;
}, 'Please fill out at least one of these fields');
How i love the jQuery and Rails community. Awesome!
