Moodle moodleform::validation()

Moodle moodleform::validation()

by delta force -
Number of replies: 3

In my custom plugin i am simply using three drop down and one text box but when i submit the form and validation($data) method is invoked then i just get value of state drop down along with value of text box. Value of other two drop downs is not returned. I am not sure what i am missing. Here is my code:-

<?php
if (!defined('MOODLE_INTERNAL')) {
    die('Direct access to this script is forbidden.');   
}
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;
        $id = optional_param('id', 0, PARAM_INT);
        $err = array();
        if ($DB->record_exists('ohio_schools', array(
                            'school_name'=>$data['school_name'],
                            'city_id'=>$data['city_id'],
                            'county_id'=>$data['county_id'],
                            'state_id'=>$data['state_id']
                            ))) {
            $err['value'] = get_string('err_coursename', 'local_kase');
            echo "record exists";
            exit;
        }else{
            echo "does not exist";
            exit;
        }
        
        if (count($err) == 0){
            return true;
        }else{
            return $err;
        }
    }

    function get_states($id=0, $country_id=0, $fields = "*", $orderby = "state_name"){
        global $DB;
        $condition = $id>0 ? " AND id = '".$id."'" : "";
        $sql = "SELECT $fields FROM {ohio_states} WHERE 1 $condition";                
        $countries = $DB->get_records_sql($sql);
        return $countries;
    }
    
}

Any help?

Attachment array.JPG
Average of ratings: -
In reply to delta force

Re: Moodle moodleform::validation()

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Moodleform validates select inputs on the server-side, using the list of valid option values. (So you don't need $mform->setType). So, if you are only populating the select using Ajax, then that won't work.

There are various ways to do this:

  • Make a new formslib field type.
  • Hack the form definition code, so that if a value is selected in the first drop-down, then you populate the second drop-down with the correct values, etc.
In reply to Tim Hunt

Re: Moodle moodleform::validation()

by delta force -

Thanks for your response Tim. I got the reason why it was not working because i was creating blank county and city drop down on page load. So to fix it i simply displayed counties and cities for id 1 (one). This is just a little hack but it worked for me.

 

In reply to Tim Hunt

Re: Moodle moodleform::validation()

by delta force -

Hey Tim,

I actually overlooked. sorry for that... can you please instruct that how can i create a new formslib field type OR Hack the form definition code?