I am having a validation problem when writing a Redmine plugin.
I'm writing a hook for the issue model, and as a part of the hook method, i would like to invalidate the creation of the issue, by adding a custom error:
def controller_issues_new_before_save( context = { } )
context[:issue].errors.add(:due_date, "A custom error")
end
For testing purposes, I have written a patch that overwrites Issue.validate_on_create, but it seems that every time when entering validate_on_create errors.count is set to zero.
I need to stop the creation of the issue object , but only when an attribute is set into another model object.
I thought about writing this in the validate_on_create method, but then I would need to pass it the other object.
The first solution that I thought about would be to insert an additional field in the Issue model, and modify it inside the hook.
Something like :
def controller_issues_new_before_save( context = { } )
context[:issue].can_validate = false
end
def validate_on_create
unless can_validate
errors.add("error", "A custom error")
end
end
where Issue.can_validate is an addition to the Issue model
However, this does not seem the best approach here. Is there an easier way?