Ok, here is a quick example of a few things that might help:
fields do
value1 :integer, *:required*
value2 :integer, *:required*
total :integer, *:required*
timestamps
end
attr_accessible :value1, :value2, :total
*before_save :check_total*
def check_total
if total != value1 + value2 then
errors.add(:total, "Total must be the sum of value1 and value2")
end
end
Adding :require to a field will force the fields to not be blank. Rails
will throw an error if they are and not allow the record to be saved.
Rails has various validations. Check out:
http://guides.rubyonrails.org/active_record_validations.html
However, if your are trying to do something complex, the built in
validations will not work. You will have to define a custom validation.
Above I have defined a method called check_total. The line* "**before_save
:check_total"* causes Rails to run this method before the record is saved.
This will get called when the record is created or changed. If total
doesn't equal the sum of value1 and value2 then an error is set on the
:total field.
In practice, if I am doing calculations in jQuery I will disable the submit
button so that it can't be clicked until the fields have correct values, at
which point I re-enable the submit button. I always have Rails validations
as well since you cannot trust the client javascript results to always be
correct.
--
You received this message because you are subscribed to the Google Groups "Hobo
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/hobousers.
For more options, visit https://groups.google.com/d/optout.