| 1 |
<?php |
| 2 |
defined('ABSPATH') || die(); |
| 3 |
|
| 4 |
class HashFormFieldText extends HashFormFieldType { |
| 5 |
|
| 6 |
protected $type = 'text'; |
| 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 |
|
| 21 |
$format = trim((string) HashFormFields::get_option($this->field, 'format')); |
| 22 |
$max_length = intval(HashFormFields::get_option($this->field, 'max')); |
| 23 |
|
| 24 |
// Only run the format check when one is configured and a value was |
| 25 |
// submitted — an empty optional field must not fail the pattern. |
| 26 |
if ($format !== '' && $args['value'] !== '' && !preg_match(self::format($this->field), $args['value'])) { |
| 27 |
$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'); |
| 28 |
} |
| 29 |
|
| 30 |
if ($max_length && strlen($args['value']) > $max_length) { |
| 31 |
$errors['field' . $args['id']] = HashFormFields::get_error_msg($this->field, 'max_char'); |
| 32 |
} |
| 33 |
return $errors; |
| 34 |
} |
| 35 |
|
| 36 |
public static function format($field) { |
| 37 |
$pattern = HashFormFields::get_option($field, 'format'); |
| 38 |
// Escape raw delimiters so an admin pattern containing "/" cannot |
| 39 |
// break the expression (preg_match would then always fail). Already |
| 40 |
// escaped slashes are normalized first to avoid double escaping. |
| 41 |
$pattern = str_replace('\/', '/', $pattern); |
| 42 |
return '/' . str_replace('/', '\/', $pattern) . '/'; |
| 43 |
} |
| 44 |
|
| 45 |
protected function input_html() { |
| 46 |
?> |
| 47 |
<input type="text" <?php $this->field_attrs(); ?> /> |
| 48 |
<?php |
| 49 |
} |
| 50 |
|
| 51 |
} |
| 52 |
|