From 2590461baee0cd8f6a56849ab0c6a093946ac01d Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 26 Dec 2023 15:38:08 +0100 Subject: [PATCH] v 2.63.0 Toolbox v 0.8.0 - Add form basic validation. - Fix for invalid input IDs. --- inc/WPUBaseToolbox/WPUBaseToolbox.php | 22 ++++++--- inc/WPUBaseToolbox/assets/form-validation.js | 50 ++++++++++++++++++++ wpubaseplugin.php | 6 +-- 3 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 inc/WPUBaseToolbox/assets/form-validation.js diff --git a/inc/WPUBaseToolbox/WPUBaseToolbox.php b/inc/WPUBaseToolbox/WPUBaseToolbox.php index 3063c8f..12aaba0 100644 --- a/inc/WPUBaseToolbox/WPUBaseToolbox.php +++ b/inc/WPUBaseToolbox/WPUBaseToolbox.php @@ -1,10 +1,10 @@ plugin_version); + } /* ---------------------------------------------------------- Forms @@ -47,7 +56,7 @@ public function get_form_html($form_id, $fields = array(), $args = array()) { } /* Start form */ - $html .= '
'; + $html .= ''; /* Insert fields */ foreach ($args['fieldsets'] as $fieldset_id => $fieldset) { @@ -181,7 +190,7 @@ public function get_field_html($field_name, $field, $form_id, $args = array()) { } /* Values */ - $field_id = $form_id . '__' . $field_name; + $field_id = strtolower($form_id . '__' . $field_name); $field_id_name = ' name="' . esc_attr($field_name) . '" id="' . esc_attr($field_id) . '" ' . $field['extra_attributes']; if ($field['required']) { $field_id_name .= ' required'; @@ -219,7 +228,7 @@ public function get_field_html($field_name, $field, $form_id, $args = array()) { case 'radio': $html .= $default_label; foreach ($field['data'] as $key => $var) { - $id_field = $field_id . '___' . $key; + $id_field = strtolower($field_id . '___' . $key); $html .= ''; $html .= ''; $html .= ''; @@ -256,6 +265,7 @@ public function get_field_html($field_name, $field, $form_id, $args = array()) { $html .= '<' . $field_tag . ' class="' . $args['field_box_classname'] . '" data-box-name="' . $field_name . '" data-box-type="' . esc_attr($field['type']) . '">'; $html .= $field['html_before_content']; $html .= $field_html; + $html .= ''; $html .= $field['html_after_content']; $html .= ''; $html .= $field['html_after_fieldgroup_inner']; diff --git a/inc/WPUBaseToolbox/assets/form-validation.js b/inc/WPUBaseToolbox/assets/form-validation.js new file mode 100644 index 0000000..2f1d7e7 --- /dev/null +++ b/inc/WPUBaseToolbox/assets/form-validation.js @@ -0,0 +1,50 @@ +document.addEventListener("DOMContentLoaded", function() { + 'use strict'; + Array.prototype.forEach.call(document.querySelectorAll('.wpubasetoolbox-form [data-box-name]'), function($box, i) { + wpubasetoolbox_box_validation($box); + }); +}); + +function wpubasetoolbox_box_validation($box) { + var _id = $box.getAttribute('data-box-name'), + $fields = $box.querySelectorAll('[name="' + _id + '"]'), + $message = $box.querySelector('.wpubasetoolbox-form-validation-message'), + _ischecking = false; + + if (!$fields.length || !$message) { + return; + } + + function check_field_error($tmp_field) { + if (_ischecking) { + return false; + } + _ischecking = true; + var _valid = $tmp_field.checkValidity(); + _ischecking = false; + if (_valid) { + $box.setAttribute('data-box-error', '0'); + $message.innerHTML = ''; + return; + } + setTimeout(function() { + window.scrollTo({ + top: $box.getBoundingClientRect().top + window.pageYOffset - 100, + behavior: 'smooth' + }); + }, 10); + + $box.setAttribute('data-box-error', '1'); + $message.innerHTML = $tmp_field.validationMessage; + } + + Array.prototype.forEach.call($fields, function($tmp_field, i) { + $tmp_field.addEventListener("invalid", function() { + check_field_error($tmp_field); + }); + $tmp_field.addEventListener("change", function() { + check_field_error($tmp_field); + }); + }); + +} diff --git a/wpubaseplugin.php b/wpubaseplugin.php index 4ae46aa..47ef491 100644 --- a/wpubaseplugin.php +++ b/wpubaseplugin.php @@ -5,7 +5,7 @@ Plugin URI: https://github.com/WordPressUtilities/wpubaseplugin Update URI: https://github.com/WordPressUtilities/wpubaseplugin Description: A framework for a WordPress plugin -Version: 2.62.1 +Version: 2.63.0 Author: Darklg Author URI: https://darklg.me/ Text Domain: wpubaseplugin @@ -18,7 +18,7 @@ class WPUBasePlugin { - public $version = '2.62.1'; + public $version = '2.63.0'; private $utilities_classes = array( 'messages' => array( @@ -54,7 +54,7 @@ class WPUBasePlugin { 'name' => 'WPUBaseEmail' ), 'toolbox' => array( - 'namespace' => 'wpubasetoolbox_0_7_1', + 'namespace' => 'wpubasetoolbox_0_8_0', 'name' => 'WPUBaseToolbox' ), 'filecache' => array(