Ver Fonte

[svn r11871] add a rule in quickform : multiple required.
It allows to check if at least one element in an array has been filled.

Eric Marguin há 18 anos atrás
pai
commit
6631ca58a8

+ 6 - 0
main/exercice/multiple_answer.class.php

@@ -109,6 +109,8 @@ class MultipleAnswer extends Question {
 		}
 
 		$form -> addElement('hidden', 'nb_answers');
+		
+		$boxes_names = array();
 
 		for($i = 1 ; $i <= $nb_answers ; ++$i)
 		{
@@ -128,6 +130,8 @@ class MultipleAnswer extends Question {
 			$answer_number->freeze();
 			
 			$form->addElement('checkbox', 'correct['.$i.']', null, null, $i);
+			$boxes_names[] = 'correct['.$i.']';
+			
 			$form->addElement('html_editor', 'answer['.$i.']',null, 'style="vertical-align:middle"');
 			$form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
 			$form->addElement('html_editor', 'comment['.$i.']',null, 'style="vertical-align:middle"');
@@ -135,6 +139,8 @@ class MultipleAnswer extends Question {
 			$form -> addElement ('html', '</tr>');
 		}
 		$form -> addElement ('html', '</table>');
+		
+		$form -> add_multiple_required_rule ($boxes_names , get_lang('ChooseAtLeastOneCheckbox') , 'multiple_required');
 
 		$form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'));
 		$form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'));

+ 13 - 0
main/inc/lib/formvalidator/FormValidator.class.php

@@ -63,6 +63,7 @@ class FormValidator extends HTML_QuickForm
 		$this->registerRule('username_available',null,'HTML_QuickForm_Rule_UsernameAvailable',$dir.'Rule/UsernameAvailable.php');
 		$this->registerRule('username',null,'HTML_QuickForm_Rule_Username',$dir.'Rule/Username.php');
 		$this->registerRule('filetype',null,'HTML_QuickForm_Rule_Filetype',$dir.'Rule/Filetype.php');
+		$this->registerRule('multiple_required','required','HTML_QuickForm_Rule_MultipleRequired',$dir.'Rule/MultipleRequired.php');
 
 		// Modify the default templates
 		$renderer = & $this->defaultRenderer();
@@ -286,6 +287,18 @@ EOT;
 		
 	}
 	
+	/**
+	 * This function avoid to change directly QuickForm class.
+	 * When we use it, the element is threated as 'required' to be dealt during validation
+	 * @param array $element the array of elements
+	 * @param string $message the message displayed
+	 */
+	function add_multiple_required_rule($elements, $message)
+	{
+		$this->_required[] = $elements[0];
+		$this -> addRule ($elements , get_lang('YouMustCheckACheckbox') , 'multiple_required');		
+	}
+	
 	/**
 	 * Display the form.
 	 * If an element in the form didn't validate, an error message is showed

+ 49 - 0
main/inc/lib/formvalidator/Rule/MultipleRequired.php

@@ -0,0 +1,49 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------+
+// | PHP version 4.0                                                      |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 1997-2003 The PHP Group                                |
+// +----------------------------------------------------------------------+
+// | Copy of the existing rule "required" to check if at least one element|
+// | has been filled. Then $value can be an array						  |
+// +----------------------------------------------------------------------+
+// | Authors: Eric Marguin <e.marguin@elixir-interactive.com>             |
+// +----------------------------------------------------------------------+
+
+require_once('HTML/QuickForm/Rule.php');
+
+/**
+* Required elements validation
+* @version     1.0
+*/
+class HTML_QuickForm_Rule_MultipleRequired extends HTML_QuickForm_Rule
+{
+    /**
+     * Checks if all the elements are empty
+     *
+     * @param     string    $value      Value to check (can be an array)
+     * @param     mixed     $options    Not used yet
+     * @access    public
+     * @return    boolean   true if value is not empty
+     */
+    function validate($value, $options = null)
+    {
+    	if(is_array($value))
+    	{
+    		$value = implode(null,$value);
+    	}
+        if ((string)$value == '') {
+            return false;
+        }
+        return true;
+    } // end func validate
+
+
+    function getValidationScript($options = null)
+    {
+        return array('', "{jsVar} == ''");
+    } // end func getValidationScript
+
+} // end class HTML_QuickForm_Rule_MultipleRequired
+?>