Welcome Guest, Not a member yet? Register   Sign In
CI form 'best practices'
#8

[eluser]bretticus[/eluser]
Well, I'm a relatively new CI user. Perhaps I was used to doing code similar to Derek, but I have had no qualms using form helper within my views and particularity with dynamically-populated input/controls. I too think that having to make blank rules for inputs that I do not need to validate seems like bloat but considering some of my form validation classes from the past, I'm actually a little bit lighter with CI. I NEVER have html meta data in my controller that gets passed to my view (I'd be interested to see a case for that.) And if I have a commonly used validation, I ALWAYS extend the validation class as to include it in my pipe lists of rules.

Here's a recent example:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/*** Brett: Thu Apr 23 16:48:00 GMT 2009
* Extension for adding abitrary error messages and extra rules ***/


class MY_Form_validation extends CI_Form_validation {
    
    function MY_Form_validation($rules=array())
    {
        parent::CI_Form_validation($rules);
    }
    
    function create_arbitrary_error($alias, $message) {
        // Save the error message
        $this->_field_data[$alias]['error'] = $message;
        if ( ! isset($this->_error_array[$alias]))
        {
            $this->_error_array[$alias] = $message;
        }
    }
    
    /**
     * lowercase
     *
     * @access    public
     * @param    string
     * @return    bool
     */    
    function lower($str)
    {
        return ( preg_match("/([A-Z])$/", $str)) ? FALSE : TRUE;
    }
    
    /**
     * lowercase
     *
     * @access    public
     * @param    string
     * @return    bool
     */    
    function phonenumber($str)
    {
        return ( preg_match("/^\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/", $str)) ? TRUE : FALSE;
    }
}


Messages In This Thread
CI form 'best practices' - by El Forum - 08-04-2009, 07:27 AM
CI form 'best practices' - by El Forum - 08-04-2009, 10:36 AM
CI form 'best practices' - by El Forum - 08-04-2009, 12:24 PM
CI form 'best practices' - by El Forum - 08-05-2009, 12:59 AM
CI form 'best practices' - by El Forum - 08-05-2009, 01:34 AM
CI form 'best practices' - by El Forum - 08-05-2009, 01:45 AM
CI form 'best practices' - by El Forum - 08-05-2009, 01:46 AM
CI form 'best practices' - by El Forum - 08-05-2009, 02:25 AM



Theme © iAndrew 2016 - Forum software by © MyBB