Hi all!
I have a followup question to How to validate an array property itself and each item of array?. The UI I’m showing is a registration form, where one can type the names of all family members. Some families have more people than others, and I provide more fields than necessary. For example, I show 5 fields for children, when I know most families won’t have 5 children. Same with adults, where I provide 2 fields, but some families will only have one person.
The registration form requires at least one adult, but the family is allowed to have no children.
What I’d like to happen is when the form is submitted, blanks should be skipped: ["", " ", "\t", " "]
should evaluate to []
. This is my ideal case.
My form definition is as follows.
NewFamilyForm = Dry::Validation.Form do
required(:email).filled(format?: /.@.+[.][a-z]{1}/i)
required(:name).filled(min_size?: 1)
required(:adult).filled(:array?, min_size?: 1).each do
none? | (str? & min_size?(1))
end
required(:child).each do
none? | (str? & min_size?(1))
end
end
When I call the form with no adults, I do not receive an error on the adult field, since the empty string is accepted. Here’s a failing test for me:
def test_requires_at_least_one_adult
result = NewFamilyForm.call("adult" => [""])
assert_equal ["must be filled"], result.errors[:adult], result.errors.inspect
assert_equal({adult: []}, result.output)
end
and running it:
3) Failure:
NewFamilyFormTest#test_requires_at_least_one_adult [test/test_new_family_form.rb:19]:
{:email=>["is missing"], :name=>["is missing"], :child=>["is missing"]}.
Expected: ["must be filled"]
Actual: nil
Is it possible to ignore blank values during validation? Ideally, #output
would not return blank strings, but I can live with those. Should I pre-process the form before attempting form validation?
Thanks!
François