# FirstSpirit Rules and Dynamic Forms ## Overview FirstSpirit rules enable template developers to create dynamic forms that automatically adjust based on user input and content state. These rules function seamlessly across both SiteArchitect and ContentCreator environments, providing real-time form manipulation capabilities. ### Key Capabilities Rules allow developers to: 1. **Validate form content** - Check conditions such as whether dates fall within required timeframes, validate field lengths, or ensure required fields contain appropriate data 2. **Control field visibility** - Dynamically hide or show form elements based on specific conditions or user selections 3. **Establish relationships** - Create dependencies where one field's value affects others (e.g., supplier selection filtering available product options) 4. **Manipulate form properties** - Modify editability, focus, selections, and other component properties dynamically ### Execution Timing Rules analyze form states at three critical stages: - **During editorial input** - Real-time evaluation as users interact with the form - **Upon saving** - Validation before committing changes to the database - **When entering edit mode or creating new elements** - Initial form setup and configuration ### Rule Violations When rules are violated, the system displays: - Color-coded messages tied to restriction levels - Explanatory messages in the user's language - Immediate feedback without disrupting normal workspace layout ### Important Security Note **Using rules to display and hide form elements is not a security model.** Rules assist workflow usability rather than protecting sensitive data from unauthorized access. Do not rely on rules for security-sensitive operations. ### Version Compatibility FirstSpirit 5.2 introduced modified rule syntax, though earlier versions remain compatible during a transition period. --- ## The Rules Tab ### Purpose The Rules tab within the Template Store enables template developers to define rules that influence form elements and properties dynamically. ### Available Template Types The Rules tab can be configured in: - Page templates - Section templates - Format templates (style templates only) - Link templates - Table templates - Scripts - Workflows ### Section Template Limitation Rules defined in section templates only affect forms directly based on that template. **Section references do not trigger rule violations**, meaning rules have no impact on referenced sections. ### Developer Assistance The platform provides code completion functionality on the Rules tab, displaying: - Available tags in the current context - Valid parameters for each tag - Possible values for attributes This assists developers in writing syntactically correct rules. --- ## Rule Structure FirstSpirit rules consist of five main components, defined within `` tags. ### 1. Execution Time Rules execute by default when forms are edited. The optional `when` attribute restricts execution to specific events: - **ONSAVE** - Executes during saving operations - **ONLOCK** - Executes when entering edit mode or creating new items #### Example ```xml ``` ### 2. Preconditions (Optional) An `` section defines conditions that must be met before the rule executes. These check form properties and are connected using logical tags. #### Example ```xml ``` ### 3. Value Determination The rule evaluates form or element properties using: - **Synchronous checks** - Using `` tags for immediate evaluation - **Asynchronous execution** - Using `` tags for deferred evaluation Values are compared against constants or other form properties. #### Example ```xml ``` ### 4. Handling Instructions (Required) A **required** `` section specifies actions when conditions are met, such as: - Modifying input component properties - Displaying correction messages - Changing form element visibility - Manipulating selection lists #### Example ```xml ``` ### 5. Validation (Optional) A `` section executes when value determination returns FALSE, assigning actions to specific input components with three restriction levels: - **SAVE** - Prevents saving (highest severity) - **RELEASE** - Prevents releasing - **INFO** - Default informational level (lowest severity) #### Example ```xml ``` ### Minimum Requirements Each rule must contain at least: 1. A value determination section (`` or ``) 2. A handling instruction section (``) --- ## Validation ### Core Concept Validation is "a certain handling instruction that is executed as long as conditions defined within the `` section are not fulfilled." ### Key Characteristics - Validations are **optional** within rules - Must be assigned to a specific form input component - Uses an internal `` tag with the component name and `VALID` attribute - Multiple validations can exist in a single `` section for different components ### Message Display When a `` tag follows the `` tag, the corresponding message displays below the input component. #### Example ```xml ``` ### Negation with NOT Boolean results can be negated using the `` tag to invert validation logic. #### Example ```xml ``` ### Practical Validation Scenarios #### Combined Field Checks Multiple input fields can be validated together, where at least one must contain data before saving. ```xml ``` #### Multi-Rule Validation A single component can be validated across separate rules (e.g., character limit in one rule, number restrictions in another). Each rule independently affects validity - if any rule marks it invalid, the component becomes invalid overall. ```xml ``` ### Default Values Behavior Rules preventing form saves are suspended when applying default or fallback values through the template store, allowing pre-assigned values to be saved without triggering validation errors. --- ## Form Properties ### Overview The `` tag defines form element properties across rule definitions. It serves two purposes: 1. Determining values or properties (in preconditions and value determination) 2. Performing actions on form elements (in handling instructions) ### Required Attributes - **source** - Specifies the form element (component name, design component, or system object) - **name** - Specifies which property to access ### Source Types #### 1. Input Component Access specific component properties like stored values: ```xml ``` #### 2. Design Component Access non-value elements like groups or labels using the `#form.` prefix: ```xml ``` #### 3. General Form Information Access form-level data using the `#global` source: ```xml ``` ### Available Properties by Context #### For Input Components **Preconditions/Value Determination:** - CONTAINERTYPE - DEFAULT - EDITABLE - EMPTY - ENTRY - FOCUS - LABEL - LENGTH - QUERY.* - SECTION - SIZE - VALUE - VISIBLE - VALID **Handling Instructions:** - ADD - COPY - DESELECT - EDIT - EDITABLE - EMPTY - NEW - REMOVE - SELECT - VALUE - VISIBLE **Validation:** - VALID #### For Design Components **Preconditions/Value Determination:** - FOCUS (returns FALSE) - LABEL - VISIBLE **Handling Instructions:** - VISIBLE #### For General Form Information (#global) **Preconditions/Value Determination:** - BODY - ELEMENTTYPE - GID - ID - INCLUDED - LANG - MASTER - PRESET - STORETYPE - TEMPLATE - TRANSLATED - UID - WEB ### Key Property Functions | Property | Function | Example | |----------|----------|---------| | VALUE | Returns or sets the value of an input component | `` | | VISIBLE | Controls visibility of components | `` | | EMPTY | Checks or sets blank values | `` | | EDITABLE | Defines editability | `` | | FOCUS | Returns Boolean focus state | `` | | SELECT | Manipulates selection in lists | `` | | DESELECT | Removes selection in lists | `` | | LENGTH | Returns character count | `` | | VALID | Sets validation state | `` | --- ## Practical Examples and Best Practices ### Example 1: Conditional Field Visibility Show additional fields only when a checkbox is selected: ```xml ``` ### Example 2: Dependent Dropdown Lists Filter product options based on selected category: ```xml ``` ### Example 3: Date Range Validation Ensure end date is after start date: ```xml ``` ### Example 4: Required Field Groups Make fields required only when a certain option is selected: ```xml ``` ### Example 5: Character Length Validation Enforce minimum and maximum character limits: ```xml ``` ### Example 6: Making Fields Read-Only Based on Conditions Lock fields after a certain workflow stage: ```xml ``` --- ## Best Practices ### 1. Use Descriptive Component Names Name your form components clearly so rules are easier to understand and maintain: ```xml ``` ### 2. Provide Multi-Language Messages Always include messages in all supported languages: ```xml ``` ### 3. Use Appropriate Validation Severity Choose the right severity level for your validation: - **SAVE** - Use for critical validations that must pass before saving - **RELEASE** - Use for validations that should pass before releasing to production - **INFO** - Use for helpful suggestions that don't block saving ### 4. Combine Rules Logically Break complex logic into multiple rules for better maintainability: ```xml ``` ### 5. Test Rules Thoroughly Test rules in different scenarios: - Creating new content - Editing existing content - Saving content - Releasing content - Different language contexts - Different user permissions ### 6. Consider Performance - Avoid overly complex rules that might slow down form interactions - Use `when="ONSAVE"` or `when="ONLOCK"` to limit execution frequency when appropriate - Be cautious with asynchronous `` operations ### 7. Document Complex Rules Add comments to complex rule logic: ```xml ... ``` ### 8. Remember Security Limitations Do not rely on rules for security: - Rules only control UI behavior - They do not prevent direct API access - Use proper permissions and backend validation for security ### 9. Handle Edge Cases Consider what happens when: - Fields are empty - Users switch languages - Content is in different workflow states - Multiple users edit simultaneously ### 10. Use Logical Operators Effectively Combine conditions using ``, ``, and `` for precise control: ```xml ``` --- ## Summary FirstSpirit rules provide powerful capabilities for creating dynamic, user-friendly forms that adapt to content and user interactions. By understanding rule structure, validation mechanisms, and form properties, developers can create sophisticated form behaviors that guide editors and ensure content quality. Remember that rules enhance usability but should not be relied upon for security, and always test rules thoroughly across different scenarios and languages.