Welcome Guest, Not a member yet? Register   Sign In
Form validation in codeigniter does not seem DRY
#8

(This post was last modified: 10-25-2015, 11:37 AM by Martin7483.)

When you said validate more than one _post variable, I thought you were talking about more than one $_POST dataset. Reading your first provided link I now see what you mean.

You need two values from your $_POST dataset to validate a specific value. Creating a custom validation rule should do the trick.

I would suggest extending the validation class and adding your custom method.

Example:

PHP Code:
public function my_custom_validation($validate_this$_post_key) {
 
   $ci =& get_instance();
    
// do your validation
 
   $post_fields explode('.'$_post_key);
 
   // Set the fieldnames
 
   foreach($post_fields as $key) {
 
       $$key $this->ci->input->post($key);
 
   }


To use it do:

PHP Code:
$this->form_validation->set_rules('url''URL''trim|xss_clean|my_custom_validation[post_key]'); 

You add my_custom_validation to your rules and between the [] you place the name of the field or fields that must also be used for the validation. When using multiple fields use a . to seperate the names

You should now be able to validate more than one post value within the same validation method
Reply


Messages In This Thread
RE: Form validation in codeigniter does not seem DRY - by Martin7483 - 10-25-2015, 11:27 AM



Theme © iAndrew 2016 - Forum software by © MyBB