Skip Navigation

[Resolved] Prevent Toolset Forms allowing saving of non German addresses with Toolset Maps?

This support ticket is created 2 years, 1 month 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.

Our next available supporter will start replying to tickets in about 2.30 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 34 replies, has 3 voices.

Last updated by Minesh 1 year, 11 months ago.

Assisted by: Minesh.

Author
Posts
#2479081

Hi Support

We got some JavaScript code from you on a previous ticket (https://toolset.com/forums/topic/javascript-to-limit-datepickers-geo-stopped-working-on-some-views-post-forms/) to limit the autocomplete picklist suggestions from Google Maps to limit the address entry to addresses in Germany on the following Toolset Post Forms:
- Post Form - New Nanny Ad (ID: 921)
- Post Form - Edit Nanny Ad (ID: 923)
- Post Form - New Job Ad (ID: 929)
- Post Form - Edit Job Ad (ID: 931)

This works well for the most part, however, if a user just enters for example "Paris", or for example a postcode which does not exist in Germany like "80033" and does not "pick" an address from the autocomplete list in the APPROXIMATE LOCATION field, the record is still allowed to be saved. When the record is viewed on the front end then, the record shows the location of Paris, France, or the American location of zipcode 80033 in our examples, which are obviously not in Germany.

Is there any way to prevent the user technically from entering a non-German address? Ideally we would like to be able to show them a custom error message along the lines of "Address information entered in field xxxx must be in Germany" or similar and prevent them from continuing unless they correct the address to a German address.

Thanks and regards
Simon

#2479705

Hello,

Unfortunately, there isn't such kind of built-in feature within Toolset plugins: prevent the user technically from entering a non-German address.

You might consider custom codes, for example:
1) After user fill and submit the post forms, use filter hook cred_form_validate to trigger a PHP function
2) In this PHP function, check the address value, if it is a non-German address, then return a error message

More help:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

#2482447

Hi Luo

It would be enough if you could help us adapt the code here: https://toolset.com/forums/topic/validate-address-field/ to limit the latitude and longitude to the following values:

LATITUDE >= 47.2 AND <= 55
LONGITUDE >= 5.87 AND <= 15.03

Thanks and regards
Simon

#2482791

Here is the example codes:

add_filter('cred_form_validate', 'validate_address', 10, 2);
function validate_address( $error_fields, $form_data ) {
    $slug = 'wpcf-test-addr'; // replace test-addr with your custom address field slug
    list($fields,$errors)=$data; 
    if (in_array($form_data['id'], array(123))){ // replace 123 with your post form IDs
        if(isset($_POST['toolset-extended-form-' . $slug]['latitude'], $_POST['toolset-extended-form-' . $slug]['longitude'])){
            $latitude = $_POST['toolset-extended-form-' . $slug]['latitude'];
            $longitude = $_POST['toolset-extended-form-' . $slug]['longitude'];
			if(
				$latitude < 47.2
				|| $latitude > 55
				|| $longitude < 5.87
				|| $longitude > 15.03
			){
				$errors[$slug] = __('Please input a valid German address');
			}
        }
    }
    return array($fields,$errors);
}
#2485611

Hi Luo

Thanks for that helpful code. I adapted it to our site, adding the relevant Toolset Post Form IDs and custom field slug, as a test to handle the creation of Job Ad (hidden link)

