Welcome Guest, Not a member yet? Register   Sign In
Validation : set_fields : default value [FIX]
#1

[eluser]Brant[/eluser]
When setting fields with preset data the validation is not populated in the validation fields. I was able to fix this by extending the CI_Validation class. Please note the line in MY_Validation.

Quote:$new_value = (!empty($val)) ? $val : '';


@MY_Validation
Code:
/**
     * Set Fields
     *
     * This function takes an array of field names as input
     * and generates class variables with the same name, which will
     * either be blank or contain the $_POST value corresponding to it
     *
     * @access    public
     * @param    string
     * @param    string
     * @return    void
     */
    function set_fields($data = '', $field = '')
    {    
        if ($data == '')
        {
            if (count($this->_fields) == 0)
            {
                return FALSE;
            }
        }
        else
        {
            if ( ! is_array($data))
            {
                $data = array($data => $field);
            }
            
            if (count($data) > 0)
            {
                $this->_fields = $data;
            }
        }        
            
        foreach($this->_fields as $key => $val)
        {
            $new_value = (!empty($val)) ? $val : '';
            $this->$key = ( ! isset($_POST[$key])) ? $new_value : $this->prep_for_form($_POST[$key]);
            
            $error = $key.'_error';
            if ( ! isset($this->$error))
            {
                $this->$error = '';
            }
        }        
    }


@my_controller
Code:
$fields = array();
        $fields['name'] = $oRow->name;
        $this->validation->set_fields($fields);
        
        if ($this->validation->run() == FALSE)
        {
            $this->load->view('view/update');
        }


@view/update
Code:
<table class="datagrid" cellspacing="1" cellpadding="5">
<tbody>
<tr>
    <td>
        &lt;?php echo form_label('Name','form-name');
echo form_input(array('name'=>'name','id'=>'form-name'),$this->validation->name);
echo $this->validation->name_error;
?&gt;</td>
</tr>
<tr>
</tbody>
</table>


I'm not sure if this is because I'm also using the form helpers or not?




Theme © iAndrew 2016 - Forum software by © MyBB