client_side_validations and callbacks

Maxence Source

I have a model with fields that are validated frontend with client side validations gem.

My problem is for the below field (European VAT number for France) :

validates :VAT, length: { maximum: 15 }
validates :VAT, format: { with: /\A[a-zA-Z]{2}\d{0,13}\z/ }

Also I have this callback in order to normalize it before validation :

before_validation :normalize_vat

def normalize_vat
  if self.VAT.present?
      self.VAT = self.VAT.delete(' ')

This callback capitalize letters and strips the spaces that a user could have input such as :

FR 333 3333 333333

Though it seems client_side_validations is not picking the callback frontend...

I could change the regexp to match any spaced input but it wrecks a bit my validations as I can't validate length no more for example ...

Would there be a trick to fix this ?



comments powered by Disqus