bcardarella/client_side_validations — Issue #243: Client side validation for class-level validations
Author: jeremywadsack
Created: 2011-12-21 19:41:24+00:00
State: open

We're trying to implement a client-side validation that handles what, in the model, is a class-level (not field-level) validation rule.

Basically, the sum of child allocations must be greater than zero.

````
class QuantityValidator < ActiveModel::Validator

def validate(record)
record.errors[:base] << I18n.t(:number_can_not_be_zero) if
record.child_types.sum(&:number_chosen) == 0
end

end
````

Looking through [your code](https://github.com/bcardarella/client_side_validations/blob/master/vendor/assets/javascripts/rails.validations.js#L82), it appears that validators can only apply to *elements*, not `fieldset`s or the entire form, and that those elements must also be inputs.

````
form.trigger('form:validate:before').find('[data-validate="true"]:input:enabled').each(function() {
if (!$(this).isValid(validators)) { valid = false; }
});
````

It would seem reasonable that there would be validations applied to an object in aggregate that are not specific to a field and that these should be able to be duplicated client side. Based on the FormBuilder/Formtasitc model I would expect these to be attached to a `fieldset` in the HTML.

If this seems reasonable (and I'm not missing something obvious), I'm happy to dig into this and submit a proposed solution.

Failing that, we can handle the validations separately (and are doing that at the moment) but I can't figure out any way for something outside of `clientSideValidations` object to indicate that the form is not valid and cancel the submit event. Thoughts?