Welcome Guest, Not a member yet? Register   Sign In
form_validation.php in config and conditional validation rules
#1

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?
Reply
#2

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...
Reply
#3

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.
Reply
#4

(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.
Reply
#5

(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.
Reply
#6

(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.
Reply
#7

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.
Reply
#8

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.
Reply
#9

(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.
Reply
#10

(This post was last modified: 02-02-2018, 11:57 PM by Elias.)

1st option: Callback documentation https://codeigniter.com/user_guide/libra...on-methods

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




Theme © iAndrew 2016 - Forum software by © MyBB