Ruby on Rails custom validator to validate HEX color format

62
0

This snippet defines a custom validator for hex color codes, ensuring that values follow the correct #RGB or #RRGGBB format. By using ActiveModel::EachValidator, the validation logic remains reusable across multiple models. This approach keeps models clean while providing a centralized way to enforce formatting rules, making it easier to maintain consistent validation logic across the application.

Martin Sojka, Maker of CodeSnips