Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionals mark fields as optional (#1799) #1808

Merged
merged 2 commits into from
Oct 28, 2020

Conversation

jmeinerz
Copy link
Contributor

We were assuming that all fields were required when a presence
validation existed. While that makes sense, it's also possible for
validations to be conditional.

Take the following validation as an example:

validates :phone_number, presence: true, if: :egyptian?

Before this commit, the UI would flag phone_number as required, even for
records who were not egyptian.

We now always flag these fields as optional. This is a bit misleading
too, but it's impossible to know these things when the page is rendered,
and marking them as optional makes for a slightly better user interface,
as the user will most likely be prompted with detailed validation errors
after trying to persist an invalid item, rather than be led to fill some
fields that are, in fact, not mandatory.

Given that application helpers are available throughout the app and the
highly specific nature of some of the actions required to determine
whether or not a field is required, moving that logic to Field::Base
feels more appropriate.

This also allows us to add more complex checks to determine whether or
not a field is required without exposing that logic everywhere in the
application.
@jmeinerz jmeinerz self-assigned this Oct 23, 2020
Copy link
Member

@nickcharlton nickcharlton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Do you think it'd be worth having a feature spec here, or this enough to show the behaviour?

@jmeinerz
Copy link
Contributor Author

@nickcharlton I think the feature spec is a good call 👍 I'll add it as soon as I find some time. Thanks!

@jmeinerz jmeinerz force-pushed the jm-conditionalrequirement branch from 6e94a04 to 9e73c51 Compare October 28, 2020 13:27
We were assuming that all fields were required when a presence
validation existed. While that makes sense, it's also possible for
validations to be conditional.

Take the following validation as an example:

        validates :phone_number, presence: true, if: :egyptian?

Before this commit, the UI would flag phone_number as required, even for
records who were not egyptian.

We now always flag these fields as optional. This is a bit misleading
too, but it's impossible to know these things when the page is rendered,
and marking them as optional makes for a slightly better user interface,
as the user will most likely be prompted with detailed validation errors
after trying to persist an invalid item, rather than be led to fill some
fields that are, in fact, not mandatory.
@jmeinerz jmeinerz force-pushed the jm-conditionalrequirement branch from 9e73c51 to f5790b4 Compare October 28, 2020 13:29
@jmeinerz jmeinerz merged commit 64aaa98 into master Oct 28, 2020
@jmeinerz jmeinerz deleted the jm-conditionalrequirement branch October 28, 2020 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants