title: Rails Active Record - How to do validation which calls method rather than throws error
body: <p>Active Record validations throw an error when they fail. What I have in a model is </p>
  
  <pre><code>validate_format_of :field_which_cannot_have_spaces, :with =&gt; /^[^\s]+$/, :message =&gt; "Some error message"
  </code></pre>
  
  <p>What I want instead, is for a string replacement to substitute spaces for underscores (snake_case). </p>
  
  <p>The advantages of using validation for me, are that it runs every time the field is changed unless <a href="http://edgeguides.rubyonrails.org/active_record_validations.html#skipping-validations" rel="nofollow"><code>save(validate: false)</code></a>, and that I don't need to repeat the replacement in the create and update controller methods.</p>
  
  <p>Front end javascript solutions won't help if the user hacks the form... a rails solution is needed!</p>