From 8ed69132cfaf250c19272422f8e02d50a8aef276 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 1 Apr 2026 20:27:29 +0200 Subject: [PATCH] fix: add hex color validation to Category model and form (to solve #1247) (#1341) - Added server-side validation to Category model to enforce 6-digit hex format for colors. - Added HTML pattern attribute to category form for client-side validation. - Updated tests to cover validation and fixed existing tests using shorthand hex colors. --- app/models/category.rb | 1 + app/views/categories/_form.html.erb | 2 +- test/models/budget_test.rb | 4 ++-- test/models/category_test.rb | 15 +++++++++++++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app/models/category.rb b/app/models/category.rb index 94d5097e1..39a740f7d 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -9,6 +9,7 @@ class Category < ApplicationRecord belongs_to :parent, class_name: "Category", optional: true validates :name, :color, :lucide_icon, :family, presence: true + validates :color, format: { with: /\A#[0-9A-Fa-f]{6}\z/ } validates :name, uniqueness: { scope: :family_id } validate :category_level_limit diff --git a/app/views/categories/_form.html.erb b/app/views/categories/_form.html.erb index 3b9e6f1b4..1f3e5fbeb 100644 --- a/app/views/categories/_form.html.erb +++ b/app/views/categories/_form.html.erb @@ -30,7 +30,7 @@