<?php
/**
 * Toolset provided custom code to validate wpcf-family-approximate-location by latitude and longitude to German area (rectangle containing Germany)
 * See also https://toolset.com/forums/topic/prevent-toolset-forms-allowing-saving-of-non-german-addresses-with-toolset-maps/
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

add_filter('cred_form_validate', 'validate_job_ad_approximate_location', 10, 2);
function validate_job_ad_approximate_location ( $error_fields, $form_data ) {
	$slug = 'wpcf-family-approximate-location';
	list($fields,$errors)=$data; 
	// Post Form - New Job Ad (929)
	// Post Form - Edit Job Ad (931)
	if (in_array($form_data['id'], array(929, 931))){ 
		if(isset($_POST['toolset-extended-form-' . $slug]['latitude'], $_POST['toolset-extended-form-' . $slug]['longitude'])){
			$latitude = $_POST['toolset-extended-form-' . $slug]['latitude'];
			$longitude = $_POST['toolset-extended-form-' . $slug]['longitude'];
				if(
					$latitude < 47.2
					|| $latitude > 55
					|| $longitude < 5.87
					|| $longitude > 15.03
					){
					$errors[$slug] = __('Please enter a valid address in Germany');
				}
			}
		}
	return array($fields,$errors);
}

However , when I activate that code, it seems to cause a conflict with the taxonomy validation snippet "func-validate-taxonomies-on-job-ad-submission". It tells me that the taxonomies aren't filled out correctly, even if they are and I can't save the Job Ad. (see screenshots)

The error message is:
Warning: Undefined variable $data in /var/www/web24632223/html/dev/wp-content/toolset-customizations/function-validate-job-ad-approximate-location.php on line 12

Kind regards
Simon

#2486087

It seems to be a conflict with other custom codes, please provide a test site with the same problem, also point out the problem page URL and form URL, I need to test and debug it in a live website, thanks

#2486325
#2486627

I have changed the custom PHP codes snippet "function-validate-job-ad-approximate-location" as below:

add_filter('cred_form_validate', 'validate_job_ad_approximate_location', 10, 3);
function validate_job_ad_approximate_location ( $error_fields, $form_data ) {
	$slug = 'wpcf-family-approximate-location';
	list($fields,$errors)=$error_fields;
	// Post Form - New Job Ad (929)
	// Post Form - Edit Job Ad (931)
	if (in_array($form_data['id'], array(929, 931))){ 
		if(isset($_POST['toolset-extended-form-' . $slug]['latitude'], $_POST['toolset-extended-form-' . $slug]['longitude'])){
			$latitude = $_POST['toolset-extended-form-' . $slug]['latitude'];
			$longitude = $_POST['toolset-extended-form-' . $slug]['longitude'];
				if(
					$latitude < 47.2
					|| $latitude > 55
					|| $longitude < 5.87
					|| $longitude > 15.03
					){
					$errors[$slug] = __('Please enter a valid address in Germany');
				}
			}
		}
	return array($fields,$errors);
}

Please test again, check if it is fixed, thanks

#2486887

HI Luo

Thanks for the updated code. I put the updated code in dev.native-nanny.de again and am no longer getting error messages, however it is still allowing me to type in "Paris", not pick anything from the list and store the location Paris in France. So I'm not sure why it allows that location to be saved since it is not within the defined boundaries for latitude and longitude specified in the code.

Kind regards
Simon

#2487337

Above custom codes can only validate the address latitude and longitude values in below range:

LATITUDE >= 47.2 AND <= 55
LONGITUDE >= 5.87 AND <= 15.03

See your own post:
https://toolset.com/forums/topic/prevent-toolset-forms-allowing-saving-of-non-german-addresses-with-toolset-maps/#post-2482447

It can not prevent user input "Paris", hope it is clear.

#2487647

Hi Luo

Paris, France has the following longitude in the various measuring systems.

System Latitude Longitude
Simple decimal standard 48.85341 2.3488
Decimal Degrees (DD) 48.8534° N 2.3488° E
Degrees and Decimal Minutes (DDM) 48°51.205' N 2°20.928' E
Degrees, Minutes and Seconds (DMS) 48°51'12.3'' N 2°20'55.7'' E

1) Which system is Toolset Maps using by the way?
2) Paris should not be allowed to be stored, because it outside the LONGITUDE >= 5.87 AND <= 15.03 range.

Kind regards
Simon

#2488417

I have tried again in your website with below steps:
1) Open URL:
hidden link
2) In field "APPROXIMATE LOCATION", input value: Paris, France
3) submit the form:
I can see the error message correctly:
APPROXIMATE LOCATION: Please enter a valid address in Germany

Can you confirm it in your website? thanks

#2488449

Hi Luo

I can verify that entering "Paris" and trying to save in test.native-nanny.de is working as expected. However with the identical code on dev.native-nanny.de (same login credentials for you), it does not work. The form numbers are the same in dev and test, since test is just a duplicate of dev.

Kind regards
Simon

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/the-identical-code-on-dev-site-does-not-work/

#2488485

Thanks for the confirmation, according to our support policy we prefer one ticket one question, I assume the original question of this thread is resolved.
For other new questions, please check the new thread here:
https://toolset.com/forums/topic/the-identical-code-on-dev-site-does-not-work/

#2491335

HI Luo

Just waiting on my colleague to confirm all working in her tests as well.

Kind regards
Simon