From 700df5a4150238f9004c24927b14973a9de361e9 Mon Sep 17 00:00:00 2001 From: Stephen Cameron Date: Wed, 29 Apr 2020 21:04:20 +0200 Subject: [PATCH] Form validation, localisation, UX + email formatting. WIP #3445 @10 --- wp-content/mu-plugins/cube/src/Forms/Base.php | 72 ++++++++++++++++--- .../cube/src/Forms/Consultation.php | 25 ++++--- wp-content/themes/CCV/package.json | 1 + .../assets/styles/components/forms.styl | 25 +++++-- .../views/forms/common/email.blade.php | 20 ++++-- wp-content/themes/CCV/webpack.mix.js | 6 ++ wp-content/themes/CCV/yarn.lock | 12 ++++ 7 files changed, 128 insertions(+), 33 deletions(-) diff --git a/wp-content/mu-plugins/cube/src/Forms/Base.php b/wp-content/mu-plugins/cube/src/Forms/Base.php index 4a4c5d5..f81b069 100644 --- a/wp-content/mu-plugins/cube/src/Forms/Base.php +++ b/wp-content/mu-plugins/cube/src/Forms/Base.php @@ -50,9 +50,9 @@ class Base public function register_scripts() { - // Base forms functionality - wp_register_script('cube-forms', asset('scripts/forms.js'), ['jquery'], null, true); - wp_enqueue_script('cube-forms'); // Always needed, so enqueue it directly here + //=== Cube Forms + // Base forms functionality - enqueued directly since it is always required + wp_enqueue_script('cube-forms', asset('scripts/forms.js'), ['jquery'], null, true); // JS variables wp_add_inline_script( @@ -65,6 +65,48 @@ class Base 'before' // Output before script so we can use the vars ); + + //=== Parsley JS Validation + wp_enqueue_script('parsleyjs', asset('scripts/parsley/parsley.min.js'), ['jquery'], null, true); + + // Load localisation if available (English is default so not needed) + $parsley_locales = ['ar', 'fr', 'ru']; // List of possible locales (see webpack.mix.js for which ones are copied) + if (function_exists('pll_current_language')) { + $current_locale = pll_current_language('slug'); + + if (in_array($current_locale, $parsley_locales)) { + // Include it inline since it's small and to save an extra HTTP request + wp_add_inline_script('parsleyjs', asset("scripts/parsley/locale/{$current_locale}.js")->contents()); + } + } + + // Parsley JS initialisation and overrides + // Ref: https://stackoverflow.com/a/30122442 + wp_add_inline_script('parsleyjs', " + const parsleyConfig = { + classHandler: function(parsleyField) { + var fieldWrapper = parsleyField.\$element.parents('.form-field'); + return (fieldWrapper.length > 0) ? fieldWrapper : parsleyField; + }, + + errorsContainer: function(parsleyField) { + var inputWrapper = parsleyField.\$element.parents('.form-field-input'); + return (inputWrapper.length > 0) ? inputWrapper : parsleyField; + } + }; + + jQuery('.cube-form').parsley(parsleyConfig); + + // On validation errors, scroll to the first invalid input + jQuery.listen('parsley:field:error', function() { + window.scrollTo({...jQuery('.parsley-error').first().offset(), ...{behavior: 'smooth'}}); + }); + + "); + + + //=== Flatpickr Calendar + // Elementor has an older, incompatible version of Flatpickr so we need to replace it with our version if (wp_script_is('flatpickr', 'registered')) { wp_deregister_script('flatpickr'); @@ -165,7 +207,7 @@ class Base } } - $message = view('forms.common.email', compact('data')); + $message = view('forms.common.email', compact('data', 'subject')); $success = wp_mail($to, $subject, $message, $headers); @@ -210,9 +252,15 @@ class Base 'placeholder' => $field['title'], 'field_before' => '', 'field_after' => '', + 'validation' => '', ]; $settings = array_merge($default_settings, $settings); + // Set up default validation that can be overridden by setting validation field + if ($field['required'] && empty($settings['validation'])) { + $settings['validation'] = 'required'; // Default but could also be set with other ParsleyJS attributes + } + $res = ''; if ($settings['show_title']) { @@ -270,7 +318,7 @@ class Base foreach ($options as $option) { $res .= '