Bladeren bron

Add MinText validation to QuickForm rules to check for the minimum length of a text

Yannick Warnier 8 jaren geleden
bovenliggende
commit
7899fe2899

+ 2 - 0
main/badge/assign.php

@@ -115,6 +115,8 @@ $form->addRule('skill', get_lang('ThisFieldIsRequired'), 'required');
 $form->addSelect('acquired_level', get_lang('AcquiredLevel'), $acquiredLevel);
 $form->addRule('acquired_level', get_lang('ThisFieldIsRequired'), 'required');
 $form->addTextarea('argumentation', get_lang('Argumentation'), ['rows' => 6]);
+$form->addRule('argumentation', get_lang('ThisFieldIsRequired'), 'required');
+$form->addRule('argumentation', sprintf(get_lang('ThisTextShouldBeAtLeastXCharsLong'), 10), 'mintext', 10);
 $form->applyFilter('argumentation', 'trim');
 $form->addButtonSave(get_lang('Save'));
 $form->setDefaults($formDefaultValues);

+ 21 - 0
main/inc/lib/pear/HTML/QuickForm/Rule/MinText.php

@@ -0,0 +1,21 @@
+<?php
+/* For licensing terms, see /license.txt */
+/**
+ * QuickForm rule to check a text field has a minimum of X chars
+ * @package chamilo.include
+ */
+class Html_Quickform_Rule_MinText extends HTML_QuickForm_Rule
+{
+	/**
+	 * Function to check a text field has a minimum of X chars
+	 * @see HTML_QuickForm_Rule
+	 * @param string $text A text
+     * @param int $count The minimum number of characters that the text should contain
+	 * @return boolean True if text has the minimum number of chars required
+	 */
+	function validate($text, $count)
+	{
+		$checkMinText = create_function('$a,$b', 'return strlen(utf8_decode($a)) >= $b;');
+        return $checkMinText($text, $count);
+	}
+}

+ 2 - 2
main/inc/lib/pear/HTML/QuickForm/RuleRegistry.php

@@ -151,7 +151,8 @@ class HTML_QuickForm_RuleRegistry
             'maxfilesize', 'HTML_QuickForm_Rule_MaxFileSize',
             'mimetype', 'HTML_QuickForm_Rule_MimeType',
             'filename', 'HTML_QuickForm_Rule_FileName',
-            'validquestiontype' => 'HTML_QuickForm_Rule_QuestionType'
+            'validquestiontype' => 'HTML_QuickForm_Rule_QuestionType',
+            'mintext' => 'Html_Quickform_Rule_MinText'
         );
 
         $class = $rules[$ruleName];
@@ -192,7 +193,6 @@ class HTML_QuickForm_RuleRegistry
 
             return ($result == 0) ? false : $result;
         } else {
-
             return $rule->validate($values, $options);
         }
     } // end func validate