/** * Testcase for validation */ public function test_validation() { // A default select with single values validates the data. $options = array('1' => 'One', 2 => 'Two'); $element = new MoodleQuickForm_autocomplete('testel', null, $options); $submission = array('testel' => 2); $this->assertEquals($element->exportValue($submission), 2); $submission = array('testel' => 3); $this->assertNull($element->exportValue($submission)); // A select with multiple values validates the data. $options = array('1' => 'One', 2 => 'Two'); $element = new MoodleQuickForm_autocomplete('testel', null, $options, array('multiple' => 'multiple')); $submission = array('testel' => array(2, 3)); $this->assertEquals($element->exportValue($submission), array(2)); // A select where the values are fetched via ajax does not validate the data. $element = new MoodleQuickForm_autocomplete('testel', null, array(), array('multiple' => 'multiple', 'ajax' => 'anything')); $submission = array('testel' => array(2, 3)); $this->assertEquals($element->exportValue($submission), array(2, 3)); }
/** * Returns a 'safe' element's value * * @param array $submitValues array of submitted values to search * @param bool $assoc whether to return the value as associative array * @return mixed */ public function exportValue(&$submitValues, $assoc = false) { if (!$this->is_tagging_enabled()) { return $assoc ? array($this->getName() => array()) : array(); } return parent::exportValue($submitValues, $assoc); }