From c972f44dc2cc40a99b296e403e54533a3610c655 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 6 Sep 2024 02:59:21 +0200 Subject: [PATCH] Debug v20 - Fix mandatory fields --- htdocs/core/class/doleditor.class.php | 13 +++-- htdocs/core/lib/functions.lib.php | 80 +++++++++++++++++++++++---- htdocs/langs/en_US/main.lang | 1 + htdocs/product/card.php | 4 +- 4 files changed, 79 insertions(+), 19 deletions(-) diff --git a/htdocs/core/class/doleditor.class.php b/htdocs/core/class/doleditor.class.php index f0487fd875b..b8f63e352fc 100644 --- a/htdocs/core/class/doleditor.class.php +++ b/htdocs/core/class/doleditor.class.php @@ -230,7 +230,12 @@ class DolEditor textDirection: \''.dol_escape_js($langs->trans("DIRECTION")).'\', on : { instanceReady : function(ev) { - console.log("ckeditor instanceReady"); + console.log(\'ckeditor '.dol_escape_js($this->htmlname).' instanceReady\'); + + /* If we found the attribute required on source div, we remove it (not compatible with ckeditor) */ + /* Disabled, because attribute required should never be used on fields for doleditor */ + /* jQuery("#'.dol_escape_js($this->htmlname).'").attr("required", false); */ + // Output paragraphs as

Text

