2013-03-22 47 views
1

在我的自定義插件中,我簡單地使用了三個下拉菜單和一個文本框。當我提交表單並調用validation($data)方法時,我只是將狀態值與文本框值一起下拉。沒有返回另外兩個下拉菜單的Moodle moodleform :: validation()

價值。我不知道我錯過了什麼。

這裏是我的代碼:

if (!defined('MOODLE_INTERNAL')) { 
    die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page 
} 
require_once($CFG->libdir.'/formslib.php'); 

class ohio_addconfiguration_form extends moodleform { 

// Define the form 
function definition() { 

    $id = optional_param('id', 0, PARAM_INT); 

    $countries = array(); 
    $states = array(); 
    $counties = array(); 
    $cities = array(); 

    $mform =& $this->_form; 

    // Creating hidden variable id 
    $mform->addElement('hidden', 'id'); 
    $mform->setType('id', PARAM_INT); 

    // Creating header "Configuration" 
    $mform->addElement('header', 'configuration', get_string('ohio', 'local_ohio')); 

    /* Listing States */ 
    $states_result = $this->get_states("", "1", "id, state_name", "state_name ASC");  
    if($states_result) { 
     foreach($states_result as $key=>$state){ 
     $states[$state->id] = $state->state_name; 
     }   
    }  
    $states= count($states)?array(''=>get_string('select_state', 'local_ohio').'...') + $states :array(''=>get_string('select_state', 'local_ohio').'...'); 
    $mform->addElement('select', 'state_id', get_string('select_state', 'local_ohio'), $states); 
    $mform->addRule('state_id', get_string('required'), 'required', null, 'client'); 
    $mform->setType('state_id', PARAM_INT); 

    /* Listing Counties */ 
    $counties= array(''=>get_string('select_county', 'local_ohio').'...'); 
    $mform->addElement('select', 'county_id', get_string('select_county', 'local_ohio'), $counties); 
    $mform->addRule('county_id', get_string('required'), 'required', null, 'client'); 
    $mform->setType('county_id', PARAM_INT); 

    /* Listing Cities */ 
    $cities= array(''=>get_string('select_city', 'local_ohio').'...'); 
    $mform->addElement('select', 'city_id', get_string('select_city', 'local_ohio'), $cities); 
    $mform->addRule('city_id', get_string('required'), 'required', null, 'client'); 
    $mform->setType('city_id', PARAM_INT); 

    // Creating text box for School 
    $mform->addElement('text', 'school_name', get_string('school_name', 'local_ohio'), 'size="25"'); 
    $mform->setType('school_name', PARAM_TEXT); 
    $mform->addRule('school_name', get_string('required'), 'required', null, 'client'); 
    $mform->addRule('school_name', get_string('maximumchars', '', 100), 'maxlength', 100, 'client'); 

    $this->add_action_buttons(); 
} 

function validation($data) { 
    global $DB; 
    echo "<pre>"; 
    print_r($data); 
    exit; 
} 
} 

回答

0

我不知道你在尋找準確,無論是形式驗證或數據檢索,但我假設你感興趣的數據檢索和代碼你上面提供的是用'filename_form.php'寫的。

驗證()方法被用來驗證在服務器側的字段中輸入的數據,並且不獲取字段的值。

要獲得字段的值,你需要它來創建一個名爲「filename.php」另一個文件,包括「filename_form.php」顯示形式。 您可以參考here來使用Formslib。