Welcome Guest, Not a member yet? Register   Sign In
[library concept] Harmony (JavaScript and Server side validation simplified)
#1

[eluser]litzinger[/eluser]
I have no idea if this has been attempted and if I'm trying to reinvent the wheel here, but last night I had an idea to try and combine my favorite JS form validation library (jQuery and http://jquery.bassistance.de/validate/demo-test/) with CI. Usually I have to type in my rules twice, and maintain them in two locations for each form. Below is my first attempt at combining the two. The idea is to put all the form values into a config array, and use that array to populate the CI form validation fields and rules, and to print out the rules in the HTML form fields to be used by the jQuery validation. Below is a simple concept, and I'm stuck on setting the CI messages (via set_message()) to display the custom message when JS is disabled.

Let me know if this is a waste of time, or if I'm onto something.

site_settings.php
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

$config['harmony_forms']['test_form'] = array(
        'name' => array(
                'type' => 'text',
                'rules' => 'required',
                'label' => 'First Name',
                'error_message' => 'Please enter your first name'
            ),
        'email' => array(
                'type' => 'text',
                'rules' => 'required|valid_email',
                'label' => 'Email Address',
                'error_message' => 'Please enter a valid email address'
            )    
    );

?>

Harmony.php
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Harmony {
    
    function Harmony()
    {
        $CI =& get_instance();
        $this->forms = $CI->config->item('harmony_forms');
    }
    
    function pre_form($form_class, $current_form, $error_class = false)
    {
        $CI =& get_instance();
        $this->current_form = $current_form;
        $error_class = ($error_class) ? $error_class : 'error';
                
        $print = '
                    $(function(){  
                        $("form.' . $form_class . '").validate({
                            focusInvalid: true,
                            errorLabelContainer: $("ol.' . $error_class . '"),
                            wrapper: "li"
                        });
                    })
                ';
        $print .= '<ol class="' . $error_class . '">' . $CI->validation->error_string . '</ol>';
        
        return $print;
    }
    
    function input($field_name, $field_value, $field_attributes)
    {
        $CI =& get_instance();
        $print = NULL;
        $type = $this->_get_attr_val($field_name, 'type');
        $title = $this->_get_attr_val($field_name, 'error_message');
        $field_value = ($field_value) ? $field_value : $CI->input->post($field_name);

        switch($type)
        {
            case 'text':
                $print = '&lt;input type="text" name="' . $field_name . '" value="' . $field_value . '" title="' . $title . '" ' . $this-&gt;_print_attributes($field_name, $field_attributes) . '  />';
            break;
        }

        return $print;
    }
    
    function _print_attributes($field_name, $field_attributes)
    {
        /* the class attribute should be the only real unique attribute here, b/c the JS library is looking for the meta data within the class */
        foreach($field_attributes as $attr_name => $attr_value)
        {
            switch($attr_name)
            {
                case 'class':
                    $print = 'class="' . $attr_value . ' {' . rtrim($this->_ci_to_jquery($this->_get_attr_val($field_name, 'rules')), ',') . '}"';
                break;
                default:
                    $print = $attr_name . '=' . $attr_value;
                break;
            }
            
            return $print;
        }  
    }
    
    function _ci_to_jquery($rules)
    {
        /* jQuery's form validation rule titles are slightly different than CI's, so lets match them up.
        since they are different, we probably won't end up with a match for every rule in the end.
        */
        $print = NULL;
        $rules = explode('|', $rules);
        foreach($rules as $rule)
        {
            switch($rule)
            {
                case 'required':
                    $print .= 'required: true,';
                break;
                case 'valid_email':
                    $print .= 'email: true,';
                break;
            }
        }        
        
        return $print;
    }
    
    function _get_attr_val($field_name, $attr_name)
    {
        return $this->forms[$this->current_form][$field_name][$attr_name];
    }
    
    
}

?&gt;


Messages In This Thread
[library concept] Harmony (JavaScript and Server side validation simplified) - by El Forum - 08-07-2007, 02:18 PM



Theme © iAndrew 2016 - Forum software by © MyBB