function index($locator) { // get parameter object from controller $this->addField($param1 = new A_Controller_Form_Field('field1')); $param1->addFilter(new A_Filter_Regexp('/[^0-9]/', '')); $param1->addRule(new A_Rule_Notnull('field1', 'Please enter Field 1')); $param1->addRule(new A_Rule_Range(1, 10, 'field1', 'Field 1 must be 1-10')); $param1->setType(array('renderer' => 'A_Html_Form_Select', 'values' => array(5, 10, 15), 'labels' => array('five', 'ten', 'fifteen'))); $this->addField($param2 = new A_Controller_Form_Field('field2')); $param2->addFilter(new A_Filter_Regexp('/[^0-9]/', '')); $param2->addRule(new A_Rule_Notnull('field2', 'Please enter Field 2')); $param2->addRule(new A_Rule_Match('field1', 'field2', 'Field 2 must match Field 1')); $param2->setType(array('renderer' => 'A_Html_Form_Text', 'size' => '10')); $this->addField($param3 = new A_Controller_Form_Field('field3')); $param3->addFilter(new A_Filter_Regexp('/[^a-zA-Z]/', '')); $param3->addRule(new A_Rule_Length(5, 20, 'field3', 'Field 3 must be 5-20 characters')); // create parameter object then add it to the controller $this->addField($param4 = new A_Controller_Form_Field('field4')); $param4->addFilter(new A_Filter_Regexp('/[^a-zA-Z]/', '')); $param4->addFilter(new A_Filter_ToUpper()); $param4->addRule(new A_Rule_Notnull('field4', 'Please enter Field 4')); parent::run($locator); }
/** * Indexes of $attr: * 'type' = 'print_query' prints the query values * 'select_query' creates a select from query values * 'checkbox_query' creates checkboxes from query values * 'radio_query' creates radios from query values * 'sql' contains SQL for query * 'value_field' specifices the column for values * 'label_fields' specifices the column for labels. Multi column|column|column concatenated * 'separator' for concatenating column values * * @param array $attr * @return string */ public function toQuery($attr) { if (!isset($this->db)) { return 'Error: no DB. '; } switch ($attr['type']) { case 'print_query': $attr['type'] = 'print'; $doquery = true; break; case 'select_query': $attr['type'] = 'select'; $doquery = true; break; case 'checkbox_query': $attr['type'] = 'checkbox'; $doquery = true; break; case 'radio_query': $attr['type'] = 'radio'; $doquery = true; break; default: $attr['type'] = ''; } if ($attr['type']) { $sql = $attr['sql']; unset($attr['sql']); if (isset($attr['separator'])) { $separator = $attr['separator']; } else { $separator = ' '; } $val = ''; $str = array(); $txt = array(); // if previous query used the same sql then get data from cache rather than query again if (isset($this->query_cache)) { foreach ($this->query_cache as $query) { if ($query['sql'] == $sql) { $val = $query['val']; $txt = $query['txt']; } } } // if data not found in the cache then query database if (!$val) { $res = $this->db->query($sql); if ($this->db->isError()) { $this->errorMsg .= $this->db->getErrorMsg() . '. '; } unset($val); unset($txt); if ($attr['type'] == 'print') { $row = $res->fetchRow(); $val = null; if (is_array($row)) { foreach ($row as $i) { if ($i) { $val[] = $i; } } } if ($val) { $str = implode($separator, $val); } else { $str = $attr['default']; // no values from query then set default value } } else { $i = 0; // multiple fields can be combined for the text label $textfields = explode('|', $attr['label_fields']); while ($option = $res->fetchRow()) { $val[$i] = $option[$attr['value_field']]; $txt[$i] = ''; foreach ($textfields as $tf) { // concat multiple text labels $txt[$i] .= $option[$tf] . $separator; } $i++; } } $query['sql'] = $sql; $query['val'] = $str; $query['txt'] = $str; $this->query_cache[] = $query; } // assign to form array so it works like the form types below if ($attr['type'] == 'print') { $attr['value'] = $query['val']; return A_Controller_Form_Field::toPrint($attr); } else { $attr['values'] = $val; $attr['labels'] = $txt; return A_Html_Form_Field::toHTML($attr); } } }