Permalink
Browse files

Easily add the ability to use model methods as rules.

Adds one public method to set a model name or array of models that will be checked.
Slightly modifies the _execute() method to check for rules in models before continuing the hunt.
Originally I added a simple public method to Loader() which would return the protected $_ci_models array to see if it was already loaded but thought just trying to load the model was less intrusive as it will just return the model if it is or load and it if it isn't.
  • Loading branch information...
1 parent 43fcdbc commit 196f5e10e4c354562ae02147b4331e34b4287d51 @CroNiX CroNiX committed Jan 5, 2012
Showing with 45 additions and 3 deletions.
  1. +45 −3 system/libraries/Form_validation.php
@@ -47,7 +47,8 @@ class CI_Form_validation {
protected $_error_suffix = '</p>';
protected $error_string = '';
protected $_safe_form_data = FALSE;
-
+ protected $_rules_models = array();
+
/**
* Constructor
*/
@@ -202,6 +203,25 @@ public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
return $this;
}
+ //---------------------------------------------------------------------
+
+ /**
+ * Add a model or array of models to be checked for validation rules at runtime.
+ *
+ * @param string/Array If string, it will be added to an array. If Array, it is merged.
+ */
+ public function set_rules_model($model)
+ {
+ if(is_array($model))
+ {
+ $this->_rules_models = array_merge($this->_rules_models, $model);
+ }
+ else
+ {
+ $this->_rules_models[] = (string)$model;
+ }
+ }
+
// --------------------------------------------------------------------
/**
@@ -591,7 +611,29 @@ protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
}
else
{
- if ( ! method_exists($this, $rule))
+ //Check to see if we are supposed to look in a model for the rule
+ $found_in_model = FALSE;
+ $models = $this->_rules_models;
+ $found_model = '';
+
+ if(count($models) > 0)
+ {
+ //Find the model with the rule we want
+ foreach($models as $model)
+ {
+ $this->CI->load->model($model);
+ //Check for Rule
+ $found_in_model = method_exists($this->CI->$model, $rule);
+ //If we found it set the model name and break out of the loop
+ if($found_in_model)
+ {
+ $found_model = $model;
+ break;
+ }
+ }
+ }
+
+ if ( ! method_exists($this, $rule) AND ! $found_in_model)
{
// If our own wrapper function doesn't exist we see if a native PHP function does.
// Users can use any native PHP function call that has one param.
@@ -616,7 +658,7 @@ protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
continue;
}
- $result = $this->$rule($postdata, $param);
+ $result = ($found_in_model) ? $this->CI->$model->$rule($postdata, $param) : $this->$rule($postdata, $param);
if ($_in_array === TRUE)
{

0 comments on commit 196f5e1

Please sign in to comment.