Welcome Guest, Not a member yet? Register   Sign In
Easier SetFields method? (proposed change?)
#4

[eluser]Michael Wales[/eluser]
/application/libraries/MY_Validation.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');

class MY_Validation extends CI_Validation {

function MY_Validation() {
  parent::CI_Validation();
}

function SetFields($data = '', $field = '', $separator = '_')
    {    
        if ($data == '')
        {
            if (count($this->_fields) == 0 && count($this->_rules) == 0)
            {
                return FALSE;
            }
        }
        else
        {
            if ( ! is_array($data))
            {
                $data = array($data => $field);
            }
            
            if (count($data) > 0)
            {
                $this->_fields = $data;
            }
        }    
        
        foreach($this->_rules as $key => $val)
        {                  
            $text = ucwords(str_replace($separator, ' ', $key));            
            $auto_fields[$key] = $text;    
        }  
        
        $this->_fields = array_merge($auto_fields, $this->_fields);    
            
        foreach($this->_fields as $key => $val)
        {        
            $this->$key = ( ! isset($_POST[$key]) OR is_array($_POST[$key])) ? '' : $this->prep_for_form($_POST[$key]);
            
            $error = $key.'_error';
            if ( ! isset($this->$error))
            {
                $this->$error = '';
            }
        }        
    }
}


Work like a charm - a great addition tilzinger - it's saving me a lot of typing.


Messages In This Thread
Easier SetFields method? (proposed change?) - by El Forum - 11-13-2007, 03:59 PM
Easier SetFields method? (proposed change?) - by El Forum - 11-13-2007, 05:33 PM
Easier SetFields method? (proposed change?) - by El Forum - 11-15-2007, 12:01 AM
Easier SetFields method? (proposed change?) - by El Forum - 11-15-2007, 12:34 AM
Easier SetFields method? (proposed change?) - by El Forum - 11-15-2007, 08:23 AM
Easier SetFields method? (proposed change?) - by El Forum - 11-29-2007, 05:02 PM



Theme © iAndrew 2016 - Forum software by © MyBB