Hi, is there any way to validate the address field before a Cred form is submitted?
I just need to be sure that the user has entered a value that returns a valid longitude and latitude.
Thanks
Hello,
It is possible with filter hook "cred_form_validate", for example:
add_filter('cred_form_validate', 'validate_address', 10, 2);
function validate_address( $error_fields, $form_data ) {
list($fields,$errors)=$data;
if (in_array($form_data['id'], array(83))){ // replace 123 with your post form IDs
$slug = 'wpcf-test-addr'; // replace test-addr with your custom address field slug
if(!isset($_POST['toolset-extended-form-' . $slug]['latitude']) || empty($_POST['toolset-extended-form-' . $slug]['latitude'])){
$errors['wpcf-test-addr'] = __('Please input a valid address');
}
}
return array($fields,$errors);
}
More help:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
Hi, thanks for the snippet.
Tested, but I get an error even if the address is legit.
I have tried above codes in my localhost, it works fine, please make sure you are using correct address field value, you can click the the link "Show/Hide coordinates", make sure you get the coordinates values, see my screenshot validate-address.JPG
In my Cred module I do not have the option "Show/Hide Coordinates" front-end.
hidden link
and it generates the same error for the field "wpcf-address", with or without coordinates.
hidden link
So I checked backend the address field, and it generates the coordinates correctly, but I noticed that they don't reset if you switch to a non-existing address
hidden link
The problem you mentioned above is abnormal, please check these:
1) Make sure you are using the latest version of Toolset plugins, you can download them here:
https://toolset.com/account/downloads/
2) In case it is a compatibility problem, please deactivate all other plugins, and switch to WordPress default theme 2021, deactivate all custom PHP/JS code snippets, and test again
3) Also check if there is any PHP/JS error in your website:
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/
4) If the problem still persists, please provide database dump file(ZIP file) of your website, you can put the package files in your own google drive disk, share the link only, also point out the problem page URL and form URL, I need to test and debug it in my localhost, thanks
https://toolset.com/faq/provide-supporters-copy-site/
Yes, there is a jscript error in console
hidden link
Tested with standard theme and only Toolset plugins + Woocommerce activated.
Any idea? Thanks
The problem is abnormal, if there is any custom JS codes in that post form/page, please try to remove them, and test again.
If you still need more assistance for it, please provide a test site with the same problem, I need a live website to test and debug, thanks