CI_Form_validation::set_value PHP Method

set_value() public method

Permits you to repopulate a form field with the value it was submitted with, or, if that value doesn't exist, with the default
public set_value ( $field = '', $default = '' ) : string
return string
    public function set_value($field = '', $default = '')
    {
        if (!isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])) {
            return $default;
        }
        // If the data is an array output them one at a time.
        //	E.g: form_input('name[]', set_value('name[]');
        if (is_array($this->_field_data[$field]['postdata'])) {
            return array_shift($this->_field_data[$field]['postdata']);
        }
        return $this->_field_data[$field]['postdata'];
    }

Usage Example

 /**
  * ----------------------------------------------------------
  * Set Value: Get the value from a form
  * Extends method to return posted data for fields with no rules
  * ----------------------------------------------------------
  * 
  * @param string $field
  * @param string $default
  * @return string
  */
 function set_value($field = '', $default = '')
 {
     // no post?
     if (count($_POST) == 0) {
         return $default;
     }
     // no rules for this field?
     if (!isset($this->_field_data[$field])) {
         $this->set_rules($field, '', '');
         // fieldname is an array
         if ($this->_field_data[$field]['is_array']) {
             $keys = $this->_field_data[$field]['keys'];
             $value = $this->_traverse_array($_POST, $keys);
         } else {
             $value = isset($_POST[$field]) ? $_POST[$field] : FALSE;
         }
         // field was not in the post
         if ($value === FALSE) {
             return $default;
         } else {
             $this->_field_data[$field]['postdata'] = form_prep($value, $field);
         }
     }
     return parent::set_value($field, $default);
 }
All Usage Examples Of CI_Form_validation::set_value