If you come across a situation where you need each form entry to submit a unique value — e.g; one entry per email address — you may use the af/form/before_submission hook.
To do so, you need to look up existing entries for the given form with a value matching the submitted value for a given field. If an entry is found, simply add a submission error to Advanced Forms using the af_add_submission_error() function. The form won't submit and the error message will be displayed to the user.
// Prevent a form from submitting if there is already a form entry containing the same value in a specific field. add_action( 'af/form/before_submission', function ( $form, $fields, $args ) { // Configure these accordingly. $form_key = 'YOUR_FORM_KEY_HERE'; $field_name = 'email'; // Only run on a specific form. if ( $form['key'] !== $form_key ) { return; } // Get the value from the submission to compare against. $submitted_value = af_get_field( $field_name ); // See if we already have an entry for the given form from the email address. $entries = get_posts( [ 'post_type' => 'af_entry', 'posts_per_page' => '-1', 'meta_query' => [ [ 'key' => 'entry_form', 'value' => $form_key, ], [ 'key' => $field_name, 'value' => $submitted_value, ] ], ] ); // If one or more entries are found matching the criteria, add a submission error. if ( ! empty( $entries ) ) { af_add_submission_error( "'$field_name' must be unique. There is already an entry with the value '$submitted_value'." ); } }, 10, 3 );
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article