Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
wants to merge 1 commit into from

6 participants

@CroNiX

-Adds one public method to set a model name or array of models that will be checked for rules.
-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.

I will gladly amend the related docs if this change is accepted.

@CroNiX CroNiX 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.
196f5e1
@RS71

I support this request - would be very useful and help centralize validation.

@cryode

I certainly agree with adding the ability to use model methods as validation rules (actually I would prefer even more expandability than that, but I digress). Just not sure this is the best method for doing it. There's potential for conflicts; if your model has a min_length method, it will be called instead of the standard rule, because in your suggested code, models are given "priority" (they are checked before looking for Form_validation). This could be bad if you intended on using a different model method rule and intended for min_length to act normally. I'm also not sure if adding models to the entire rule process is best - maybe adding them individually by field would be more ideal.

Maybe collectively we can improve upon this or figure out a better solution.

@cryode cryode commented on the diff
system/libraries/Form_validation.php
@@ -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);
@cryode
cryode added a note

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dchill42

I would like to see this implemented with a little more flexibility. I have added the ability to route callback functions to controllers in my HMVC implementation, where you can specify any Controller and it will be loaded if necessary and the function called. I imagine a similar mechanism for callbacks on models. What if Form_validation accepted a new prefix like 'callmodel_' which could be followed by a model name and function? (e.g. - 'callmodel_mymodel/newrule') I think this would be much more widely usable, and not limit the rules to a single model.

@narfbg
Owner

#312 is a better approach to the problem.

@narfbg narfbg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 5, 2012
  1. @CroNiX

    Easily add the ability to use model methods as rules.

    CroNiX authored
    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.
This page is out of date. Refresh to see the latest.
Showing with 45 additions and 3 deletions.
  1. +45 −3 system/libraries/Form_validation.php
View
48 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);
@cryode
cryode added a note

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
if ($_in_array === TRUE)
{
Something went wrong with that request. Please try again.