PluginProbe
Hash Form – Drag & Drop Form Builder / trunk
Hash Form – Drag & Drop Form Builder vtrunk
1.4.4 1.4.3 1.4.2 1.4.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.6.1 1.2.7 1.2.8 1.2.9 1.3.0 All 47 releases
hash-form / includes / fields / HashFormFieldRangeSlider.php

HashFormFieldRangeSlider.php in Hash Form – Drag & Drop Form Builder trunk, at includes/fields/HashFormFieldRangeSlider.php

82 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined('ABSPATH') || die();
3
4 class HashFormFieldRangeSlider extends HashFormFieldType {
5
6 protected $type = 'range_slider';
7
8 public function field_settings_for_type() {
9 return array(
10 'range' => true
11 );
12 }
13
14 public function validate($args) {
15 $errors = array();
16
17 if (!is_numeric($args['value']) && '' !== $args['value'])
18 $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');
19
20 if ($args['value'] != '') {
21 $minnum = HashFormFields::get_option($this->field, 'minnum');
22 $maxnum = HashFormFields::get_option($this->field, 'maxnum');
23 if ($maxnum !== '' && $minnum !== '') {
24 $value = (float) $args['value'];
25 if ($value < $minnum) {
26 $errors['field' . $args['id']] = esc_html__('Please select a higher number', 'hash-form');
27 } elseif ($value > $maxnum) {
28 $errors['field' . $args['id']] = esc_html__('Please select a lower number', 'hash-form');
29 }
30 }
31 $this->validate_step($errors, $args);
32 }
33 return $errors;
34 }
35
36 private function validate_step(&$errors, $args) {
37 if (isset($errors['field' . $args['id']])) {
38 return;
39 }
40 $step = HashFormFields::get_option($this->field, 'step');
41 if (!$step || !is_numeric($step)) {
42 return;
43 }
44 $result = $this->check_value_is_valid_with_step($args['value'], $step);
45 if (!$result) {
46 return;
47 }
48 /* translators: 1: nearest number, 2: nearest number */
49 $errors['field' . $args['id']] = sprintf(__('Please enter a valid value. Two nearest valid values are %1$s and %2$s', 'hash-form'), floatval($result[0]), floatval($result[1]));
50 }
51
52 private function check_value_is_valid_with_step($value, $step) {
53 $decimals = max(HashFormHelper::count_decimals($value), HashFormHelper::count_decimals($step));
54 $pow = pow(10, $decimals);
55 $value = intval($pow * $value);
56 $step = intval($pow * $step);
57 $div = $value / $step;
58 if (is_int($div)) {
59 return 0;
60 }
61 $div = floor($div);
62 return array($div * $step / $pow, ($div + 1) * $step / $pow);
63 }
64
65 public function sanitize_value(&$value) {
66 return hashform_sanitize_float($value);
67 }
68
69 protected function input_html() {
70 $field = $this->get_field();
71 ?>
72 <div class="hf-range-slider-container">
73 <div class="hf-range-slider-wrap">
74 <div class="hf-range-slider"></div>
75 <input class="hf-range-input-selector" type="number" <?php $this->field_attrs(); ?>>
76 </div>
77 </div>
78 <?php
79 }
80
81 }
82