Easily add the ability to use model methods as Form Validation rules. #866

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+45 −3
Diff settings

Always

Just for now

@@ -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);

This comment has been minimized.

Show comment Hide comment
@AkenRoberts

AkenRoberts Oct 13, 2012

Contributor

Also, pretty sure there is a bug here. In $this->CI->$model->$rule($postdata, $param), I believe $modelshould be $found_model.

@AkenRoberts

AkenRoberts Oct 13, 2012

Contributor

Also, pretty sure there is a bug here. In $this->CI->$model->$rule($postdata, $param), I believe $modelshould be $found_model.

if ($_in_array === TRUE)
{