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

[eluser]Dan Tdr[/eluser]
Hello,

I have a dilemma, i searched over the web as much as i could (it`s almost 2am so maybe i missed the answer, though i dought it).
I`m trying to make my controllers as readable as possible for other programmers and me Smile

I have a controller that has $lang as a parameter and this code:

Code:
$this->form_validation->set_rules('service_name'.$lang, 'Nume', 'required|min_length[3]|max_length[20]|xss_clean|callback_paginaNume');
$this->form_validation->set_rules('service_url'.$lang, 'URL', 'required|xss_clean');
$this->form_validation->set_rules('service_content'.$lang, 'Continut', 'required');
$this->form_validation->set_rules('service_description'.$lang, 'Descriere [META]', 'required|min_length[10]|max_length[160]|xss_clean');
$this->form_validation->set_rules('service_keywords'.$lang, 'Cuvinte Cheie [META]', 'required|min_length[10]|max_length[160]|xss_clean');
$this->form_validation->set_rules('service_robots', 'Robots [META]', 'required');
$this->form_validation->set_rules('service_visible', 'Vizibila', 'required');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

this works well for me, but i want to add all this info to an array in form_validation.php. Is there any way i could transmit the variable $lang to the form_validation.php file?

so that this code would work?

form_validation.php content below
Code:
$config = array(
        'admin/add_servicii' => array(
                        array(
                            'field' => 'username'[b].$lang[/b],
                            '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'
                        )
                    ),                
               );
Note: the $config variable is taken from the CI userguide and does not really matter, all that matters is the $lang variable.

For example i could insert in my database from the link example.com/adduser/ro the field usernamero, from the link example.com/adduser/en the field usernameen. Keep in mind that the languages could be just 2 or 10, and writing the validation rules in all the languages would be of no use.
Thank you very much,
Dan
#2

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

I extended the ci_form_validation
Code:
&lt;?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




Theme © iAndrew 2016 - Forum software by © MyBB