Welcome Guest, Not a member yet? Register   Sign In
Transmit parameters to form_validation.php
#2

[eluser]Dan Tdr[/eluser]
I figured it out, i think.

I extended the ci_form_validation
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package        CodeIgniter
* @author         ExpressionEngine Dev Team
* @moddedby       Daniel Toader( www.compactdesign.ro )
* @copyright      Copyright (c) 2008 - 2010, EllisLab, Inc.
* @license        http://ellislab.com/codeigniter/user-guide/license.html
* @link           http://codeigniter.com
* @since          Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------

class MY_Form_validation extends CI_Form_validation {
    
function run($group = '', $var = '')
    {
        // Do we even have any data to process?  Mm?
        if (count($_POST) == 0)
        {
            return FALSE;
        }

        // Does the _field_data array containing the validation rules exist?
        // If not, we look to see if they were assigned via a config file
        if (count($this->_field_data) == 0)
        {
            // No validation rules?  We're done...
            if (count($this->_config_rules) == 0)
            {
                return FALSE;
            }

            // Is there a validation rule for the particular URI being accessed?
            $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;

            if ($uri != '' AND isset($this->_config_rules[$uri]))
            {
                    for($i=0;$i<count($this->_config_rules[$uri]);$i++):
                        $this->_config_rules[$uri][$i]['field'] = str_replace('_lang', ($var?$var:'') , $this->_config_rules[$uri][$i]['field']);
                    endfor;
                //var_dump($this->_config_rules[$uri]);
                
                $this->set_rules($this->_config_rules[$uri]);
            }
            else
            {
                $this->set_rules($this->_config_rules);
            }

            // We're we able to set the rules correctly?
            if (count($this->_field_data) == 0)
            {
                log_message('debug', "Unable to find validation rules");
                return FALSE;
            }
        }

        // Load the language file containing error messages
        $this->CI->lang->load('form_validation');

        // Cycle through the rules for each field, match the
        // corresponding $_POST item and test for errors
        foreach ($this->_field_data as $field => $row)
        {
            // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
            // Depending on whether the field name is an array or a string will determine where we get it from.

            if ($row['is_array'] == TRUE)
            {
                $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
            }
            else
            {
                if (isset($_POST[$field]) AND $_POST[$field] != "")
                {
                    $this->_field_data[$field]['postdata'] = $_POST[$field];
                }
            }

            $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
        }

        // Did we end up with any errors?
        $total_errors = count($this->_error_array);

        if ($total_errors > 0)
        {
            $this->_safe_form_data = TRUE;
        }

        // Now we need to re-set the POST data with the new, processed data
        $this->_reset_post_array();

        // No errors, validation passes!
        if ($total_errors == 0)
        {
            return TRUE;
        }

        // Validation fails
        return FALSE;
    }
}

form_validation.php
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config = array(
        'add_servicii' => array(
                        array(
                            'field' => 'service_name_lang',
                            'label' => 'Nume',
                            'rules' => 'required|min_length[3]|max_length[20]|xss_clean'
                        ),
                        array(
                            'field' => 'service_url_lang',
                            'label' => 'URL',
                            'rules' => 'required|xss_clean'
                        ),
                        array(
                            'field' => 'service_content_lang',
                            'label' => 'Continut',
                            'rules' => 'required'
                        ),
                        array(
                            'field' => 'service_description_lang',
                            'label' => 'Descriere [META]',
                            'rules' => 'required|min_length[10]|max_length[160]|xss_clean'
                        )
                    ),                
               );


/* End of file form_validation.php */
/* Location: ./application/config/form_validation.php */

And Use:

Code:
if ($this->form_validation->run('add_servicii', $lang) == FALSE):
// do something
else:
// do something else
endif;

if the 2nd parameter $lang is not present all works as normal, but if it is, it replaces the _lang string from the rules defined in form_validation with the language you need
Note: my lang variable is something like: "_languagecode" (ex: _ro, _en, _fr)

so what do you say, is this way of looking at things a good one? or you have a better idea?
thanks,
Dan


Messages In This Thread
Transmit parameters to form_validation.php - by El Forum - 04-22-2011, 04:28 PM
Transmit parameters to form_validation.php - by El Forum - 04-23-2011, 04:19 AM



Theme © iAndrew 2016 - Forum software by © MyBB