Skip Navigation

[Resolved] Validation unique field Email in database.

This support ticket is created 7 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 1 reply, has 2 voices.

Last updated by Luo Yang 7 years ago.

Assisted by: Luo Yang.

Author
Posts
#598165

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!

#598268

Dear Simone,

Yes, you are right, since you are going to setup custom validation on the custom email field "e-mail-candidato", so in your PHP codes you will need to change it the PHP codes to be the same, I assume the email field "e-mail-candidato" is created with Types plugin, Types will pre-pend "wpcf-" before the field slug, so in database, the meta_key is "wpcf-e-mail-candidato", and you will need to replace those test in your PHP codes from:
wpcf-e-mail

To:
wpcf-e-mail-candidato