2014-11-22 111 views
0

我需要自定義驗證提交,我做了如下編碼:Moodle的驗證

<?php 

require_once("{$CFG->libdir}/formslib.php"); 

class pool_status_form extends moodleform { 

    function definition() { 
global $DB, $USER, $qnsCount; 

     $mform =&$this->_form; 
//number of question per user 
$mform->addElement('text', 'peruser', get_string('peruserquestion', 'qpool'), array('id' => 'peerqn')); 
$mform->addRule('peruser', null, 'required', null, 'client'); 
$mform->addRule('peruser', null, 'numeric', null, 'client'); 
$mform->setType('peruser', PARAM_RAW); 


//$this->add_action_buttons(false, 'SAVE'); 
$mform->addElement('submit', 'subbtn', get_string("buttonlabel", "qpool")); 

    } 


public function validation($data, $files) { 


$errors = parent::validation($data, $files); 

if (($data['peruser'])>3) { 

$errors['peruser'] = "Error.."; 

} 

return $errors; 
} 
} 

當我點擊提交按鈕,它直接提交,但不檢查我已內提到的驗證我的

'函數驗證'。我該怎麼辦??

回答

0

「驗證」功能在服務器上運行。只需調用$ form-> get_data(),它就會觸發。

另外,你爲什麼要兩次添加'required'規則? 爲什麼數據類型PARAM_RAW - 它看起來像PARAM_INT給我。