Moodle

Using compare rule results in fatal error in getValidationScript function

Details

  • Type: Bug Bug
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: 1.9.2
  • Fix Version/s: None
  • Component/s: Forms Library
  • Labels:
    None
  • Difficulty:
    Easy
  • Affected Branches:
    MOODLE_19_STABLE

Description

Improving usability of question editing I found that code below result in error:

$mform->addElement('text',$repeatname,$repeatstring,array('size'=>3));
$mform->addElement('hidden', $repeathiddenname, $repeats);
$mform->addRule($repeatname,null,'required',null,'client',true);
$mform->addRule($repeatname,null,'nonzero',null,'client',true);
//$mform->addRule(array($repeatname, $repeathiddenname),'You can\'t delete blanks','compare','>=','client',true);//uncommenting this line results in error in getValidationScript

If you uncomment the last line you receive
"Fatal error: Call to a member function on a non-object in z:\home\moodle\www\lib\formslib.php"
The line with error is "$onBlur = $element->getAttribute('onBlur');" in function getValidationScript

Issue Links

Activity

Hide
Oleg Sychev added a comment -

The core of the issue I think is the assumption, that $element part of the rule is always an element, while in 'compare' rule it must be an array. This will be pretty easy to fix. Could you please fix it because now it's a sort of blocker for repeat_elements improvement.

Show
Oleg Sychev added a comment - The core of the issue I think is the assumption, that $element part of the rule is always an element, while in 'compare' rule it must be an array. This will be pretty easy to fix. Could you please fix it because now it's a sort of blocker for repeat_elements improvement.
Hide
Oleg Sychev added a comment -

There is a very rough fix: you must replace
$onBlur = $element->getAttribute('onBlur');
$onChange = $element->getAttribute('onChange');
$element->updateAttributes(array('onBlur' => $onBlur . $valFunc,
'onChange' => $onChange . $valFunc));
with
if(is_array($element)){ $onBlur = $element[0]->getAttribute('onBlur'); $onChange = $element[0]->getAttribute('onChange'); $element[0]->updateAttributes(array('onBlur' => $onBlur . $valFunc, 'onChange' => $onChange . $valFunc)); }else{ $onBlur = $element->getAttribute('onBlur'); $onChange = $element->getAttribute('onChange'); $element->updateAttributes(array('onBlur' => $onBlur . $valFunc, 'onChange' => $onChange . $valFunc)); }

This doesn't really solves problem as first element may be hidden (than we must use the second), and if validation fails then error message isn't displayed, but at least there is no Fatal Errors and validation seems to work.

Show
Oleg Sychev added a comment - There is a very rough fix: you must replace $onBlur = $element->getAttribute('onBlur'); $onChange = $element->getAttribute('onChange'); $element->updateAttributes(array('onBlur' => $onBlur . $valFunc, 'onChange' => $onChange . $valFunc)); with if(is_array($element)){ $onBlur = $element[0]->getAttribute('onBlur'); $onChange = $element[0]->getAttribute('onChange'); $element[0]->updateAttributes(array('onBlur' => $onBlur . $valFunc, 'onChange' => $onChange . $valFunc)); }else{ $onBlur = $element->getAttribute('onBlur'); $onChange = $element->getAttribute('onChange'); $element->updateAttributes(array('onBlur' => $onBlur . $valFunc, 'onChange' => $onChange . $valFunc)); } This doesn't really solves problem as first element may be hidden (than we must use the second), and if validation fails then error message isn't displayed, but at least there is no Fatal Errors and validation seems to work.
Hide
Jamie Pratt added a comment -

Hi Oleg,

What about just adding a select drop down box to select how many blanks to add to the form?

Jamie

Show
Jamie Pratt added a comment - Hi Oleg, What about just adding a select drop down box to select how many blanks to add to the form? Jamie
Hide
Oleg Sychev added a comment -

First of all I think that ability to clearly see the number of blanks and enter it is far better that to search for the current number of blanks and calculate how many there is need to add. I can't see any advantage of selection number for adding on selection number of blanks. This doesn't exclude dropdowns by itself.

I tried dropdown box, but drop it because it's not highly usable (and accessible too - see Pierre's comment) with long list of numbers: I can get two digits number in text field by pressing two buttons - how many buttons I must press (or how much must I scroll down) to get any two-digits number in dropdown? On Internet Explorer (widely used by staff of our University) - too many.

Dropdowns are good when you have 3-7 choices. WIth more than 10 choices it's not very usable. Pity we doesn't have spinbox control in HTTP Forms, it's very good for entering numbers.

P.S. There isn't even a note in documentation that 'compare' rules doesn't supported by Moodle forms, just link to PEAR site with all types of rules.

Show
Oleg Sychev added a comment - First of all I think that ability to clearly see the number of blanks and enter it is far better that to search for the current number of blanks and calculate how many there is need to add. I can't see any advantage of selection number for adding on selection number of blanks. This doesn't exclude dropdowns by itself. I tried dropdown box, but drop it because it's not highly usable (and accessible too - see Pierre's comment) with long list of numbers: I can get two digits number in text field by pressing two buttons - how many buttons I must press (or how much must I scroll down) to get any two-digits number in dropdown? On Internet Explorer (widely used by staff of our University) - too many. Dropdowns are good when you have 3-7 choices. WIth more than 10 choices it's not very usable. Pity we doesn't have spinbox control in HTTP Forms, it's very good for entering numbers. P.S. There isn't even a note in documentation that 'compare' rules doesn't supported by Moodle forms, just link to PEAR site with all types of rules.
Hide
Michael de Raadt added a comment -

Thanks for reporting this issue.

We have detected that this issue has been inactive for over a year has been recorded as affecting versions that are no longer supported.

If you believe that this issue is still relevant to current versions (2.1 and beyond), please comment on the issue. Issues left inactive for a further month will be closed.

Michael d;

lqjjLKA0p6

Show
Michael de Raadt added a comment - Thanks for reporting this issue. We have detected that this issue has been inactive for over a year has been recorded as affecting versions that are no longer supported. If you believe that this issue is still relevant to current versions (2.1 and beyond), please comment on the issue. Issues left inactive for a further month will be closed. Michael d; lqjjLKA0p6
Hide
Oleg Sychev added a comment -

I temporarely have no means to re-test it, but as far as I know there were no serious rework of forms library in Moodle 2.x versions so the bug is likely to persist.

Show
Oleg Sychev added a comment - I temporarely have no means to re-test it, but as far as I know there were no serious rework of forms library in Moodle 2.x versions so the bug is likely to persist.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated: