Home Forums Front-end Issues Validation a front-end form


Validation a front-end form

  • I am trying to validate a front-end form created with ACF. There are two fields that the user can choose to fill out, but they have to fill out at least one of them. Usually, when I validate a form I just use the $_POST object and check it before saving the post to the database (or display an error) but with ACF the field names seem unconventional and I can’t quite figure out how to do it.

    The below code is what I’ve tried most recently. The check for the two fields always returns false, even if there something in at least one of them. I’ve also tried putting the slug of the custom fields with no luck.

    How do you suggest the best way to validate a front-end form is?

    //Create new Custom Design post from frontend
    function my_pre_save_post( $post_id )
        // check if this is to be a new post
        if( $post_id != 'new' )
            return $post_id;
    	if ( (isset( $_POST['field_5259b4ae85a65'] ) && !empty( $_POST['field_5259b4ae85a65'] )) 
    	(isset( $_POST['field_5259b4be85a66'] ) && !empty( $_POST['field_5259b4be85a66'] )) ){
    		// Create a new post
    		global $current_user;
    		  $title = $current_user->user_email.'-'.get_date();
    		$post = array(
    			'post_status'  => 'publish' ,
    			'post_title'  => $title,
    			'post_type'  => 'custom-designs' ,
    		// insert the post
    		$post_id = wp_insert_post( $post ); 
    		// update $_POST['return']
    		$_POST['return'] = add_query_arg( array('post_id' => $post_id, 'submitted' => 'true', 'custom-error' => false), $_POST['return'] );    
    		// return the new ID
    		return $post_id;
    	} else {
    		wp_redirect(add_query_arg(array('custom-error' => 'true'), $_POST['return'])); 
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
  • Hi @arcanepsyche

    The issue here is that you are assuming the wrong $_POST key.

    All acf fields are posted as an array at $_POST[‘fields’];

    Perhaps you should debug the $_POST array to see how the data is formatted.


  • Thanks! That makes sense now. For the sake of others, this is the working conditional statement I’m using:

    if ( (isset( $_POST['fields']['field_5259b4be85a66'] ) && !empty( $_POST['fields']['field_5259b4be85a66'] )) 
    	(isset( $_POST['fields']['field_5259b44f99143'] ) && !empty( $_POST['fields']['field_5259b44f99143'] )) ){
  • where did you find the fields object id (eg field_5259b4be85a66)


  • Sorry – I searched, I found, I was ashamed!

    Its under screen options for other dufuses

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Validation a front-end form’ is closed to new replies.