uf.field_interactions#
Field dependencies and interactions for uf.
Provides tools for defining relationships between form fields, such as conditional display, dynamic validation, and field dependencies.
Functions
|
Augment RJSF spec with field dependencies. |
|
Get field dependencies from a function. |
|
Decorator to attach field dependencies to a function. |
Classes
|
Actions that can be triggered by field dependencies. |
Builder for creating field dependencies. |
|
|
Define a dependency between form fields. |
- class uf.field_interactions.DependencyAction(*values)[source]#
Bases:
EnumActions that can be triggered by field dependencies.
- class uf.field_interactions.DependencyBuilder[source]#
Bases:
objectBuilder for creating field dependencies.
Provides a fluent interface for defining dependencies between fields.
Example
>>> builder = DependencyBuilder() >>> builder.when('reason').equals('other').show('other_reason') >>> builder.when('age').greater_than(18).enable('alcohol_consent') >>> dependencies = builder.build()
- build()[source]#
Build and return the list of dependencies.
- Return type:
- Returns:
List of FieldDependency objects
- custom(condition)[source]#
Condition: custom callable.
- disable(target_field)[source]#
Action: disable target field.
- Parameters:
target_field (
str) – Name of field to disable- Return type:
- Returns:
Self for method chaining
- enable(target_field)[source]#
Action: enable target field.
- Parameters:
target_field (
str) – Name of field to enable- Return type:
- Returns:
Self for method chaining
- equals(value)[source]#
Condition: field equals value.
- Parameters:
value (
Any) – Value to compare against- Return type:
- Returns:
Self for method chaining
- greater_than(value)[source]#
Condition: field is greater than value.
- Parameters:
value (
Any) – Value to compare against- Return type:
- Returns:
Self for method chaining
- hide(target_field)[source]#
Action: hide target field.
- Parameters:
target_field (
str) – Name of field to hide- Return type:
- Returns:
Self for method chaining
- in_list(values)[source]#
Condition: field value is in list.
- Parameters:
values (
list) – List of acceptable values- Return type:
- Returns:
Self for method chaining
- less_than(value)[source]#
Condition: field is less than value.
- Parameters:
value (
Any) – Value to compare against- Return type:
- Returns:
Self for method chaining
- make_optional(target_field)[source]#
Action: make target field optional.
- Parameters:
target_field (
str) – Name of field to make optional- Return type:
- Returns:
Self for method chaining
- not_equals(value)[source]#
Condition: field does not equal value.
- Parameters:
value (
Any) – Value to compare against- Return type:
- Returns:
Self for method chaining
- require(target_field)[source]#
Action: make target field required.
- Parameters:
target_field (
str) – Name of field to require- Return type:
- Returns:
Self for method chaining
- show(target_field)[source]#
Action: show target field.
- Parameters:
target_field (
str) – Name of field to show- Return type:
- Returns:
Self for method chaining
- class uf.field_interactions.FieldDependency(source_field, target_field, condition, action, else_action=None)[source]#
Bases:
objectDefine a dependency between form fields.
- source_field#
Name of the field that triggers the dependency
- target_field#
Name of the field affected by the dependency
- condition#
Function that takes source value and returns bool
- action#
Action to perform when condition is true
- else_action#
Optional action when condition is false
Example
>>> # Show 'other_reason' only when reason is 'other' >>> dep = FieldDependency( ... source_field='reason', ... target_field='other_reason', ... condition=lambda v: v == 'other', ... action=DependencyAction.SHOW, ... else_action=DependencyAction.HIDE ... )
- check(value)[source]#
Check the condition and return the appropriate action.
- Parameters:
value (
Any) – Value of the source field- Return type:
- Returns:
The action to perform
- uf.field_interactions.add_field_dependencies(func, dependencies)[source]#
Augment RJSF spec with field dependencies.
- Parameters:
func (
Callable) – Function to augmentdependencies (
list[FieldDependency]) – List of field dependencies
- Return type:
- Returns:
Dictionary with augmented schema and uiSchema
Example
>>> deps = [ ... FieldDependency('reason', 'other_reason', ... lambda v: v == 'other', ... DependencyAction.SHOW) ... ] >>> spec = add_field_dependencies(my_func, deps)
- uf.field_interactions.get_field_dependencies(func)[source]#
Get field dependencies from a function.
- Parameters:
func (
Callable) – Function to get dependencies from- Return type:
- Returns:
List of FieldDependency objects
- uf.field_interactions.with_dependencies(*dependencies)[source]#
Decorator to attach field dependencies to a function.
- Parameters:
*dependencies (
FieldDependency) – FieldDependency objects- Returns:
Decorator function
Example
>>> @with_dependencies( ... FieldDependency('reason', 'other_reason', ... lambda v: v == 'other', ... DependencyAction.SHOW) ... ) ... def submit_feedback(reason: str, other_reason: str = ''): ... pass