. this.dataProcessor.writer.setRules( \'p\', { indent : false, @@ -241,15 +246,13 @@ class DolEditor }); }, /* This is to remove the tab Link on image popup. Does not work, so commented */ - /* - dialogDefinition: function (event) { + /* dialogDefinition: function (event) { var dialogName = event.data.name; var dialogDefinition = event.data.definition; if (dialogName == \'image\') { dialogDefinition.removeContents(\'Link\'); } - } - */ + } */ }, disableNativeSpellChecker: '.(getDolGlobalString('CKEDITOR_NATIVE_SPELLCHECKER') ? 'false' : 'true'); diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 0824379ab79..b1c147c1761 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -10925,7 +10925,8 @@ function complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, */ function printCommonFooter($zone = 'private') { - global $conf, $hookmanager, $user, $debugbar; + global $conf, $hookmanager, $user, $langs; + global $debugbar; global $action; global $micro_start_time; @@ -10973,6 +10974,7 @@ function printCommonFooter($zone = 'private') $relativepathstring = preg_replace('/^\//', '', $relativepathstring); $relativepathstring = preg_replace('/^custom\//', '', $relativepathstring); //$tmpqueryarraywehave = explode('&', dol_string_nohtmltag($_SERVER['QUERY_STRING'])); + if (!empty($user->default_values[$relativepathstring]['focus'])) { foreach ($user->default_values[$relativepathstring]['focus'] as $defkey => $defval) { $qualified = 0; @@ -10995,10 +10997,11 @@ function printCommonFooter($zone = 'private') } if ($qualified) { + print 'console.log("set the focus by executing jQuery(...).focus();")'."\n"; foreach ($defval as $paramkey => $paramval) { // Set focus on field print 'jQuery("input[name=\''.$paramkey.'\']").focus();'."\n"; - print 'jQuery("textarea[name=\''.$paramkey.'\']").focus();'."\n"; + print 'jQuery("textarea[name=\''.$paramkey.'\']").focus();'."\n"; // TODO KO with ckeditor print 'jQuery("select[name=\''.$paramkey.'\']").focus();'."\n"; // Not really useful, but we keep it in case of. } } @@ -11026,19 +11029,72 @@ function printCommonFooter($zone = 'private') } if ($qualified) { - foreach ($defval as $paramkey => $paramval) { - // Add property 'required' on input - print 'jQuery("input[name=\''.$paramkey.'\']").prop(\'required\',true);'."\n"; - print 'jQuery("textarea[name=\''.$paramkey.'\']").prop(\'required\',true);'."\n"; - print '// required on a select works only if key is "", so we add the required attributes but also we reset the key -1 or 0 to an empty string'."\n"; - print 'jQuery("select[name=\''.$paramkey.'\']").prop(\'required\',true);'."\n"; - print 'jQuery("select[name=\''.$paramkey.'\'] option[value=\'-1\']").prop(\'value\', \'\');'."\n"; - print 'jQuery("select[name=\''.$paramkey.'\'] option[value=\'0\']").prop(\'value\', \'\');'."\n"; + print 'console.log("set the js code to manage fields that are set as mandatory");'."\n"; + foreach ($defval as $paramkey => $paramval) { + // Solution 1: Add handler on submit to check if mandatory fields are empty + print 'var form = $(\'#'.dol_escape_js($paramkey).'\').closest("form");'."\n"; + print "form.on('submit', function(event) { + var submitter = event.originalEvent.submitter; + if (submitter) { + var buttonName = $(submitter).attr('name'); + if (buttonName == 'cancel') { + console.log('We click on cancel button so we accept submit with no need to check mandatory fields'); + return true; + } + } + + console.log('We did not click on cancel button but on something else, we check that field #".dol_escape_js($paramkey)." is not empty'); + + var tmpvalue = jQuery('#".dol_escape_js($paramkey)."').val(); + let tmptypefield = jQuery('#".dol_escape_js($paramkey)."').prop('nodeName').toLowerCase(); // Get the tag name (div, section, footer...) + + if (tmptypefield == 'textarea') { + // We must instead check the content of ckeditor + var tmpeditor = CKEDITOR.instances['".dol_escape_js($paramkey)."']; + if (tmpeditor) { + tmpvalue = tmpeditor.getData(); + console.log('For textarea tmpvalue is '+tmpvalue); + } + } + + let tmpvalueisempty = false; + if (tmpvalue === null || tmpvalue === undefined || tmpvalue === '') { + tmpvalueisempty = true; + } + if (tmpvalue === '0' && tmptypefield == 'select') { + tmpvalueisempty = true; + } + if (tmpvalueisempty) { + console.log('field has type '+tmptypefield+' and is empty, we cancel the submit'); + event.preventDefault(); // Stop submission of form to allow custom code to decide. + event.stopPropagation(); // Stop other handlers. + alert('".dol_escape_js($langs->trans("ErrorFieldRequired", $paramkey).' ('.$langs->trans("CustomMandatoryFieldRule").')')."'); + return false; + } + console.log('field has type '+tmptypefield+' and is defined to '+tmpvalue); + return true; + }); + \n"; + + // Solution 2: Add property 'required' on input + // so browser will check value and try to focus on it when submitting the form. + //print 'setTimeout(function() {'; // If we want to wait that ckeditor beuatifier has finished its job. + //print 'jQuery("input[name=\''.$paramkey.'\']").prop(\'required\',true);'."\n"; + //print 'jQuery("textarea[id=\''.$paramkey.'\']").prop(\'required\',true);'."\n"; + //print 'jQuery("select[name=\''.$paramkey.'\']").prop(\'required\',true);'."\n";*/ + //print '// required on a select works only if key is "", so we add the required attributes but also we reset the key -1 or 0 to an empty string'."\n"; + //print 'jQuery("select[name=\''.$paramkey.'\'] option[value=\'-1\']").prop(\'value\', \'\');'."\n"; + //print 'jQuery("select[name=\''.$paramkey.'\'] option[value=\'0\']").prop(\'value\', \'\');'."\n"; // Add 'field required' class on closest td for all input elements : input, textarea and select - print 'jQuery(":input[name=\'' . $paramkey . '\']").closest("tr").find("td:first").addClass("fieldrequired");' . "\n"; + //print '}, 500);'; // 500 milliseconds delay + + // Now set the class "fieldrequired" + print 'jQuery(\':input[name="' . dol_escape_js($paramkey) . '"]\').closest("tr").find("td:first").addClass("fieldrequired");'."\n"; } - // If we submit the cancel button we remove the required attributes + + + // If we submit using the cancel button, we remove the required attributes print 'jQuery("input[name=\'cancel\']").click(function() { console.log("We click on cancel button so removed all required attribute"); jQuery("input, textarea, select").each(function(){this.removeAttribute(\'required\');}); diff --git a/htdocs/langs/en_US/main.lang b/htdocs/langs/en_US/main.lang index bedc6a60e08..f8e6f2ccede 100644 --- a/htdocs/langs/en_US/main.lang +++ b/htdocs/langs/en_US/main.lang @@ -45,6 +45,7 @@ NoError=No error Error=Error Errors=Errors ErrorFieldRequired=Field '%s' is required +CustomMandatoryFieldRule=Custom "Mandatory field" rule ErrorFieldFormat=Field '%s' has a bad value ErrorFileDoesNotExists=File %s does not exist ErrorFailedToOpenFile=Failed to open file %s diff --git a/htdocs/product/card.php b/htdocs/product/card.php index 8422ba4e103..cd8433c59e3 100644 --- a/htdocs/product/card.php +++ b/htdocs/product/card.php @@ -1397,7 +1397,7 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($canvasdisplayactio } // Label - print ''.$langs->trans("Label").''; + print ''.$langs->trans("Label").''; // On sell print ''.$langs->trans("Status").' ('.$langs->trans("Sell").')'; @@ -3064,7 +3064,7 @@ if (getDolGlobalString('PRODUCT_ADD_FORM_ADD_TO') && $object->id && ($action == $html .= ''; $html .= ''; - print ''; + print '
'; print $html; print '
';