Skip to content

Commit

Permalink
Merge pull request #605 from iommirocks/required-lambda-on-field-broken
Browse files Browse the repository at this point in the history
  • Loading branch information
jlubcke authored Dec 12, 2024
2 parents 7832cce + 03108db commit 1ab1ecd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions iommi/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,8 @@ def on_bind(self) -> None:
# Not strict evaluate on purpose
self.model = evaluate(self.model, **self.iommi_evaluate_parameters())

self.required = evaluate_strict(self.required, **self.iommi_evaluate_parameters())

self.bind_from_instance()

bind_member(self, name='label')
Expand Down Expand Up @@ -999,6 +1001,8 @@ def _validate(self):
if self.parsed_data is not None:
value = self._validate_parsed_data(self.parsed_data)

assert not callable(self.required)

if not self._errors:
if (
form.mode is FULL_FORM_FROM_REQUEST
Expand Down
8 changes: 8 additions & 0 deletions iommi/form__tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3922,3 +3922,11 @@ class MyForm(Form):
foo = Template('supernaut')

assert 'supernaut' in MyForm().bind(request=req('get')).__html__()


def test_required_truthy_bug():
form = Form.create(auto__model=Album, fields__genres__required=lambda **_: False).bind(request=req('post', **{'-submit': ''}))

assert form.actions.submit.iommi_name() == 'submit'
assert 'genres' not in form.get_errors()['fields']
assert form.get_errors()['fields']['name'] == {'This field is required'}

0 comments on commit 1ab1ecd

Please sign in to comment.