Moodle
  1. Moodle
  2. MDL-8886

Add new "in" condition for $mform->disabledIf in Moodle forms

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.9
    • Fix Version/s: None
    • Component/s: Forms Library
    • Labels:
      None
    • Affected Branches:
      MOODLE_19_STABLE
    • Rank:
      11895

      Description

      I would like to be able to disable a "text" field depending on the value of an associated "select" field.

      Here is some sample code to use for testing.

      • locate the following line "mod/chat/mod_form.php" (around line 48)
        $this->standard_coursemodule_elements();
      • insert the following code just before the above line
        $mform->addElement('header', 'testsection', 'Test Section');
        $HOTPOT_FEEDBACK = array (
        0 => get_string("feedbacknone", "hotpot"),
        1 => get_string("feedbackwebpage", "hotpot"),
        2 => get_string("feedbackformmail", "hotpot"),
        3 => get_string("feedbackmoodleforum", "hotpot"),
        4 => get_string("feedbackmoodlemessaging", "hotpot")
        );
        $mform->addElement('select', 'studentfeedback', 'Student Feedback', $HOTPOT_FEEDBACK);
        $mform->addElement('text', 'studentfeedbackurl', 'Student Feedback URL');
        $mform->disabledIf('studentfeedbackurl', 'studentfeedback', 'in', '0,3,4');
      • locate the following lines in "lib/javascript-static.js" (around line 99)
        case 'noitemselected':
        lock = master.selectedIndex==-1; break;
      • insert the following code just after the above lines
        case 'in':
        lock = false;
        var values = value.split(',');
        for (var i in values)
        Unknown macro: { if (values[i]==master.options[master.selectedIndex].value) { lock = true; } }

        break;

      • login to Moodle as a teacher/admin
      • add a "Chat" activity in the course and view the fields on the "Adding a Chat" page
        (you may need to "force-refresh" the page, Ctrl+F5, to get the new javascript file to load)

      This allows the text field to be used to take extra information for certain values of the select field, and I believe it would prove useful in other situation beyond the example above which is taken from the HotPot module.

      thanks in advance for considering this option
      Gordon

        Activity

        Hide
        Jamie Pratt added a comment -

        hi Gordon,

        Don't have time to test this but would :

        $mform->disabledIf('studentfeedbackurl', 'studentfeedback', 'eq', 0);
        $mform->disabledIf('studentfeedbackurl', 'studentfeedback', 'eq', 3);
        $mform->disabledIf('studentfeedbackurl', 'studentfeedback', 'eq', 4);

        Do what you want to do here?

        Jamie

        Show
        Jamie Pratt added a comment - hi Gordon, Don't have time to test this but would : $mform->disabledIf('studentfeedbackurl', 'studentfeedback', 'eq', 0); $mform->disabledIf('studentfeedbackurl', 'studentfeedback', 'eq', 3); $mform->disabledIf('studentfeedbackurl', 'studentfeedback', 'eq', 4); Do what you want to do here? Jamie
        Hide
        Gordon Bateson added a comment -

        Hi Jamie,
        thanks for that suggestion. Actually, I had already tried that, and unfortunately it doesn't achieve the required effect.

        when "studentfeedback" is 0, "studentfeedbackurl" field is left ENABLED
        when "studentfeedback" is 3, "studentfeedbackurl" field is left ENABLED
        when "studentfeedback" is 4, "studentfeedbackurl" field is disabled

        It seems only the LAST "disabledIf" setting is effective.

        I will wait till you have a bit more time to test the solution I offered.

        cheers
        Gordon

        Show
        Gordon Bateson added a comment - Hi Jamie, thanks for that suggestion. Actually, I had already tried that, and unfortunately it doesn't achieve the required effect. when "studentfeedback" is 0, "studentfeedbackurl" field is left ENABLED when "studentfeedback" is 3, "studentfeedbackurl" field is left ENABLED when "studentfeedback" is 4, "studentfeedbackurl" field is disabled It seems only the LAST "disabledIf" setting is effective. I will wait till you have a bit more time to test the solution I offered. cheers Gordon
        Hide
        Gordon Bateson added a comment -

        Hi Jamie,
        just wondering if you have had a chance to try the sample code I gave earlier?

        all the best
        Gordon

        Show
        Gordon Bateson added a comment - Hi Jamie, just wondering if you have had a chance to try the sample code I gave earlier? all the best Gordon
        Hide
        Jamie Pratt added a comment -

        I think it would make sense to add a condition like this to the formslib. Can you commit this to 1.7, 1.8 and HEAD. The formslib api stuff has just been backported to 1.7 (but not the changes to the mod and qtype editing forms).

        Show
        Jamie Pratt added a comment - I think it would make sense to add a condition like this to the formslib. Can you commit this to 1.7, 1.8 and HEAD. The formslib api stuff has just been backported to 1.7 (but not the changes to the mod and qtype editing forms).
        Hide
        Gordon Bateson added a comment -

        Thanks Jamie.
        I don't have sufficient "karma" to update "lib/formslib.php". Can you do it, or shall we ask someone else?

        Show
        Gordon Bateson added a comment - Thanks Jamie. I don't have sufficient "karma" to update "lib/formslib.php". Can you do it, or shall we ask someone else?
        Hide
        Jamie Pratt added a comment -

        Added Petr as a watcher for this post I hope he can review the patch I attach. It seems that :

        $mform->disabledIf('studentfeedbackurl', 'studentfeedback', 'eq', 0);
        $mform->disabledIf('studentfeedbackurl', 'studentfeedback', 'eq', 3);
        $mform->disabledIf('studentfeedbackurl', 'studentfeedback', 'eq', 4);

        not working was due to a bug in the lockoptionsall js function. The array of lockoptions was getting output properly but effectively only the last condition was being tested as the result of that was being used to set the disable property. I'll attach a patch to OR the results of the conditions.

        I suggest we should commit this into 1.7, 1.8 and HEAD.

        Show
        Jamie Pratt added a comment - Added Petr as a watcher for this post I hope he can review the patch I attach. It seems that : $mform->disabledIf('studentfeedbackurl', 'studentfeedback', 'eq', 0); $mform->disabledIf('studentfeedbackurl', 'studentfeedback', 'eq', 3); $mform->disabledIf('studentfeedbackurl', 'studentfeedback', 'eq', 4); not working was due to a bug in the lockoptionsall js function. The array of lockoptions was getting output properly but effectively only the last condition was being tested as the result of that was being used to set the disable property. I'll attach a patch to OR the results of the conditions. I suggest we should commit this into 1.7, 1.8 and HEAD.
        Hide
        Jamie Pratt added a comment -

        Attached the patch. I tested this in HEAD with Gordon's test case and other forms with disabledIf and it seems to work well. Can't see any reason why there might be any cross browser issues with this.

        Show
        Jamie Pratt added a comment - Attached the patch. I tested this in HEAD with Gordon's test case and other forms with disabledIf and it seems to work well. Can't see any reason why there might be any cross browser issues with this.
        Hide
        Petr Škoda added a comment -

        The logic seems ok, my +1 for the patch

        Show
        Petr Škoda added a comment - The logic seems ok, my +1 for the patch
        Hide
        Gordon Bateson added a comment -

        Thanks Petr.

        Neither Jamie nor I can sufficient permissions to update "lib/formslib" on the CVS. Could you do it please - or should we ask someone else?

        Show
        Gordon Bateson added a comment - Thanks Petr. Neither Jamie nor I can sufficient permissions to update "lib/formslib" on the CVS. Could you do it please - or should we ask someone else?
        Hide
        Jamie Pratt added a comment -

        I'll do it. I have the karma.

        Show
        Jamie Pratt added a comment - I'll do it. I have the karma.
        Hide
        Jamie Pratt added a comment -

        Committed to 1.7 and 1.8 stable and HEAD.

        Show
        Jamie Pratt added a comment - Committed to 1.7 and 1.8 stable and HEAD.
        Hide
        Gordon Bateson added a comment -

        Thanks Jamie.

        I can confirm that if there are several "disabledIf" conditions, they now behave as if they were combined using "OR", rather than "AND". This will suit my purposes very well.

        much obliged
        Gordon

        Show
        Gordon Bateson added a comment - Thanks Jamie. I can confirm that if there are several "disabledIf" conditions, they now behave as if they were combined using "OR", rather than "AND". This will suit my purposes very well. much obliged Gordon

          People

          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: