Create a custom behavior action
See the built-in reference for the definition fields and available built-ins.
Add an action that trims a draft value
Use the selected listener’s string value to update a text target. The layout supplies the listener and target fields.
python
from bloomerp.form_behaviors.definition import (
BehaviorActionDefinition,
BehaviorContext,
BehaviorResult,
BehaviorUser,
CleanedConfigData,
FieldValueUpdate,
)
from bloomerp.form_behaviors.registry import ACTION_REGISTRY
def trim_text(
context: BehaviorContext,
config: CleanedConfigData,
user: BehaviorUser,
) -> BehaviorResult:
'''Copy the trimmed listener value to the target field.'''
value = context.listener_value
if not isinstance(value, str):
return BehaviorResult()
return BehaviorResult(
values=(FieldValueUpdate(field=context.target_field, value=value.strip()),)
)
trim_action = BehaviorActionDefinition(
id="example_trim_draft_text",
label="Trim draft text",
description="Copy text to another field and trim surrounding whitespace.",
requires_target_field=True,
execute=trim_text,
)
ACTION_REGISTRY.register(trim_action.id, trim_action)Add an action that only shows a message
A message action can run when a rule matches without a separate target field.
python
from bloomerp.form_behaviors.definition import (
BehaviorActionDefinition,
BehaviorContext,
BehaviorMessage,
BehaviorResult,
BehaviorUser,
CleanedConfigData,
)
from bloomerp.form_behaviors.registry import ACTION_REGISTRY
def show_review_note(
context: BehaviorContext,
config: CleanedConfigData,
user: BehaviorUser,
) -> BehaviorResult:
'''Return a reminder for the user reviewing the draft.'''
return BehaviorResult(
messages=(BehaviorMessage(type="info", message="Review the details before saving."),)
)
review_note = BehaviorActionDefinition(
id="example_review_note",
label="Show review note",
description="Show a review reminder on the form.",
requires_target_field=False,
execute=show_review_note,
)
ACTION_REGISTRY.register(review_note.id, review_note)