I have a form with 3 ActiveRecord fields. One of these fields has its own stupidity and requirements for checking STATE DEPENDENCE. (For example, I only check a field if an object is created in the form of a setup wizard.)
In my POST handler to create an object, I thought I could call errors.add to insert a special error condition
@foo = Foo.new(params[:foo]) if goofy_conditions(params[:foo][:goofy_field]) @foo.errors.add(:goofy_field, "doesn't meet the goofy conditions" ) end respond_to do |format| if @foo.save ... else ... redirect back to form (with error fields hilited)
However, executing @ foo.errors.add () on the controller does not seem to do anything ... it does not prevent saving () if other fields pass validation.
An alternative is to install a special validation handler in the model ... I know that using errors.add (: field, 'msg') works fine ... but in this case, how can my controller pass information to the validator, indicating whether check this field.
jpwynn
source share