Hi I am working on a Rails 3 project and I am using acts-as-taggable-on and everything works perfectly! 🙂
I just have one question.
Does anyone know how I can add my ‘custom’ validation to ActsAsTaggableOn::Tag? Any callbacks I can hook into (e.g before_tag_save)? or something similar?
I need to run a regex on each ‘tag’ (to ensure that each tag does not contain any illegal characters) in my tag_list before I save my model and would like to know if there is a standard way of doing it.
The way I solved the problem is by adding a validation method in my PostController which just iterates over the list of Tags and runs the regex, but this seems ugly to me.
Any suggestions?
Thank you in advance! 🙂
I used two ways in the past. One via a custom validator, another with a validates call.
Custom Validation method
In your model, setup the following
Obviously you will need to replace the validation logic and the error message text with something more appropriate in your circumstance.
Keep in mind that in this scenario you can evaluate each tag as a string.
Standard Validation Method
Include this in your model
With this method, you will have to keep in mind that you are validating the entire array that looks like a string. For this reason, you will need to allow for commas and white spaces between the tags.
Choose whichever method suits you most