В примере кода, который вы увидите ниже, мы сначала проверяем идентификатор формы, чтобы убедиться, что он соответствует целевой форме. Затем мы проверяем указанное c поле (по идентификатору поля), чтобы увидеть, пусто ли оно.
Просто не забудьте изменить идентификатор формы с 5, чтобы соответствовать идентификатору формы, и изменить «4» на соответствует идентификатору поля.
function wpf_dev_process( $fields, $entry, $form_data ) {
// Optional, you can limit to specific forms. Below, we restrict output to
// form #5.
if ( absint( $form_data['id'] ) !== 5 ) {
return $fields;
}
// check the field ID 4 to see if it's empty and if it is, run the error
if(empty($fields[4]['value']))
{
// Add to global errors. This will stop form entry from being saved to the database.
// Uncomment the line below if you need to display the error above the form.
// wpforms()->process->errors[ $form_data['id'] ]['header'] = esc_html__( 'Some error occurred.', 'plugin-domain' );
// Check the field ID 4 and show error message at the top of form and under the specific field
wpforms()->process->errors[ $form_data['id'] ] [ '4' ] = esc_html__( 'Some error occurred.', 'plugin-domain' );
// Add additional logic (what to do if error is not displayed)
}
}
add_action( 'wpforms_process', 'wpf_dev_process', 10, 3 );