public function onDispatchCustomFieldRule(mixed $field, mixed $value, array &$data) : bool

Fires before dispatching a custom field rule.


Description

Trigger hook to allow external plugins to dispatch a custom rule during the saving process.

This process is mainly used to collect the details specified by the customers into different database tables.

It is possible to access the rule of the field with $field->get('rule').

Here it is possible to throw an exception to safely abort the saving process and return an error message to the customer.


Parameters

$field

(VAPCustomField)  The custom field instance.

$value

(mixed)  The value of the field set in request.

&$data

(array)  The array data to fill-in in case of specific rules (name, e-mail, etc...).

Return Value

Boolean. True to avoid dispatching the default system rules.


Example

The example registers the specified country code into the customer database table.

/**
 * Trigger hook to allow external plugins to dispatch a custom rule.
 *
 * @param   mixed    $field  The custom field instance.
 * @param   mixed    $value  The value of the field set in request.
 * @param   array    &$data  The array data to fill-in in case of
 *                           specific rules (name, e-mail, etc...).
 *
 * @return  boolean  True to avoid dispatching the default system rules.
 */
public function onDispatchCustomFieldRule($field, $value, &$data)
{
    // make sure we are dispatching a field with "country" rule
    if ($field->get('rule') !== 'country')
    {
        // different rule, do nothing
        return false;
    }

    // register country code within the array data to be saved into the customer record
    $data['country_code'] = $value;

    return true;
}

Example #2

The example below explains how to extend the form validation via PHP for a field assigned to a specific rule.

public function onDispatchCustomFieldRule($field, $value, &$data)
{
    // make sure we are dispatching a field with "ticket" rule
    if ($field->get('rule') !== 'ticket')
    {
        // different rule, do nothing
        return false;
    }
	
    // validate the provided ticket via API to our own ticketing platform
    $response = (new JHttp)->get('https://your-ticket-platform.com/api/v1/ticket/' . $value);
  
    if ($response->code != 200)
    {
    	throw new Exception('The provided ticket does not exist.', 404);  
    }

    return true;
}

Changelog

Version Description
1.7 Introduced.
Last Update: 2021-10-08 12:53
Helpful?