I've opened ticket about this problem but after I didn't resolve it.
I created email post field but it must be unique into db. Can I validate it?
I want that if I write an existent email it return with error: "This email exist, try again."
- I've created the post type called 'coppie candidate' with slug 'coppia-candidata' and a form called
- the name is 'e-mail' (not string but email field).
I hope you know!
I paste you html module with shortcodes:
[credform class='cred-form cred-keep-original']
[cred_field field='form_messages' value='' class='alert alert-warning']
<div class="form-group">
<label>Nome 1</label>
[cred_field field='nome-1' post='coppia-candidata' value='' urlparam='' class='form-control' output='bootstrap']
</div>
<div class="form-group">
<label>Cognome 1</label>
[cred_field field='cognome-1' post='coppia-candidata' value='' urlparam='' class='form-control' output='bootstrap']
</div>
<div class="form-group">
<label>Nome 2</label>
[cred_field field='nome-2' post='coppia-candidata' value='' urlparam='' class='form-control' output='bootstrap']
</div>
<div class="form-group">
<label>Cognome 2</label>
[cred_field field='cognome-2' post='coppia-candidata' value='' urlparam='' class='form-control' output='bootstrap']
</div>
<div class="form-group">
<label>E-mail</label>
[cred_field field='e-mail' post='coppia-candidata' value='' urlparam='' class='form-control' output='bootstrap']
</div>
<div class="form-group">
<label>Foto</label>
[cred_field field='foto' post='coppia-candidata' value='' urlparam='' output='bootstrap']
</div>
<div class="form-group">
[cred_field field='accetto-il-regolamento' post='coppia-candidata' value='' urlparam='' output='bootstrap']
</div>
[cred_field field='form_submit' value='Invia' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']
[/credform]
---------------------
Nigel wrote me this:
add_filter('cred_form_validate', 'custom_validate_email');
function custom_validate_email( $data ) {
list($fields,$errors)=$data;
if ( !empty( $fields['wpcf-e-mail']['value'] ) ) {
$email = $fields['wpcf-e-mail']['value'];
// check if any posts already have this email registered
$args = array(
'post_type' => 'coppia-candidata',
'post_status' => 'publish',
'numberposts' => 1,
'meta_key' => 'wpcf-e-mail',
'meta_value' => $email
);
$matching = get_posts( $args );
if ( !empty( $matching ) ) {
$errors['wpcf-e-mail'] = 'Email already used';
}
}
return array($fields,$errors);
}
--------
Now I'm in this situation:
I paste it into function.php of my theme as usual but the form pass without problems and the post is created with email duplicated.
One question:
in $args you put:
$args = array(
'post_type' => 'coppia-candidata',
'post_status' => 'publish',
'numberposts' => 1,
'meta_key' => 'wpcf-e-mail',
'meta_value' => $email
);
where meta_key is' wpcf-e-mail'.
But I think that in my form the field is different. In the form the field is this:
........<div class="form-group">
<label>E-mail</label>
[cred_field field='e-mail-candidato' post='coppia-candidata' value='' urlparam='' class='form-control' output='bootstrap']
</div>.........
Must 'meta_key' in args and 'field' into form be the same?
Thank you!