2
0
forked from Wavyzz/dolibarr

Debug v20 - Fix mandatory fields

This commit is contained in:
Laurent Destailleur
2024-09-06 02:59:21 +02:00
parent ba489369dd
commit c972f44dc2
4 changed files with 79 additions and 19 deletions

View File

@@ -230,7 +230,12 @@ class DolEditor
textDirection: \''.dol_escape_js($langs->trans("DIRECTION")).'\', textDirection: \''.dol_escape_js($langs->trans("DIRECTION")).'\',
on : { on : {
instanceReady : function(ev) { 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 <p>Text</p>. // Output paragraphs as <p>Text</p>.
this.dataProcessor.writer.setRules( \'p\', { this.dataProcessor.writer.setRules( \'p\', {
indent : false, indent : false,
@@ -241,15 +246,13 @@ class DolEditor
}); });
}, },
/* This is to remove the tab Link on image popup. Does not work, so commented */ /* 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 dialogName = event.data.name;
var dialogDefinition = event.data.definition; var dialogDefinition = event.data.definition;
if (dialogName == \'image\') { if (dialogName == \'image\') {
dialogDefinition.removeContents(\'Link\'); dialogDefinition.removeContents(\'Link\');
} }
} } */
*/
}, },
disableNativeSpellChecker: '.(getDolGlobalString('CKEDITOR_NATIVE_SPELLCHECKER') ? 'false' : 'true'); disableNativeSpellChecker: '.(getDolGlobalString('CKEDITOR_NATIVE_SPELLCHECKER') ? 'false' : 'true');

View File

@@ -10925,7 +10925,8 @@ function complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type,
*/ */
function printCommonFooter($zone = 'private') function printCommonFooter($zone = 'private')
{ {
global $conf, $hookmanager, $user, $debugbar; global $conf, $hookmanager, $user, $langs;
global $debugbar;
global $action; global $action;
global $micro_start_time; global $micro_start_time;
@@ -10973,6 +10974,7 @@ function printCommonFooter($zone = 'private')
$relativepathstring = preg_replace('/^\//', '', $relativepathstring); $relativepathstring = preg_replace('/^\//', '', $relativepathstring);
$relativepathstring = preg_replace('/^custom\//', '', $relativepathstring); $relativepathstring = preg_replace('/^custom\//', '', $relativepathstring);
//$tmpqueryarraywehave = explode('&', dol_string_nohtmltag($_SERVER['QUERY_STRING'])); //$tmpqueryarraywehave = explode('&', dol_string_nohtmltag($_SERVER['QUERY_STRING']));
if (!empty($user->default_values[$relativepathstring]['focus'])) { if (!empty($user->default_values[$relativepathstring]['focus'])) {
foreach ($user->default_values[$relativepathstring]['focus'] as $defkey => $defval) { foreach ($user->default_values[$relativepathstring]['focus'] as $defkey => $defval) {
$qualified = 0; $qualified = 0;
@@ -10995,10 +10997,11 @@ function printCommonFooter($zone = 'private')
} }
if ($qualified) { if ($qualified) {
print 'console.log("set the focus by executing jQuery(...).focus();")'."\n";
foreach ($defval as $paramkey => $paramval) { foreach ($defval as $paramkey => $paramval) {
// Set focus on field // Set focus on field
print 'jQuery("input[name=\''.$paramkey.'\']").focus();'."\n"; 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. 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) { if ($qualified) {
foreach ($defval as $paramkey => $paramval) { print 'console.log("set the js code to manage fields that are set as mandatory");'."\n";
// 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";
// Add 'field required' class on closest td for all input elements : input, textarea and select foreach ($defval as $paramkey => $paramval) {
print 'jQuery(":input[name=\'' . $paramkey . '\']").closest("tr").find("td:first").addClass("fieldrequired");' . "\n"; // 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;
} }
// If we submit the cancel button we remove the required attributes }
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 '}, 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 using the cancel button, we remove the required attributes
print 'jQuery("input[name=\'cancel\']").click(function() { print 'jQuery("input[name=\'cancel\']").click(function() {
console.log("We click on cancel button so removed all required attribute"); console.log("We click on cancel button so removed all required attribute");
jQuery("input, textarea, select").each(function(){this.removeAttribute(\'required\');}); jQuery("input, textarea, select").each(function(){this.removeAttribute(\'required\');});

View File

@@ -45,6 +45,7 @@ NoError=No error
Error=Error Error=Error
Errors=Errors Errors=Errors
ErrorFieldRequired=Field '%s' is required ErrorFieldRequired=Field '%s' is required
CustomMandatoryFieldRule=Custom "Mandatory field" rule
ErrorFieldFormat=Field '%s' has a bad value ErrorFieldFormat=Field '%s' has a bad value
ErrorFileDoesNotExists=File %s does not exist ErrorFileDoesNotExists=File %s does not exist
ErrorFailedToOpenFile=Failed to open file %s ErrorFailedToOpenFile=Failed to open file %s

View File

@@ -1397,7 +1397,7 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($canvasdisplayactio
} }
// Label // Label
print '<tr><td class="fieldrequired">'.$langs->trans("Label").'</td><td><input name="label" class="minwidth300 maxwidth400onsmartphone" maxlength="255" value="'.dol_escape_htmltag(GETPOST('label', $label_security_check)).'"></td></tr>'; print '<tr><td class="fieldrequired">'.$langs->trans("Label").'</td><td><input id="label" name="label" class="minwidth300 maxwidth400onsmartphone" maxlength="255" value="'.dol_escape_htmltag(GETPOST('label', $label_security_check)).'"></td></tr>';
// On sell // On sell
print '<tr><td class="fieldrequired">'.$langs->trans("Status").' ('.$langs->trans("Sell").')</td><td>'; print '<tr><td class="fieldrequired">'.$langs->trans("Status").' ('.$langs->trans("Sell").')</td><td>';
@@ -3064,7 +3064,7 @@ if (getDolGlobalString('PRODUCT_ADD_FORM_ADD_TO') && $object->id && ($action ==
$html .= '<input type="text" class="flat" name="remise_percent" size="1" value="0">'; $html .= '<input type="text" class="flat" name="remise_percent" size="1" value="0">';
$html .= '</td></tr>'; $html .= '</td></tr>';
print '<table width="100%" class="border">'; print '<table class="centpercent border">';
print $html; print $html;
print '</table>'; print '</table>';