Welcome Guest, Not a member yet? Register   Sign In
Static + Dynamic Form Validation rules
#1

[eluser]theshiftexchange[/eluser]
I currently have all my form_validation in a config file. I have groups of rules that I successful use to control my form validation.

I have a requirement where I need a particular function to use one of those groups of rules AND an additional rule, which is dynamically created (it wont always need this rule, just depends on a few things).

Is this possible?

For example:

Code:
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_valudation->run('create_ad')

This wont take into account the first 'username' rule, the 'create_ad' overrides it.
#2

[eluser]theshiftexchange[/eluser]
quick bump
#3

[eluser]jekorma[/eluser]
Hello.

Was struggling with the same issue. CI is not very flexible with setting validation rules as it is either/or when it comes to config vs set_rules, as set_rules overrides config.

However, when using the config file to set rules you can access and modify it in your controller like so:

Code:
$this->form_validation->_config_rules // your main array of rules

// I had a rule set called 'eventedit' where I wanted to remove
// rules for an input field called sport:

unset($this->form_validation->_config_rules['eventedit']['sport']);

// normally you would have a rule set in your config that looks like this:

$config = array(

'rule_set_name' => array(
array(
'field' => 'bla',
'label' => 'foo',
'rules' => 'your rules here'
),
array(
'field' => 'name',
'label' => 'something',
'rules' => 'more rules here'
)

)// end: rule_set_name
)// end: $config


// but in order to manipulate specific sub arrays inside 'rule_set_name' it
// seems best to give names to the sub arrays you want to modify:


$config = array(

'rule_set_name' => array(
array(
'field' => 'bla',
'label' => 'foo',
'rules' => 'your rules here'
),
'named_array' => array( // like so... I named mine sport in the unset example above
'field' => 'name',
'label' => 'something',
'rules' => 'more rules here'
)

)// end: rule_set_name
)// end: $config

Of course in your case you want to add rules, but I assume you will now know how to do that :-)

Regards,

Jesper.
#4

[eluser]theshiftexchange[/eluser]
cool - thanks - i'll have a play around with that!
#5

[eluser]Unknown[/eluser]
@jekorma: i can't make your idea work and not really sure if im doing it right. basically i have something in my config file (of set_rules)...

Code:
$config = array(

  'properties' => array(
    array(
     'field'   => 'fld_title',
     'label'   => 'Property',
     'rules'   => 'trim|required'
    ),
    array(
     'field'   => 'fld_description',
     'label'   => 'Description',
     'rules'   => 'required'
    ),
    array(
     'field'   => 'fld_images',
     'label'   => 'Photos',
     'rules'   => 'required'
    ),
    ...

and in my controller (following your suggestion) within a function, i did something like...

Code:
$this->form_validation->_config_rules;
unset($this->form_validation->_config_rules['properties']['fld_images']);

its the "fld_images" that i wanted to disable for specific condition, but this is not working... can you please help?

thanks much...
#6

[eluser]Unknown[/eluser]
Works like a charm

in your form validation config file

$billing_sender = array_merge($sender, array(
array('field' => 'cc_type', 'label' => 'CC Type', 'rules' => 'trim|required'),
'newsletter' => array('field' => 'newsletter', 'label' => 'Newsletter', 'rules' => 'trim')));


(make sure to name your sub arrays)

in your controller

$this->form_validation->_config_rules['billing_ca']['newsletter']['rules'] = 'trim|required';




Theme © iAndrew 2016 - Forum software by © MyBB