CodeIgniter Forums
form_validation.php in config and conditional validation rules - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: form_validation.php in config and conditional validation rules (/showthread.php?tid=69921)

Pages: 1 2


form_validation.php in config and conditional validation rules - webmachine - 01-30-2018

I have made a CRM in CodeIgniter that I am working on improving, and want to move all the form validation rules to a form_validation.php file in the config folder. It is working fine, but some of my rules are conditional on other form fields responses, and I don't know how to integrate those rules into the file's arrays.

Would I make a separate small array for each conditional rule, and place it as a new set of rules outside the main method validation array? Or can I make a part of the main validation array conditional?


RE: form_validation.php in config and conditional validation rules - neuron - 01-30-2018

There is no optional rule in CI.

I defined separate array of rules for each action. 
For example add and update form contents are mostly similar, but anyway I defined 2 separate array of for each of these actions.


PS: I am a junior developer. perhaps there are better methods...


RE: form_validation.php in config and conditional validation rules - webmachine - 01-30-2018

Here's a snippet of one of my sets of validation rules (currently in the body of the controller).

PHP Code:
$this->form_validation->set_rules('caseworker_contact''<span>"Contact Case Worker"</span>''required'); 

if (
$this->input->post('caseworker_contact') == 'yes') {
   $this->form_validation->set_rules('caseworker_name''<span>"Case Worker Name"</span>''required');  


It's that conditional rule that I am trying to figure out how to place in the config file.


RE: form_validation.php in config and conditional validation rules - Narf - 01-30-2018

(01-30-2018, 09:42 AM)webmachine Wrote: I have made a CRM in CodeIgniter that I am working on improving, and want to move all the form validation rules to a form_validation.php file in the config folder.

This is not an improvement. Every developer who has to look at those forms later will hate you for essentially obfuscating the validation logic for them.


RE: form_validation.php in config and conditional validation rules - webmachine - 01-31-2018

(01-30-2018, 12:13 PM)Narf Wrote:
(01-30-2018, 09:42 AM)webmachine Wrote: I have made a CRM in CodeIgniter that I am working on improving, and want to move all the form validation rules to a form_validation.php file in the config folder.

This is not an improvement. Every developer who has to look at those forms later will hate you for essentially obfuscating the validation logic for them.

I was wondering about that. But the form is very long, and I thought that removing the many validation rules to their own file would streamline the methods and make them more readable. I am quite willing to keep them the way they are, if that is the lesser of two evils.


RE: form_validation.php in config and conditional validation rules - Narf - 01-31-2018

(01-31-2018, 01:02 PM)webmachine Wrote:
(01-30-2018, 12:13 PM)Narf Wrote:
(01-30-2018, 09:42 AM)webmachine Wrote: I have made a CRM in CodeIgniter that I am working on improving, and want to move all the form validation rules to a form_validation.php file in the config folder.

This is not an improvement. Every developer who has to look at those forms later will hate you for essentially obfuscating the validation logic for them.

I was wondering about that. But the form is very long, and I thought that removing the many validation rules to their own file would streamline the methods and make them more readable. I am quite willing to keep them the way they are, if that is the lesser of two evils.

If there's any evil, this is certainly the lesser one.

Perhaps you have too much other logic mixed in there that should be abstracted away.


RE: form_validation.php in config and conditional validation rules - webmachine - 02-02-2018

It is an assessment form with numerous sections, and quite a few questions within each section. The method is set up the way Codeigniter suggests - all form validation rules and then, if they pass, on to the array of responses to be sent to the model, if not, then on to loading the view. But the form validation is made longer because some of the rules are wrapped in conditionals.


RE: form_validation.php in config and conditional validation rules - Elias - 02-02-2018

You can move simple part of validation rules definitions to config and in controller getting this config item and do array_merge with hard validations rules. Or you can hold custom validations methods in you controller and hold all validation rules in config file with definitions with 'callback_' prfefix.


RE: form_validation.php in config and conditional validation rules - webmachine - 02-02-2018

(02-02-2018, 12:26 PM)Elias Wrote: You can move simple part of validation rules definitions to config and in controller getting this config item and do array_merge with hard validations rules. Or you can hold custom validations methods in you controller and hold all validation rules in config file with definitions with 'callback_' prefix.

That sounds like something I would like to do - but I'm not familiar with how those two suggestions would be coded. Could you please include some code showing the use of array_merge here, or the use of the callback_ prefix? i can also leave a comment in the controller guiding other developers to the config file where they can find the main validation rules.


RE: form_validation.php in config and conditional validation rules - Elias - 02-02-2018

1st option: Callback documentation https://codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods

2nd option: Create config, load that and get item of that https://www.codeigniter.com/userguide3/libraries/config.html
Than, merge array with definition of rule using model's method
http://php.net/manual/en/function.array-merge.php