Support

Account

Home Forums Front-end Issues Using acf/validate_value on front-end form

Unread

Using acf/validate_value on front-end form

  • Hello,
    I made a front-end form with ACF, and I needed to add a field manually, but I can’t figure out how to validate this field, my page code is:

    <form action="<?php the_permalink(); ?>" method="post" accept-charset="UTF-8">
    	
    	<label for="message">message</label>
    	<input type="text" name="message" value="<?php echo esc_attr($_POST['message']); ?>">
    	
    	<?php acf_form(array(
    		'form' => false
    	)); ?>
    	
    	<input type="submit" name="submitted" value="Send">
    
    </form>

    and my function.php:

    add_filter('acf/validate_value/name=message', 'my_acf_validate_value', 9, 4);
    function my_acf_validate_value( $valid, $value, $field, $input ){
    	if( !$valid ) {
    		return $valid;
    	}
    	if ( empty($_POST['message']) ) {
    		$valid = 'error';
    	}
    	return $valid;
    }
    add_action('acf/save_post', 'my_save_post');
    function my_save_post() {
    	if( is_admin() ) {
    		return;
    	}
    	// get custom fields (field group exists for content_form)
    	$name = get_field('name');
    	$email = get_field('email');
    	// email data
    	$to = 'contact@website.com';
    	$headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n";
    	$subject = 'Form';
    	$message = $_POST['message'];
    	// send email
    	wp_mail($to, $subject, $message, $headers );
    }

    I guess it’s because this validation works only for the ACF fields ? But there is maybe a way to make it works, if you have any idea, would appreciate it a lot!
    Thanks

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.