title: Rails validation on form, not record
body: <p>I have a Rails input form on which one field is not directly related to a field in the underlying object: I have to do some calculation before putting it into the ActiveRecord field. That's ok so far: I just put the calculation into the controller.</p>
  
  <p>My problem is that the calculation can fail, and I want to report that to the user. I know how to do that with a flash alert, but that doesn't associate the error with the specific form entry field and is inconsistent with the errors that record validation gives. What I want to do is tell the form which entry field has failed, and why.</p>
  
  <p>All the documentation I can find on that uses ActiveRecord validations, but that's too late: I can't create the ActiveRecord because I don't have the data to populate it (and even if I populate it with invalid dummy data -- yech! -- I've lost the details of what caused the error by the time it hits the validation).</p>
  
  <p>So what's the Rails way of generating form validation errors from a controller (ok, ok, but I want to get it working before I refactor it into a concern) rather from a model validator? Or what should I be doing instead?</p>
  
  <p>Edit:
  Here's an example of the sort of think I'm trying to do. Imagine a fleet manager dealing with thousands of vehicles, identified by their registration numbers (licence plates). When a vehicle is checked out to a driver, there's no point offering a drop-down with thousands of items, so the user enters the registration number. The controller looks this up (or, better, passes it to the model to look up), it gets matched to the vehicle record, and vehicle record id gets put into the checkout. If the user mistypes the number, the controller/model (probably) finds there's no entry for that vehicle. I know want to tell the user why the save has failed, in a way that's consistent with other validation failures.</p>