Welcome Guest, Not a member yet? Register   Sign In
How to import validation rules from file?
#1

[eluser]Leon Stafford[/eluser]
Hi,

Am trying to move my code for multiple forms' config arrays to an external config/settings file.

Should I be using load->config or includes or something else?

My code is below, with a sample config array publicly defined at the top.

If some CI-savvy person could show me how to modify this to call from an external file I'd be grateful.

Code:
<?php
class Save extends Controller {
    public $results = array();
    public $validation_config = array();
    public $shop_validation = array(
               array(
                     'field'   => 'username',
                     'label'   => 'Username',
                     'rules'   => 'trim|required|prep_for_form'
                  ),
               array(
                     'field'   => 'password',
                     'label'   => 'Password',
                     'rules'   => 'trim|required|matches[passconf]|md5'
                  ),
               array(
                     'field'   => 'passconf',
                     'label'   => 'Password Confirmation',
                     'rules'   => 'trim|required'
                  ),  
               array(
                     'field'   => 'email',
                     'label'   => 'Email',
                     'rules'   => 'trim|required|valid_email'
                  )
            );
            
    function Save()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url','html'));
        $this->load->library('form_validation');
    }
    
    function index()
    {    
        
        $this->load->view('upload_form');    
    }

    function file_upload()
    {
        
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '3000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $this->load->library('upload', $config);
        $number_of_files = 4;
        
        for ($file_counter = 0; $file_counter < $number_of_files; $file_counter +=1){
            $this->upload->initialize($config);
            if ( ! $this->upload->do_upload('userfile'.$file_counter))
            {
                $this->results['result'.$file_counter]['error'] = array($this->upload->display_errors('<p style="color:red;">', '</p>'));
                $this->results['result'.$file_counter]['success'] = array('');
            }    
            else
            {
                $this->results['result'.$file_counter]['success'] = array($this->upload->data());
                $this->results['result'.$file_counter]['error'] = array('');
            }
        }
        
    }
        
    function validate_form()
    {
        $this->form_validation->set_error_delimiters('<p style="color:red;">', '</p>');
        $this->form_validation->set_rules($this->validation_config);        
                
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('upload_form', $this->results);
        }
        else
        {
            $this->load->view('form_success');
        }
        
    }
    function save_shop()
    {
    $this->file_upload();
    $this->validation_config = $this->shop_validation;
    $this->validate_form();
    }
}
?&gt;
#2

[eluser]TheFuzzy0ne[/eluser]
Please see the user guide section on [url="http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#savingtoconfig"]Saving Sets of Validation Rules to a Config File[/url].
#3

[eluser]Leon Stafford[/eluser]
my bad Tongue

Thanks!
#4

[eluser]Leon Stafford[/eluser]
Is there a similar method for multiple upload configs?

ie.

Code:
$this->load->library('upload', $config['user_pic']);
#5

[eluser]TheFuzzy0ne[/eluser]
Yes, you can store your configurations in a config file, load the file as needed, and grab the array you want from the config. It's all documented [url="http://ellislab.com/codeigniter/user-guide/libraries/config.html"]here[/url].
#6

[eluser]Leon Stafford[/eluser]
Thanks again!

Here's what I have if it helps anyone:

Main class:
Code:
&lt;?php
class Admin extends Controller {
    public $results = array();
    public $validation_config = array();
    public $upload_config = array();
    function Admin()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url','html'));
        $this->load->library('form_validation');
    }
    
    function index()
    {    
        
        $this->load->view('upload_form');//change to admin_top
    }

    function file_upload()
    {

        $this->load->library('upload', $this->upload_config);
        $number_of_files = 4;
        
        for ($file_counter = 0; $file_counter < $number_of_files; $file_counter +=1){
            $this->upload->initialize($this->upload_config);
            if ( ! $this->upload->do_upload('userfile'.$file_counter))
            {
                $this->results['result'.$file_counter]['error'] = array($this->upload->display_errors('<p style="color:red;">', '</p>'));
                $this->results['result'.$file_counter]['success'] = array('');
            }    
            else
            {
                $this->results['result'.$file_counter]['success'] = array($this->upload->data());
                $this->results['result'.$file_counter]['error'] = array('');
            }
        }
        
    }
        
    function validate_form()
    {
        $this->form_validation->set_error_delimiters('<p style="color:red;">', '</p>');
                
        if ($this->form_validation->run($this->validation_config) == FALSE)
        {
            $this->load->view('upload_form', $this->results);
        }
        else
        {
            $this->load->view('form_success');
        }
        
    }
    function save()
    {
    $area_to_save = $this->uri->segment(3);
    switch ($area_to_save)
    {
        case "shop":
            $this->config->load('upload_settings');
            $this->upload_config = $this->config->item('shop_upload_settings');    
            $this->file_upload();
            $this->validation_config = 'shop_validation';
            $this->validate_form();
        break;
        case "event":
            $this->config->load('upload_settings');
            $this->upload_config = $this->config->item('event_upload_settings');
            $this->file_upload();
            $this->validation_config = 'event_validation';
            $this->validate_form();
        break;
        default:
            echo "no save target specified";
        break;
    }
    }
}
?&gt;

form_validation config:
Code:
&lt;?php
$config = array(
    'shop_validation' =>array(
                       array(
                             'field'   => 'username',
                             'label'   => 'Username',
                             'rules'   => 'trim|required|prep_for_form'
                          ),
                       array(
                             'field'   => 'password',
                             'label'   => 'Password',
                             'rules'   => 'trim|required|matches[passconf]|md5'
                          ),
                       array(
                             'field'   => 'passconf',
                             'label'   => 'Password Confirmation',
                             'rules'   => 'trim|required'
                          ),  
                       array(
                             'field'   => 'email',
                             'label'   => 'Email',
                             'rules'   => 'trim|required|valid_email'
                          )
                    ),
    'event_validation' =>array(
                       array(
                             'field'   => 'username',
                             'label'   => 'Username',
                             'rules'   => 'trim|required|prep_for_form'
                          ),
                       array(
                             'field'   => 'password',
                             'label'   => 'Password',
                             'rules'   => 'trim|required|matches[passconf]|md5'
                          ),
                       array(
                             'field'   => 'passconf',
                             'label'   => 'Password Confirmation',
                             'rules'   => 'trim|required'
                          ),  
                       array(
                             'field'   => 'email',
                             'label'   => 'Email',
                             'rules'   => 'trim'
                          )
                    )                
);                    
?&gt;

upload_settings config:
Code:
&lt;?php
$config = array(
    'shop_upload_settings'=>array(
                                     'upload_path'   => './uploads/shop/',
                                     'allowed_types'   => 'gif|jpg|png',
                                     'max_size'   => '3000',
                                     'max_width'   => '1024',
                                     'max_height'   => '768'
                                  ),
    'event_upload_settings'=>array(
                                     'upload_path'   => './uploads/event/',
                                     'allowed_types'   => 'gif|jpg|png',
                                     'max_size'   => '3000',
                                     'max_width'   => '1024',
                                     'max_height'   => '768'
                                  )                              
        );                    
?&gt;
#7

[eluser]Jondolar[/eluser]
This was just what I was looking for. I think you could just pass 'event_validation' to your validate_form() function. Just an observation as I'm learning CI and walking through your great example code.




Theme © iAndrew 2016 - Forum software by © MyBB