-
Notifications
You must be signed in to change notification settings - Fork 5
Add conditional logic to Fields with tests #95
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
30d4dcc
Add conditional logic to Fields. Add backend/snapshot tests for field…
kbayliss 9842564
Also update CrispyGDSFieldNode
kbayliss b56f51f
Update changelog
kbayliss 08bc6b9
Remove attrs from widget and correctly use flat_attrs on the checkbox…
kbayliss 46245f2
Simplify
kbayliss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,27 @@ | ||
import json | ||
|
||
from crispy_forms import layout as crispy_forms_layout | ||
|
||
|
||
class Layout(crispy_forms_layout.Layout): | ||
pass | ||
|
||
|
||
def setup_conditional_attrs(kwargs: dict): | ||
""" | ||
Set up our flat attributes to handle conditional field/container logic. | ||
|
||
As an example, this will transform: | ||
{ "data_conditional": { "field_name": "trigger_field", "values": ["yes", "no"] } } | ||
to: | ||
{ "data_conditional_field_name": "trigger_field", "data_conditional_field_values": "[\"yes\", \"no\"]" } | ||
""" # noqa: E501 | ||
|
||
conditional_attrs = kwargs.pop("data_conditional", None) | ||
if conditional_attrs: | ||
kwargs["data_conditional_field_name"] = conditional_attrs["field_name"] | ||
kwargs["data_conditional_field_values"] = json.dumps( | ||
conditional_attrs["values"] | ||
) | ||
|
||
return kwargs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...ditionalFieldsToShowAsRequiredMethod.test_field_can_be_shown_as_required[checkboxes].html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<form class="tbxforms" method="post"> | ||
<div id="div_id_test_field" class="tbxforms-form-group"> | ||
<label for="id_test_field" class="tbxforms-label tbxforms-label--m">Test field</label> | ||
<select name="test_field" class="selectmultiple" id="id_test_field" multiple> | ||
<option value="yes">Yes</option> | ||
<option value="no">No</option> | ||
</select> | ||
</div> | ||
</form> |
9 changes: 9 additions & 0 deletions
9
...tConditionalFieldsToShowAsRequiredMethod.test_field_can_be_shown_as_required[radios].html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<form class="tbxforms" method="post"> | ||
<div id="div_id_test_field" class="tbxforms-form-group"> | ||
<label for="id_test_field" class="tbxforms-label tbxforms-label--m">Test field</label> | ||
<select name="test_field" class="tbxforms-select" id="id_test_field"> | ||
<option value="yes">Yes</option> | ||
<option value="no">No</option> | ||
</select> | ||
</div> | ||
</form> |
9 changes: 9 additions & 0 deletions
9
...tConditionalFieldsToShowAsRequiredMethod.test_field_can_be_shown_as_required[select].html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<form class="tbxforms" method="post"> | ||
<div id="div_id_test_field" class="tbxforms-form-group"> | ||
<label for="id_test_field" class="tbxforms-label tbxforms-label--m">Test field</label> | ||
<select name="test_field" class="tbxforms-select" id="id_test_field"> | ||
<option value="yes">Yes</option> | ||
<option value="no">No</option> | ||
</select> | ||
</div> | ||
</form> |
9 changes: 9 additions & 0 deletions
9
...estConditionalFieldsToShowAsRequiredMethod.test_field_can_be_shown_as_required[text].html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<form class="tbxforms" method="post"> | ||
<div id="div_id_test_field" class="tbxforms-form-group"> | ||
<label for="id_test_field" class="tbxforms-label tbxforms-label--m">Test field</label> | ||
<input type="text" | ||
name="test_field" | ||
class="tbxforms-input tbxforms-input--text" | ||
id="id_test_field"> | ||
</div> | ||
</form> |
10 changes: 10 additions & 0 deletions
10
...onditionalFieldsToShowAsRequiredMethod.test_field_can_be_shown_as_required[textarea].html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<form class="tbxforms" method="post"> | ||
<div id="div_id_test_field" class="tbxforms-form-group"> | ||
<label for="id_test_field" class="tbxforms-label tbxforms-label--m">Test field</label> | ||
<input type="text" | ||
name="test_field" | ||
rows="10" | ||
class="tbxforms-input tbxforms-input--text" | ||
id="id_test_field"> | ||
</div> | ||
</form> |
22 changes: 22 additions & 0 deletions
22
...st_conditional_attrs/TestContainerConditionals.test_container_conditional_attrs[Div].html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<form class="tbxforms" method="post"> | ||
<div class="tbxforms-form-group" | ||
data-conditional-field-name="trigger_field" | ||
data-conditional-field-values="["yes"]"> | ||
<div id="div_id_field1" class="tbxforms-form-group"> | ||
<label for="id_field1" class="tbxforms-label tbxforms-label--m">Field1</label> | ||
<input type="text" | ||
name="field1" | ||
class="tbxforms-input tbxforms-input--text" | ||
required="required" | ||
id="id_field1"> | ||
</div> | ||
<div id="div_id_field2" class="tbxforms-form-group"> | ||
<label for="id_field2" class="tbxforms-label tbxforms-label--m">Field2</label> | ||
<input type="text" | ||
name="field2" | ||
class="tbxforms-input tbxforms-input--text" | ||
required="required" | ||
id="id_field2"> | ||
</div> | ||
</div> | ||
</form> |
22 changes: 22 additions & 0 deletions
22
...nditional_attrs/TestContainerConditionals.test_container_conditional_attrs[Fieldset].html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<form class="tbxforms" method="post"> | ||
<fieldset class="tbxforms-form-group tbxforms-fieldset" | ||
data-conditional-field-name="trigger_field" | ||
data-conditional-field-values="["yes"]"> | ||
<div id="div_id_field1" class="tbxforms-form-group"> | ||
<label for="id_field1" class="tbxforms-label tbxforms-label--m">Field1</label> | ||
<input type="text" | ||
name="field1" | ||
class="tbxforms-input tbxforms-input--text" | ||
required="required" | ||
id="id_field1"> | ||
</div> | ||
<div id="div_id_field2" class="tbxforms-form-group"> | ||
<label for="id_field2" class="tbxforms-label tbxforms-label--m">Field2</label> | ||
<input type="text" | ||
name="field2" | ||
class="tbxforms-input tbxforms-input--text" | ||
required="required" | ||
id="id_field2"> | ||
</div> | ||
</fieldset> | ||
</form> |
14 changes: 14 additions & 0 deletions
14
...conditional_attrs/TestContainerConditionals.test_container_with_multiple_values[Div].html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<form class="tbxforms" method="post"> | ||
<div class="tbxforms-form-group" | ||
data-conditional-field-name="trigger_field" | ||
data-conditional-field-values="["yes", "maybe"]"> | ||
<div id="div_id_field1" class="tbxforms-form-group"> | ||
<label for="id_field1" class="tbxforms-label tbxforms-label--m">Field1</label> | ||
<input type="text" | ||
name="field1" | ||
class="tbxforms-input tbxforms-input--text" | ||
required="required" | ||
id="id_field1"> | ||
</div> | ||
</div> | ||
</form> |
14 changes: 14 additions & 0 deletions
14
...tional_attrs/TestContainerConditionals.test_container_with_multiple_values[Fieldset].html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<form class="tbxforms" method="post"> | ||
<fieldset class="tbxforms-form-group tbxforms-fieldset" | ||
data-conditional-field-name="trigger_field" | ||
data-conditional-field-values="["yes", "maybe"]"> | ||
<div id="div_id_field1" class="tbxforms-form-group"> | ||
<label for="id_field1" class="tbxforms-label tbxforms-label--m">Field1</label> | ||
<input type="text" | ||
name="field1" | ||
class="tbxforms-input tbxforms-input--text" | ||
required="required" | ||
id="id_field1"> | ||
</div> | ||
</fieldset> | ||
</form> |
29 changes: 29 additions & 0 deletions
29
...ots__/test_conditional_attrs/TestFieldConditionals.test_rendering[checkbox-checkbox].html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<form class="tbxforms" method="post"> | ||
<div id="div_id_trigger_field" class="tbxforms-form-group"> | ||
<div class="tbxforms-checkboxes"> | ||
<div class="tbxforms-checkboxes__item"> | ||
<input type="checkbox" | ||
name="trigger_field" | ||
class="tbxforms-checkboxes__input" | ||
required="required" | ||
id="id_trigger_field"> | ||
<label class="tbxforms-label tbxforms-checkboxes__label" | ||
for="id_trigger_field">Trigger field</label> | ||
</div> | ||
</div> | ||
</div> | ||
<div id="div_id_dependent_field" class="tbxforms-form-group"> | ||
<div class="tbxforms-checkboxes"> | ||
<div class="tbxforms-checkboxes__item"> | ||
<input type="checkbox" | ||
name="dependent_field" | ||
data-conditional-field-name="trigger_field" | ||
data-conditional-field-values="[true]" | ||
class="tbxforms-checkboxes__input" | ||
id="id_dependent_field"> | ||
<label class="tbxforms-label tbxforms-checkboxes__label" | ||
for="id_dependent_field">Dependent field</label> | ||
</div> | ||
</div> | ||
</div> | ||
</form> |
30 changes: 30 additions & 0 deletions
30
...s__/test_conditional_attrs/TestFieldConditionals.test_rendering[checkbox-checkboxes].html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<form class="tbxforms" method="post"> | ||
<div id="div_id_trigger_field" class="tbxforms-form-group"> | ||
<div class="tbxforms-checkboxes"> | ||
<div class="tbxforms-checkboxes__item"> | ||
<input type="checkbox" | ||
name="trigger_field" | ||
class="tbxforms-checkboxes__input" | ||
required="required" | ||
id="id_trigger_field"> | ||
<label class="tbxforms-label tbxforms-checkboxes__label" | ||
for="id_trigger_field">Trigger field</label> | ||
</div> | ||
</div> | ||
</div> | ||
<div id="div_id_dependent_field" class="tbxforms-form-group"> | ||
<label for="id_dependent_field" class="tbxforms-label tbxforms-label--m"> | ||
Dependent field | ||
<span class="tbxforms-field_marker--optional">(optional)</span> | ||
</label> | ||
<select name="dependent_field" | ||
data-conditional-field-name="trigger_field" | ||
data-conditional-field-values="[true]" | ||
class="selectmultiple" | ||
id="id_dependent_field" | ||
multiple> | ||
<option value="yes">Yes</option> | ||
<option value="no">No</option> | ||
</select> | ||
</div> | ||
</form> |
29 changes: 29 additions & 0 deletions
29
...shots__/test_conditional_attrs/TestFieldConditionals.test_rendering[checkbox-radios].html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<form class="tbxforms" method="post"> | ||
<div id="div_id_trigger_field" class="tbxforms-form-group"> | ||
<div class="tbxforms-checkboxes"> | ||
<div class="tbxforms-checkboxes__item"> | ||
<input type="checkbox" | ||
name="trigger_field" | ||
class="tbxforms-checkboxes__input" | ||
required="required" | ||
id="id_trigger_field"> | ||
<label class="tbxforms-label tbxforms-checkboxes__label" | ||
for="id_trigger_field">Trigger field</label> | ||
</div> | ||
</div> | ||
</div> | ||
<div id="div_id_dependent_field" class="tbxforms-form-group"> | ||
<label for="id_dependent_field" class="tbxforms-label tbxforms-label--m"> | ||
Dependent field | ||
<span class="tbxforms-field_marker--optional">(optional)</span> | ||
</label> | ||
<select name="dependent_field" | ||
data-conditional-field-name="trigger_field" | ||
data-conditional-field-values="[true]" | ||
class="tbxforms-select" | ||
id="id_dependent_field"> | ||
<option value="yes">Yes</option> | ||
<option value="no">No</option> | ||
</select> | ||
</div> | ||
</form> |
29 changes: 29 additions & 0 deletions
29
...shots__/test_conditional_attrs/TestFieldConditionals.test_rendering[checkbox-select].html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<form class="tbxforms" method="post"> | ||
<div id="div_id_trigger_field" class="tbxforms-form-group"> | ||
<div class="tbxforms-checkboxes"> | ||
<div class="tbxforms-checkboxes__item"> | ||
<input type="checkbox" | ||
name="trigger_field" | ||
class="tbxforms-checkboxes__input" | ||
required="required" | ||
id="id_trigger_field"> | ||
<label class="tbxforms-label tbxforms-checkboxes__label" | ||
for="id_trigger_field">Trigger field</label> | ||
</div> | ||
</div> | ||
</div> | ||
<div id="div_id_dependent_field" class="tbxforms-form-group"> | ||
<label for="id_dependent_field" class="tbxforms-label tbxforms-label--m"> | ||
Dependent field | ||
<span class="tbxforms-field_marker--optional">(optional)</span> | ||
</label> | ||
<select name="dependent_field" | ||
data-conditional-field-name="trigger_field" | ||
data-conditional-field-values="[true]" | ||
class="tbxforms-select" | ||
id="id_dependent_field"> | ||
<option value="yes">Yes</option> | ||
<option value="no">No</option> | ||
</select> | ||
</div> | ||
</form> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.