Filters that allow you to modify field validation, sanitization, and custom server-side checks.
formforge_sanitize_field
Modify or override the sanitized value of a field before it is stored. Receives the sanitized value and the full field definition.
php
add_filter( 'formforge_sanitize_field', function( $value, $field ) {
// Force uppercase for a postal code field
if ( ( $field['id'] ?? '' ) === 'field_zipcode' ) {
return strtoupper( trim( $value ) );
}
return $value;
}, 10, 2 );Custom Server-Side Validation via formforge_before_submit
The formforge_before_submit action can also be used for custom validation. Call wp_send_json_error() to block the submission and show per-field errors.
php
add_action( 'formforge_before_submit', function( $form_id, $data, $form ) {
if ( $form_id !== 5 ) return;
$allowed_domains = [ 'company.com', 'partner.org' ];
foreach ( $data as $field_id => $field ) {
if ( ( $field['type'] ?? '' ) !== 'email' ) continue;
$domain = substr( strrchr( $field['value'], '@' ), 1 );
if ( ! in_array( $domain, $allowed_domains, true ) ) {
wp_send_json_error( [
'message' => 'Only company email addresses are accepted.',
'errors' => [ $field_id => 'Please use your company email.' ],
] );
}
}
}, 10, 3 );php
add_action( 'formforge_before_submit', function( $form_id, $data, $form ) {
foreach ( $data as $field_id => $field ) {
if ( ( $field['type'] ?? '' ) !== 'phone' ) continue;
$phone = preg_replace( '/[^0-9+]/', '', $field['value'] );
if ( strlen( $phone ) < 10 || strlen( $phone ) > 15 ) {
wp_send_json_error( [
'message' => 'Please provide a valid phone number.',
'errors' => [ $field_id => 'Phone must be 10-15 digits.' ],
] );
}
}
}, 10, 3 );Built-in Validation Pipeline
| Validation | Applies To | Rule |
|---|---|---|
| Required check | All | Field must have a non-empty value |
| Email format | email | is_email() WordPress function |
| Phone format | phone | Regex: ^[+ds-()]{7,20}$ |
| URL format | url | filter_var with FILTER_VALIDATE_URL |
| Number bounds | number, range | min / max comparison |
| Character length | text, textarea | min_length / max_length |
| Custom regex | text | User-defined pattern with custom error message |
| File type | file_upload | MIME type whitelist |
| File size | file_upload | 10 MB maximum |
—