Skip to content

Commit f310621

Browse files
authored
gpapf-validate-number-country.php: Created a snippet that will ensure numbers that share the same dialing code can't be submitted if their country is not allowed.
1 parent e6346b7 commit f310621

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Gravity Perks // Advanced Phone Field // Validate Number by Country
4+
* https://gravitywiz.com/documentation/gravity-forms-advanced-phone-field/
5+
*
6+
* If you are limiting available countries to Canada, US numbers are still accepted. This snippet will ensure that the
7+
* country of the submitted number is a country that is allowed according to your "Countries" setting.
8+
*/
9+
add_action( 'gform_field_validation', function( $result, $field_value, $form, $field, $context ) {
10+
11+
if ( class_exists( 'GP_Advanced_Phone_Field' ) && gp_advanced_phone_field()->is_advanced_phone_field( $field ) ) {
12+
13+
$countries_action = gp_advanced_phone_field()->get_plugin_setting( 'countries_action' );
14+
if ( $countries_action === 'all' ) {
15+
return $result;
16+
}
17+
18+
$countries = gp_advanced_phone_field()->get_plugin_setting( 'countries' );
19+
$country = gp_advanced_phone_field()->get_phone_number_proto( $field_value )->regionCode;
20+
$is_found = in_array( $country, $countries );
21+
22+
if ( $countries_action === 'include' && ! $is_found ) {
23+
$result['is_valid'] = false;
24+
$result['message'] = esc_html__( 'This phone number is not valid for the selected country.' );
25+
} else if ( $countries_action === 'exclude' && $is_found ) {
26+
$result['is_valid'] = false;
27+
$result['message'] = esc_html__( 'This phone number is not valid for the selected country.' );
28+
}
29+
30+
}
31+
32+
return $result;
33+
}, 10, 5 );

0 commit comments

Comments
 (0)