Home › Forums › Front-end Issues › Frontend form – required fields
Hi Elliot,
I did a frontend form thank to your tutorial, but some of my fields are required and ACF doesn’t check those.
Also I have some other fields like the title, the content and some taxonomies (that aren’t from ACF) which I would like to have required but it doesn’t work either.
How could I do that?
Here is my current code :
In functions.php
function my_pre_save_post($post_id) {
if($post_id != 'new') {
return $post_id;
}
if($_POST['cpt'] == 'projets') :
// On vérifie que tous les champs obligatoires sont bien remplis
if(
empty($_POST['post_titre']) ||
empty($_POST['post_content']) ||
empty($_POST['formation']) ||
empty($_POST['specialite']) ||
empty($_POST['promotion'])
) {
return false;
echo 'pas booon';
} else {
//$postTitle = trim($_POST['postTitle']);
$post_information = array(
'post_title' => esc_attr(strip_tags($_POST['post_title'])),
'post_content' => esc_attr(strip_tags($_POST['post_content'])),
'post_type' => $_POST['cpt'],
'post_status' => 'pending',
'tax_input' => array(
'formation' => array($_POST['formation']),
'specialite'=> array($_POST['specialite']),
'promotion' => array($_POST['date'])
)
);
}
endif;
$post_id = wp_insert_post($post_information);
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return']);
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
function my_acf_save_post($post_id) {
if (get_post_type($post_id) == 'acf' ) return;
$fields = get_field_objects($post_id);
remove_action( 'acf/save_post', 'my_acf_save_post' );
// Grab Post Data from the Form
$post = array(
'ID' => $post_id,
'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
'post_content' => esc_attr(strip_tags($_POST['postContent'])),
'post_type' => 'projets',
'post_status' => 'pending',
'tax_input' => array(
'formation' => array($_POST['formation']),
'catégorie' => array($_POST['categorie']),
'promotion' => array($_POST['date'])
)
);
// Update the Post
wp_update_post( $post );
// Continue save action
add_action( 'acf/save_post', 'my_save_post' );
// Set the Return URL in Case of 'new' Post
$_POST['return'] = add_query_arg( 'updated', 'true');
}
add_action('acf/save_post', 'my_acf_save_post', 10, 1);
and here is the form :
<?php /* Template name: Mon espace - Ajout projet */
acf_form_head();
get_header();
if (is_user_logged_in()):
$args = array(
'post_id' => 'new',
'field_groups' => array(25),
'form' => false,
//'return' => add_query_arg( 'ajout', 'true', get_permalink() ),
'html_before_fields' => '',
'html_after_fields' => '',
'submit_value' => 'Ajouter un projet',
'updated_message' => false
);
?>
<div class="<?php echo $post->post_name; ?>">
<?php if(isset($_GET['updated'])) :
if($_GET['updated'] == 'true') : ?>
<div class="alerte valider">
Merci pour votre projet ! Il sera vérifié rapidement par nos modérateurs.
</div>
<?php else : ?>
<div class="alerte erreur">
Il y a eu une erreur, merci de réessayer.
</div>
<?php endif; ?>
<?php else : ?>
<form action="#" method="post" class="custom">
<div class="field">
<label for="post_titre">Titre *</label>
<input type="text" name="post_titre" id="post_titre" value="<?php if(isset($_POST['post_titre'])) echo $_POST['post_titre'];?>" class="required" />
</div>
<div class="field">
<label for="post_content">Description *</label>
<textarea name="post_content" id="post_content" rows="8" cols="30"><?php if(isset($_POST['post_content'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['post_content']); } else { echo $_POST['post_content']; } } ?></textarea>
</div>
<div class="field">
<div class="one_third">
<label for="formation">Formation *</label>
<select name="formation">
<?php $formations = get_terms('formation', array('hide_empty'=>0));
foreach($formations as $formation) : ?>
<option value="<?php echo $formation->term_id; ?>"><?php echo $formation->name; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="one_third">
<label for="specialite">Spécialité *</label>
<select name="specialite">
<?php $specialites = get_terms('specialite', array('hide_empty'=>0));
foreach($specialites as $specialite) : ?>
<option value="<?php echo $specialite->term_id; ?>"><?php echo $specialite->name; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="one_third">
<label for="promotion">Promotion *</label>
<select name="promotion">
<?php $promotions = get_terms('promotion', array('hide_empty'=>0));
foreach($promotions as $promotion) : ?>
<option value="<?php echo $promotion->term_id; ?>"><?php echo $promotion->name; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="clear"></div>
</div>
<input type="hidden" name="cpt" value="projets" />
<?php acf_form($args); ?>
<input type="submit" value="Ajouter un projet" class="submit" />
</form>
Thanks 🙂
Hi @charlotte83
The form should sue JS to validate the fields. If this is failing, perhaps you could check your console log for any JS errors?
Thanks
E
Hi Elliot,
I don’t have any JS error in the console, and even with WP_DEBUG true and I deactivated all the other plugins too.
Is there any way to add a PHP check in the function my_pre_save_post and my_acf_save_post (the functions of the frontend from tutorial)?
Thanks
Charlotte
Hi @charlotte83
Currently, there is no way to prevent the saving of a post. ACF is not yet setup for front end functionality, however, this is something I make sure is built into v5
Thanks
E
Hi @elliot,
OK that is perfect. I will try to make some JS verification right now and I’m waiting for the 5 then 🙂
Thanks for your amazing plugin, I really love it and I’m so glad that not only it is free (thought I bought the repeater add-on which is awesome as well) but you’re also doing support … That’s really kind of you 🙂
The topic ‘Frontend form – required fields’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.