Welcome Guest, Not a member yet? Register   Sign In
Yet Another Form Validation Extension
#1

[eluser]loosetops[/eluser]
The CI form validation class makes validating forms easy and users happy but once you have used it more than few times a little pain comes back because it is still a chore.

A while back I tried Macigniter's lib and it was nice, then later I looked back at the code and I didn't like that I was putting html in my controllers.

On close examination what I didn't like with form validation was creating those arrays

Like this one
Code:
$config = array(
                 'signup' => array(
                                    array(
                                            'field' => 'username',
                                            'label' => 'Username',
                                            'rules' => 'required'
                                         ),
                                    array(
                                            'field' => 'password',
                                            'label' => 'Password',
                                            'rules' => 'required'
                                         ),
                                    array(
                                            'field' => 'passconf',
                                            'label' => 'PasswordConfirmation',
                                            'rules' => 'required'
                                         ),
                                    array(
                                            'field' => 'email',
                                            'label' => 'Email',
                                            'rules' => 'required'
                                         )
                                    ),
                 'email' => array(
                                    array(
                                            'field' => 'emailaddress',
                                            'label' => 'EmailAddress',
                                            'rules' => 'required|valid_email'
                                         ),
                                    array(
                                            'field' => 'name',
                                            'label' => 'Name',
                                            'rules' => 'required|alpha'
                                         ),
                                    array(
                                            'field' => 'title',
                                            'label' => 'Title',
                                            'rules' => 'required'
                                         ),
                                    array(
                                            'field' => 'message',
                                            'label' => 'MessageBody',
                                            'rules' => 'required'
                                         )
                                    )                          
               );

This Form_Validation extension makes it less verbose, e.g.
Code:
$config['signup'] = array(
   array('username','Username','required'),
   array('password','Password','required'),
   array('passconf','PasswordConfirmation','required'),
   array('email','Email','required')
   );
$config['email'] = array(
   array('emailaddress','EmailAddress','required|valid_email'),
   array('name','Name','required|alpha'),
   array('title','Title','required'),
   array('message','MessageBody','required')
   );

The extension,

application/libraries/MY_Form_validation.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');
class MY_Form_validation extends CI_Form_validation
{
    function set_rules($field, $label = '', $rules = ''){
        if(is_array($field) && !isset($field[0]['rules']) ) {
            $keys = array('field','label','rules');
            foreach($field as &$field_rules){
                $field_rules = array_combine($keys,$field_rules);
            }
        }
        parent::set_rules($field, $label = '', $rules = '');
    }
}

Hope that helps someone.
#2

[eluser]tkyy[/eluser]
really nice, i should have considered this earlier, thanks




Theme © iAndrew 2016 - Forum software by © MyBB