-
Notifications
You must be signed in to change notification settings - Fork 4
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
Skip form validation rules if a field is empty #191
Conversation
formidable/forms/__init__.py
Outdated
@@ -50,10 +50,27 @@ def __init__(self, *args, **kwargs): | |||
super(BaseDynamicForm, self).__init__(*args, **kwargs) | |||
self._bound_fields_cache = FormidableBoundFieldCache() | |||
|
|||
def rule_has_empty_fields(self, rule, data): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not put this kind of check in the rule itself ? Maybe for specific needed the rules can override its method in order to configure a specific check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sometimes you don't want the rule to be aware of the form/fields and sometimes you want :)
I can move that code in the Presets class but it will change the run method signature
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. Sorry.
(I had a previous implementation in mind...)
formidable/forms/__init__.py
Outdated
return any(( | ||
is_empty_value(data.get(name, None)) | ||
for name in used_fields | ||
)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: you don't need double-parenthesis here 😉
return any(
is_empty_value(data.get(name, None))
for name in used_fields
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, thanks 👍
formidable = TestPresets.to_formidable(label='presets') | ||
form_class = formidable.get_django_form_class(role='jedi') | ||
form = form_class(data={'left': '', 'right': ''}) | ||
self.assertFalse(form.is_valid(), form.errors) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand the test ... The field are required so the form validation will be false.
I would like to see a test like that
=> Define a Validation Between two fields required for the Jedi
=> Get the dynamic_form_class for a padawan
=> Fill the form with empty value
=> validate the form (it has to be okay).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some assertions in this test and a new test that matches your description
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool! LGTM
ad3988e
to
82080b1
Compare
As a field is not mandatory
If empty, it should be accepted