Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from
Closed

Conversation

CroNiX
Copy link
Contributor

@CroNiX CroNiX commented Jan 5, 2012

-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.

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.
@Dentxinho
Copy link
Contributor

👍

@RS71
Copy link
Contributor

RS71 commented Feb 4, 2012

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

@AkenRoberts
Copy link
Contributor

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.

@@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@dchill42
Copy link
Contributor

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
Copy link
Contributor

narfbg commented Jan 6, 2014

#312 is a better approach to the problem.

@narfbg narfbg closed this Jan 6, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants