Dynamic Forms are bundled with general hooks allowing you to change the form settings, add validation rules and trigger a custom action on submission. Those hooks are tied to forms, and act independently from Actions.
The acfe/form/load
fitler is used to change the form settings, right before it is rendered on the page. Note that fields inputs cannot be retrieved using get_field()
here, as the form hasn’t been submitted yet. Usage example:
/*
* Form Load
*
* @array $form Form settings
* @string $post_id Post ID (where the form is displayed)
*/
filter('acfe/form/load', $form, $post_id);
filter('acfe/form/load/form=my-form', $form, $post_id);
add_filter('acfe/form/load/form=my-form', 'my_form_load', 10, 2);
function my_form_load($form, $post_id){
// Change form success message dynamically
$form['updated_message'] = 'New success message!';
// Change form redirection URL
$form['return'] = '/thank-you';
// Hide the form
// return false;
// Return
return $form;
}
The acfe/form/validation
action is used to add rules during the ajax form validation process, before form submission.
Unlike the acf/validate_value
hook which validate each fields values individually, acfe/form/validation
will validate the whole form. It can be used to check one field against an another.
The acfe_add_validation_error()
function should be used to return an error. It is possible to return a general error by omitting to reference a field’s name. Example: acfe_add_validation_error('', 'General error message')
. Usage example:
/*
* Form Validation
*
* @array $form Form settings
* @string $post_id Post ID (where the form is displayed)
*/
action('acfe/form/validation', $form, $post_id);
action('acfe/form/validation/form=my-form', $form, $post_id);
add_action('acfe/form/validation/form=my-form', 'my_form_validation', 10, 2);
function my_form_validation($form, $post_id){
// Get field input value
$my_field = get_field('my_field');
if($my_field === 'Company'){
// Add validation error
acfe_add_validation_error('my_field', 'The value Company is not allowed');
}
}
The acfe/form/submit
action is used to trigger a custom script on form submission, after its validation. Usage example:
/*
* Form Submit
*
* @array $form Form settings
* @string $post_id Post ID (where the form is displayed)
*/
action('acfe/form/submit', $form, $post_id);
action('acfe/form/submit/form=my-form', $form, $post_id);
add_action('acfe/form/submit/form=my-form', 'my_form_submit', 10, 2);
function my_form_submit($form, $post_id){
// Get field input value
$my_field = get_field('my_field');
if($my_field === 'Company'){
// Do something...
}
}
acf.doAction('acfe/form/submit/success');
acf.doAction('acfe/form/submit/success/id=192');
acf.doAction('acfe/form/submit/success/name=my-form');
acf.doAction('acfe/form/submit/success/name=my-form', function(){
// do_something();
});