From 8c537b997158d1ce7aaacbc22053cd5c7c8fefad Mon Sep 17 00:00:00 2001 From: Julien Boulen Date: Mon, 13 Jan 2025 16:29:20 +0100 Subject: [PATCH] MDL-83713 form: Fix javascript selector on file picker Co-authored-by: Meirza --- lib/form/form.js | 12 +-- lib/form/tests/behat/disabledif.feature | 12 +++ lib/form/tests/behat/hideif.feature | 12 +++ .../filepicker_hideif_disabledif_form.php | 79 +++++++++++++++++++ 4 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 lib/form/tests/fixtures/filepicker_hideif_disabledif_form.php diff --git a/lib/form/form.js b/lib/form/form.js index c9cde31c89075..258ae80b59060 100644 --- a/lib/form/form.js +++ b/lib/form/form.js @@ -485,8 +485,8 @@ if (typeof M.form.dependencyManager === 'undefined') { } if (this.getAttribute('class').toLowerCase() == 'filepickerhidden') { // Check for filepicker status. - var elementname = this.getAttribute('name'); - if (elementname && M.form_filepicker.instances[elementname].fileadded) { + const elementid = this.getAttribute('id'); + if (elementid && M.form_filepicker.instances[elementid].fileadded) { lock = false; } else { lock = true; @@ -553,8 +553,8 @@ if (typeof M.form.dependencyManager === 'undefined') { } if (this.getAttribute('class').toLowerCase() == 'filepickerhidden') { // Check for filepicker status. - var elementname = this.getAttribute('name'); - if (elementname && M.form_filepicker.instances[elementname].fileadded) { + const elementid = this.getAttribute('id'); + if (elementid && M.form_filepicker.instances[elementid].fileadded) { lock = false; } else { lock = true; @@ -617,8 +617,8 @@ if (typeof M.form.dependencyManager === 'undefined') { } // Check for filepicker status. if (this.getAttribute('class').toLowerCase() == 'filepickerhidden') { - var elementname = this.getAttribute('name'); - if (elementname && M.form_filepicker.instances[elementname].fileadded) { + const elementid = this.getAttribute('id'); + if (elementid && M.form_filepicker.instances[elementid].fileadded) { lock = true; } else { lock = false; diff --git a/lib/form/tests/behat/disabledif.feature b/lib/form/tests/behat/disabledif.feature index d4f33ad9607a3..0296e161cd9f9 100644 --- a/lib/form/tests/behat/disabledif.feature +++ b/lib/form/tests/behat/disabledif.feature @@ -27,3 +27,15 @@ Feature: disabledIf functionality in forms And the "class" attribute of "#fitem_id_some_static" "css_element" should not contain "text-muted" And the "#id_some_static_username" "css_element" should be enabled And the "class" attribute of "Check" "button" should not contain "disabled" + + Scenario: The file picker element is disabled when 'eq' disabledIf conditions are met + Given I am on the "filepicker_hideif_disabledif_form" "core_form > Fixture" page logged in as "admin" + And the "#id_filepicker" "css_element" should be enabled + When I click on "Disable" "radio" + Then the "#id_filepicker" "css_element" should be disabled + + @_file_upload + Scenario: The other element is disabled when the file picker is not empty + Given I am on the "filepicker_hideif_disabledif_form" "core_form > Fixture" page logged in as "admin" + When I upload "lib/ddl/tests/fixtures/xmldb_table.xml" file to "File picker" filemanager + Then the "inputtext1" "field" should be disabled diff --git a/lib/form/tests/behat/hideif.feature b/lib/form/tests/behat/hideif.feature index 768c116248b88..72f0d0d24ed27 100644 --- a/lib/form/tests/behat/hideif.feature +++ b/lib/form/tests/behat/hideif.feature @@ -39,3 +39,15 @@ Feature: hideIf functionality in forms Then I should not see "Static with form elements" And I click on "Enable" "radio" And I should see "Static with form elements" + + Scenario: The file picker element is hidden when 'eq' hideIf conditions are met + Given I am on the "filepicker_hideif_disabledif_form" "core_form > Fixture" page logged in as "admin" + And "#fitem_id_filepicker" "css_element" should be visible + When I click on "Hide" "radio" + Then "#fitem_id_filepicker" "css_element" should not be visible + + @_file_upload + Scenario: The other element is hidden when the file picker is not empty + Given I am on the "filepicker_hideif_disabledif_form" "core_form > Fixture" page logged in as "admin" + When I upload "lib/ddl/tests/fixtures/xmldb_table.xml" file to "File picker" filemanager + Then I should not see "inputtext2" diff --git a/lib/form/tests/fixtures/filepicker_hideif_disabledif_form.php b/lib/form/tests/fixtures/filepicker_hideif_disabledif_form.php new file mode 100644 index 0000000000000..18cf1c6621df1 --- /dev/null +++ b/lib/form/tests/fixtures/filepicker_hideif_disabledif_form.php @@ -0,0 +1,79 @@ +. + +require_once(__DIR__ . '/../../../../config.php'); + +defined('BEHAT_SITE_RUNNING') || die(); + +global $CFG, $PAGE, $OUTPUT; +require_once($CFG->libdir . '/formslib.php'); +$PAGE->set_url('/lib/form/tests/fixtures/filepicker_hideif_disabledif_form.php'); +$PAGE->add_body_class('limitedwidth'); +require_login(); +$PAGE->set_context(core\context\system::instance()); + +/** + * Test class for hiding and disabling file picker elements. + * + * @copyright Meirza + * @package core_form + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class test_filepicker_hideif_disabledif_form extends moodleform { + + /** + * Form definition. + */ + public function definition(): void { + $mform = $this->_form; + + // Radio buttons. + $radiogroup = [ + $mform->createElement('radio', 'some_radios', '', 'Enable', '1'), + $mform->createElement('radio', 'some_radios', '', 'Disable', '2'), + $mform->createElement('radio', 'some_radios', '', 'Hide', '3'), + ]; + + $mform->addGroup($radiogroup, 'some_radios_group', 'Enable/Disable/Hide', ' ', false); + $mform->setDefault('some_radios', 1); + + $mform->addElement('filepicker', 'filepicker', 'File picker', null, ['accepted_types' => '*']); + + $mform->addElement('text', 'inputtext1', 'Disabled when the file picker has a file'); + $mform->setType('inputtext1', PARAM_RAW); + + $mform->addElement('text', 'inputtext2', 'Hidden when the file picker has a file'); + $mform->setType('inputtext2', PARAM_RAW); + + // Disabled the file picker by selecting the radio button. + $mform->disabledIf('filepicker', 'some_radios', 'eq', '2'); + + // Hide the file picker by selecting the radio button. + $mform->hideIf('filepicker', 'some_radios', 'eq', '3'); + + // Disabled the input text by uploading a file to the file picker. + $mform->disabledIf('inputtext1', 'filepicker', 'noteq', ''); + + // Hide the input text by uploading a file to the file picker. + $mform->hideIf('inputtext2', 'filepicker', 'neq', ''); + } +} + +$form = new test_filepicker_hideif_disabledif_form(); + +echo $OUTPUT->header(); +$form->display(); +echo $OUTPUT->footer();