| 1 |
<?php |
| 2 |
defined('ABSPATH') || die(); |
| 3 |
|
| 4 |
class HashFormFieldPhone extends HashFormFieldType { |
| 5 |
|
| 6 |
protected $type = 'phone'; |
| 7 |
|
| 8 |
protected function field_settings_for_type() { |
| 9 |
return array( |
| 10 |
'advanced_validation' => true, |
| 11 |
'clear_on_focus' => true, |
| 12 |
'invalid' => true, |
| 13 |
'format' => true, |
| 14 |
'max' => true, |
| 15 |
); |
| 16 |
} |
| 17 |
|
| 18 |
public function validate($args) { |
| 19 |
$errors = array(); |
| 20 |
$pattern = self::phone_format($this->field); |
| 21 |
$max_length = intval(HashFormFields::get_option($this->field, 'max')); |
| 22 |
|
| 23 |
if ('' !== $pattern && !preg_match($pattern, $args['value'])) { |
| 24 |
$errors['field' . $args['id']] = apply_filters('hashform_translate_string', HashFormFields::get_error_msg($this->field, 'invalid'), 'Hash Form', HashFormBuilder::get_form_title($args['form_id']) . ' - ' . $args['id'] . ' - ' . 'Field Validation Message'); |
| 25 |
} |
| 26 |
|
| 27 |
if ($max_length && strlen($args['value']) > $max_length) { |
| 28 |
$errors['field' . $args['id']] = HashFormFields::get_error_msg($this->field, 'max_char'); |
| 29 |
} |
| 30 |
return $errors; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* The regex a phone value must match, or '' for no check. |
| 35 |
* |
| 36 |
* The test used to be inverted: a custom format was thrown away and |
| 37 |
* replaced by the built-in one, while no custom format left $pattern |
| 38 |
* empty and produced '//', which matches every string. So the setting did |
| 39 |
* nothing and the field accepted anything at all. |
| 40 |
*/ |
| 41 |
public static function phone_format($field) { |
| 42 |
$pattern = trim((string) HashFormFields::get_option($field, 'format')); |
| 43 |
|
| 44 |
if ('' === $pattern) { |
| 45 |
return ''; |
| 46 |
} |
| 47 |
|
| 48 |
return '/' . $pattern . '/'; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* The pattern offered as the default in the field settings. Kept out of |
| 53 |
* phone_format() so that turning it on is a choice the form makes, not |
| 54 |
* something imposed on every phone field that has never been configured. |
| 55 |
*/ |
| 56 |
public static function default_phone_pattern() { |
| 57 |
return '^((\+\d{1,3}(-|.| )?\(?\d\)?(-| |.)?\d{1,5})|(\(?\d{2,6}\)?))(-|.| )?(\d{3,4})(-|.| )?(\d{4})(( x| ext)\d{1,5}){0,1}$'; |
| 58 |
} |
| 59 |
|
| 60 |
protected function input_html() { |
| 61 |
?> |
| 62 |
<input type="text" <?php $this->field_attrs(); ?> /> |
| 63 |
<?php |
| 64 |
} |
| 65 |
|
| 66 |
} |
| 67 |
|