Welcome Guest, Not a member yet? Register   Sign In
Form validation config & dynamic
#1

(This post was last modified: 03-17-2015, 12:03 PM by BABYpanda. Edit Reason: added )

Hello,

I am trying to achieve something with the Form Validation in CI 2.2.

The code I am using is for the validation is:
PHP Code:
$this->load->library('form_validation');

$this->setConfigValidations($page); // $page is tickets in this case
            
if(!$this->form_validation->run('form_tickets')) {
    
// Probably going to add log_message() here.
    
return;


Then the config file 'form_validation.php':
PHP Code:
<?php  if(!defined('BASEPATH')) exit('No direct script access allowed');
$config = array(
    
'form_tickets' => array(
        array(
            
'field' => 'submitData',
            
'label' => 'selectTickets',
            
'rules' => 'trim|required|max_amount|min_amount'
        
)
    );
); 

And here a custom function which is also been called in the first code snippet:
PHP Code:
private function setConfigValidations($page) {
    switch(
$page) {
        case 
'tickets':
            
$tickets $this->config->item('tb_tickets');
            
            foreach(
array_keys($tickets) as $ticket) {
                
$this->form_validation->set_rules($this->config->item('tb_tickets_name_form_prefix').$ticket'lang:'.$tickets[$ticket]['name'], 'trim|required|is_ticket|minimum|maximum');
            }
            
            break;
        default:
            break;
    }


As you can see, I am trying to get one bit loaded using a config, and some fields must be dynamically added since they are retrieved in the config.

But somehow, I am unable to achieve this. Is it somehow possible to load certain validations in a config and others dynamically?

Thanks in advance.

Kindest regards,
Reno
Reply
#2

Is there a specific reason why you want to use the config file? Since you only have one validation rule in the config file, why not move it into the setConfigValidations() function?

Adding it before, or after, the switch would set that rule each time, and you can build the rest dynamically as you already doing.

Example
PHP Code:
private function setConfigValidations($page) {

   $this->form_validation->set_rules('submitData''selectTickets''trim|required|max_amount|min_amount');

   switch($page) {
       case 'tickets':
           $tickets $this->config->item('tb_tickets');
           
           foreach
(array_keys($tickets) as $ticket) {
                   $this->form_validation->set_rules($this->config->item('tb_tickets_name_form_prefix').$ticket'lang:'.$tickets[$ticket]['name'], 'trim|required|is_ticket|minimum|maximum');
           }
           
           break
;
       default:
           break;
   }

Reply
#3

Alternatively, you could load the config file, retrieve the rules, and set them manually before setting your additional rules, then leave the group name out of the call to form_validation->run().

PHP Code:
case 'tickets':
 
   // Load the form_validation config. Passing true for the second argument loads
 
   // the values into the 'form_validation' config item.
 
   $this->config->load('form_validation'true);

 
   $this->form_validation->set_rules($this->config->item('form_tickets''form_validation'));

 
   $tickets $this->config->item('tb_tickets');
 
   // ... 
Reply
#4

Thanks both for your replies!

@silentium: The reason I'd like to do this is because I would like to create a clear structure where everything is set. Thus I thought it'd be better to do it both in config as dynamic. I've got more rules in it, and this is the only part where there's only 1 rule in the holder. The next forms include 5+ validation rules.

@mwhitney: I think this is the best way of taking care of it in my situation, thanks! I'm right about to try it out.
Reply
#5

(This post was last modified: 03-18-2015, 12:10 PM by silentium.)

(03-18-2015, 11:52 AM)BABYpanda Wrote: @silentium: The reason I'd like to do this is because I would like to create a clear structure where everything is set. Thus I thought it'd be better to do it both in config as dynamic. I've got more rules in it, and this is the only part where there's only 1 rule in the holder. The next forms include 5+ validation rules.

I see, then I understand why and agree that @mwhitney's solution would be the best.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB