|
/ Documentation /Developer Hooks & Events/ suredonation_field_block_config Filter Hook

suredonation_field_block_config Filter Hook

Overview

The suredonation_field_block_config filter allows developers to customize the validation configuration stored for individual form blocks when a SureDonation form is saved.

Use this filter when you create a custom field block that requires validation rules that are not handled by SureDonation core.

The configuration saved through this filter can later be used by suredonation_validate_field to validate submitted values.

When to Use This Filter

Use this filter when you need to:

  • Add validation settings for a custom field block.
  • Store custom validation rules when a form is saved.
  • Define whether a custom field is required.
  • Store field-specific limits such as maximum length.
  • Provide configuration that will be used during server-side validation.

Filter Signature

apply_filters( 'suredonation_field_block_config', $processed_config, $block_name,   $block['attrs'], $blocks );

Parameters

ParameterTypeDescription
$processed_configarray|nullConfiguration generated by SureDonation core. null when the block type is not handled by core.
$block_namestringThe block name, such as suredonation/input.
$attrsarrayAttributes configured for the current block.
$blocksarrayAll blocks in the form being processed.

$processed_config

When SureDonation core recognizes the block, this parameter may already contain validation rules.

For an unsupported custom block, it can be: null

Your callback can return a custom configuration array for the block.

Return Value

Return an array containing the validation configuration you want to store.

return [
    'required' => true,
];

The configuration should contain at least a required value and any additional values required by your validator.

You can also return the original value:

return $processed_config;

If the returned configuration is null or empty, no configuration is stored for that block.

Basic Usage

add_filter( 'suredonation_field_block_config', 'your_custom_function', 10,  4);

function your_custom_function(

    $processed_config,

    $block_name,

    $attrs,

    $blocks

) {

    // Return a config array for your custom block

    // or return the original value.

    return $processed_config;

}

Example: Add Configuration for a Custom Field

The following example adds validation settings for a custom field named mysite/custom-field.

add_filter( 'suredonation_field_block_config',  'mysite_field_config', 10, 4);

function mysite_field_config(

    $processed_config,

    $block_name,

    $attrs,

    $blocks

) {

    if ( 'mysite/custom-field' === $block_name ) {

        return [

            'required'   => ! empty( $attrs['required'] ),

            'max_length' => (int) ( $attrs['maxLength'] ?? 0 ),

        ];

    }

    return $processed_config;

}

In this example:

  • required determines whether the field is mandatory.
  • max_length stores the maximum allowed length.
  • The configuration is saved with the form.

Validation Flow

This filter is part of the form validation configuration process:

  1. A SureDonation form is saved.
  2. SureDonation processes each block in the form.
  3. Core generates validation configuration for supported blocks.
  4. The suredonation_field_block_config filter runs.
  5. Your callback can add or modify the configuration.
  6. The configuration is stored against the block ID.
  7. When the form is submitted, the stored configuration is loaded.
  8. suredonation_validate_field uses the configuration to validate the submitted value.

Working With Custom Fields

If your plugin adds a custom field block, you can use this filter to store the validation rules for that block.

For example:

Custom Field Block

       â†“

suredonation_field_block_config

       â†“

Validation Configuration Saved

       â†“

Form Submission

       â†“

suredonation_validate_field

       â†“

Custom Validation

For the validation configuration to be used during submission, your custom block should also be registered with:

suredonation_validatable_blocks

Example: Store Multiple Validation Rules

You can store multiple values required by your custom validator.

add_filter(   'suredonation_field_block_config',  'mysite_custom_validation_config',   10,    4

);

function mysite_custom_validation_config(

    $processed_config,

    $block_name,

    $attrs,

    $blocks

) {

    if ( 'mysite/custom-field' !== $block_name ) {

        return $processed_config;

    }

    return [

        'required'   => ! empty( $attrs['required'] ),

        'max_length' => (int) ( $attrs['maxLength'] ?? 100 ),

        'prefix'     => sanitize_text_field( $attrs['prefix'] ?? '' ),

    ];

}

The stored configuration can then be read by your validation callback.

Important: Configuration vs Validation

This filter does not validate the submitted field value.

It only defines the configuration that is saved when the form is saved.

Use:

suredonation_validate_field

to apply validation rules to submitted values.

For example:

suredonation_field_block_config

        â†“

Stores validation rules

        â†“

suredonation_validate_field

        â†“

Validates submitted value

Related Hooks

  • Suredonation_validatable_blocks: Registers block types whose submitted values should be validated.
  • Suredonation_validate_field: Validates individual field values using the stored configuration.
Was this doc helpful?
What went wrong?

We don't respond to the article feedback, we use it to improve our support content.

Need help? Contact Support
Table of Contents
Scroll to Top