This rails validation code is so ugly, and there must be a better way to do it.
The short story is my user is inputting data from test results. Each specific test has 4 measurements. Usually the user inputs all 5 tests (20 measurements), but it's not always required. I just need to check that if a tester started inputting data for a test, he input all 4 measurements in that test.
(Later I will make a validation class, but I'm using this to get started)
Edit: To clarify, the 'tests' are measurements on materials (unrelated to software). the testers record the data on paper, and later it is input into this application
<pre>
def must_input_full_test
#test Top Section
if top_section_1 || top_section_2 || top_section_3 || top_section_4
found_at_least_one = true
#at least one is present; now check if any are empty
if top_section_1.nil?
errors.add :top_section_1, "Must fill in four values for a test"
end
if top_section_2.nil?
errors.add :top_section_2, "Must fill in four values for a test"
end
if top_section_3.nil?
errors.add :top_section_3, "Must fill in four values for a test"
end
if top_section_4.nil?
errors.add :top_section_4, "Must fill in four values for a test"
end
end
#test Bottom Section
if bottom_section_1 || bottom_section_2 || bottom_section_3 || bottom_section_4
found_at_least_one = true
#at least one is present; now check if any are empty
if bottom_section_1.nil?
errors.add :bottom_section_1, "Must fill in four values for a test"
end
if bottom_section_2.nil?
errors.add :bottom_section_2, "Must fill in four values for a test"
end
if bottom_section_3.nil?
errors.add :bottom_section_3, "Must fill in four values for a test"
end
if bottom_section_4.nil?
errors.add :bottom_section_4, "Must fill in four values for a test"
end
end
#test Bottom
if bottom_1 || bottom_2 || bottom_3 || bottom_4
found_at_least_one = true
#at least one is present; now check if any are empty
if bottom_1.nil?
errors.add :bottom_1, "Must fill in four values for a test"
end
if bottom_2.nil?
errors.add :bottom_2, "Must fill in four values for a test"
end
if bottom_3.nil?
errors.add :bottom_3, "Must fill in four values for a test"
end
if bottom_4.nil?
errors.add :bottom_4, "Must fill in four values for a test"
end
end
#test Middle Section
if middle_1 || middle_2 || middle_3 || middle_4
found_at_least_one = true
#at least one is present; now check if any are empty
if middle_1.nil?
errors.add :middle_1, "Must fill in four values for a test"
end
if middle_2.nil?
errors.add :middle_2, "Must fill in four values for a test"
end
if middle_3.nil?
errors.add :middle_3, "Must fill in four values for a test"
end
if middle_4.nil?
errors.add :middle_4, "Must fill in four values for a test"
end
end
#test Top
if top_1 || top_2 || top_3 || top_4
found_at_least_one = true
#at least one is present; now check if any are empty
if top_1.nil?
errors.add :top_1, "Must fill in four values for a test"
end
if top_2.nil?
errors.add :top_2, "Must fill in four values for a test"
end
if top_3.nil?
errors.add :top_3, "Must fill in four values for a test"
end
if top_4.nil?
errors.add :top_4, "Must fill in four values for a test"
end
end
if !found_at_least_one
errors.add :middle_1, "Must fill in at least some test data"
end
end
<code>