Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add validation for incomplete and bad formatted dates

Dombi Attilaa19a97f51642
diff --git a/app/forms/projects/life_cycles/form.sass b/app/forms/projects/life_cycles/form.sass
index 2a03c6d3a3a..c9f42f2d5b1 100644
--- a/app/forms/projects/life_cycles/form.sass
+++ b/app/forms/projects/life_cycles/form.sass
@@ -5,6 +5,9 @@
flex: 1 1 100%
primer-text-field:last-child
flex: 1 0 170px
+ @media screen and (min-width: $breakpoint-lg)
+ primer-text-field
+ min-height: 76px
@media screen and (max-width: $breakpoint-sm)
.FormControl-horizontalGroup
flex-direction: column
diff --git a/app/models/project/phase.rb b/app/models/project/phase.rb
index 7bb0fca3ce9..01df7adf283 100644
--- a/app/models/project/phase.rb
+++ b/app/models/project/phase.rb
@@ -39,6 +39,7 @@ class Project::Phase < ApplicationRecord
through: :definition
validate :validate_date_range
+ validate :validate_date_format
delegate :name,
:position,
@@ -78,6 +79,15 @@ class Project::Phase < ApplicationRecord
end
end
+ def validate_date_format
+ %i[start_date finish_date].each do |attr|
+ raw_value = send("#{attr}_before_type_cast")
+ if raw_value.present? && raw_value.is_a?(String) && !raw_value.match?(/^\d{4}-\d{2}-\d{2}$/)
+ errors.add(attr, :invalid)
+ end
+ end
+ end
+
def calculate_duration
return nil unless date_range_set?
diff --git a/spec/models/project/phase_spec.rb b/spec/models/project/phase_spec.rb
index c380dfe1d4e..2559d4a87b0 100644
--- a/spec/models/project/phase_spec.rb
+++ b/spec/models/project/phase_spec.rb
@@ -86,6 +86,38 @@ RSpec.describe Project::Phase do
subject.start_date = subject.finish_date
expect(subject).to be_valid
end
+
+ describe "date format validation" do
+ it "is valid with correct YYYY-MM-DD format" do
+ subject.start_date = "2025-06-17"
+ subject.finish_date = "2025-06-18"
+ expect(subject).to be_valid
+ end
+
+ it "is invalid with extra characters in date" do
+ subject.start_date = "2025-06-170"
+ expect(subject).not_to be_valid
+ expect(subject.errors.symbols_for(:start_date)).to include(:invalid)
+ end
+
+ it "is invalid with non-date string" do
+ subject.finish_date = "not-a-date"
+ expect(subject).not_to be_valid
+ expect(subject.errors.symbols_for(:finish_date)).to include(:invalid)
+ end
+
+ it "is invalid with wrong format (missing leading zero)" do
+ subject.start_date = "2025-6-17"
+ expect(subject).not_to be_valid
+ expect(subject.errors.symbols_for(:start_date)).to include(:invalid)
+ end
+
+ it "is valid when date is blank" do
+ subject.start_date = ""
+ subject.finish_date = nil
+ expect(subject).to be_valid
+ end
+ end
end
describe "duration calculation" do