Skip Navigation

[Resolved] I am using validate to check that a listing is not already on the database.

This support ticket is created 6 years, 3 months 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 8 replies, has 2 voices.

Last updated by Christian Cox 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1137397

I am trying to:
add_filter('cred_form_validate','func_validate_listing',10,2);
function func_validate_listing ($error_fields, $form_data){

//field data are field values and errors

list($fields,$errors)=$error_fields;

//uncomment this if you want to print the field values

//print_r($fields);

//validate if specific for

if ($form_data['id']==1126){

$args = array(
'meta_query' => array(
'relation' => 'AND',
array('key' => 'wpcf-field1',
'value' => $_POST['wpcf-field1']),
array('key' => 'wpcf-field2',
'value' => $_POST['wpcf-field2']),
array('key' => 'wpcf-field3',
'value' => $_POST['wpcf-field3']),
array('key' => 'wpcf-field4',
'value' => $_POST['wpcf-field4']),
array('key' => 'wpcf-field5t',
'value' => $_POST['wpcf-field5']),

),
'post_type' => 'upn',
'posts_per_page' => -1
);

$posts = get_posts($args);

//check if this unit is already on the database

if (count($posts) > 0){

//set error message for my_field

$errors['wpcf-field5']='This unit is already listed';

}

}

//return result

return array($fields,$errors);

}

Link to a page where the issue can be seen:

I expected to see: AN error message. I also use the JS code that I found on anoyther example but I don't see anything.

Need to know how I can generate an error so that no 2 person can submit the same property listing.

#1137802

Hi, I think I see a typo here, please check:

...
array('key' => 'wpcf-field5t',
'value' => $_POST['wpcf-field5']),
...

Then, the field slugs in the errors array should not use the 'wpcf-' prefix, even though it is used in the $fields array. That syntax is confusing, I think. Anyway the line should probably be:

$errors['field5']='This unit is already listed';

If these two changes don't solve the problem, I'll need to take a closer look.

#1137995

No it did not help. The typo wasn't there, cos I repalced them with general fields to hide the actual fields from my site. The error message is still not showing anywhere.

#1138239

Please provide login credentials in the private reply fields here so I can check in wp-admin. Tell me where I can see this Form on the front-end of your site.

#1139775

Check the unit number field slugs in your PHP, they don't match the actual field slug. They should be wpcf-unit-numbers and unit-numbers:

array('key' => 'wpcf-unit-numbers',
'value' => $_POST['wpcf-unit-numbers']),
...
$errors['unit-numbers']='This unit is already listed';

The field is correct in the Form:

[cred_field field="unit-numbers" force_type="field" class="form-control" output="bootstrap"]
#1140125

Hi

Sorry about those silly mistakes but I am still not getting any error messages.

Thanks.

Jimmy Loh

#1140569

Check the browser console, I can see you have a JavaScript error related to this code in your Form JS panel:

jQuery(document).ready(function() {
  
  jQuery('input[name="wpcf-unit-number"]')
    .data('wpt-validate', {
      required: {
        args: {
          1: true
        },
        message: 'Custom Notification goes here'
      },
    }
    );
});

Also there's no need to add script tags in the JS panel, those will be inserted automatically.

#1141181

After making that change, there is still no difference. Should the error message show before I press submit or after?

#1141495

Okay I made some changes in your validation code:

$args = array(
                    'meta_query' => array(
                               'relation' => 'AND',
                        array('key' => 'wpcf-condo-postal-code',
                              'value' => $fields['wpcf-condo-postal-code']['value']),                            
                         array('key' => 'wpcf-block-number',
                              'value' => $fields['wpcf-block-number']['value']),                      
                           array('key' => 'wpcf-storey',
                              'value' => $fields['wpcf-storey']['value']),
                           array('key' => 'wpcf-unit-numbers',
                              'value' => $fields['wpcf-unit-numbers']['value']),
                           array('key' => 'wpcf-for-sale-or-rent',
                              'value' => $fields['wpcf-for-sale-or-rent']['value']),

                      ),
                    'post_type' => 'listing',
                    'posts_per_page' => -1
                    );

            $posts = get_posts($args);

You can see that I changed the post type slug from "upn" to "listing". I also changed the way you access the field values. Instead of checking the $_POST superglobal, the recommended way is to access the $fields array. I tried this a couple of times and it seems to work now. Can you confirm? Be sure to pull down these changes to your local file repository.