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
added add_rule_set method to the form_validation library #312
Conversation
I think it's a pretty good idea. Might need a little work, though. Could you explain a situation where it would be easier to do this instead of adding new rules directly by extending the Form_validations library in your application. EDIT: I can see where something like this would be useful when you want to use an existing function/method of a model or library so that you don't have repeat your self. Then again, those methods would have to be tailored to be validation callbacks anyways (i.e. have to get an instance of CI to load form_validation library so that you can call Not sure this is practical, in my opinion, without re-thinking it a bit. |
I have usage sample in an application, this application is pyrocms...following sample In pyrocms we uses a plugin to proccess public contact form (validation, send e-mail), and i've need extend some fields (an their validations). But, how this plugin works? <?php
Class Plugin_Contact extends Plugin {
// ...
$rules = array(array(
'field' => 'contact_name',
'label' => 'lang:contact_name_label',
'rules' => 'required|trim|max_length[80]'
));
// ...
function form()
{
$this->load->library('form_validation');
$this->form_validation->set_rules($this->rules);
if ($this->form_validation->run())
{
// ...
}
}
} Note: Plugin is a library that have a magic method __get that points get_instance(), as CI_Model (as in usage document from this commit). full code Ok.. now that we had a explanation of the scenary.. and the work is going fine.. i need do a extension of this source, as i said above add more fields and custom validation rules.. surely i'll need add a callback rule to my form validation.. (see bellow) which in turn will not work.. because Form Library expects that i'll declares my callback function on a instance of CI_Controller (when check method_exists($this->ci, $rule)) that is not my case. <?php
$rules = array(
array(
'field' => 'contact_name',
'label' => 'lang:contact_name_label',
'rules' => 'required|trim|max_length[80]'
),
array(
'field' => 'zipcode',
'label' => 'lang:zipcode_label',
'rules' => 'required|trim|callback__check_zipcode' // opss.. only native rules here!!
)
); It was a real case.. but i think that my comment on the new function commited in this pull explains better the usage possiblities of this mod. So.. the solution was: <?php
Class Plugin_Contact extends Plugin {
// ...
function form()
{
$this->load->library('form_validation');
$this->form_validation->add_rule_set($this); // Yuuup!! o//
$this->form_validation->set_rules($this->rules);
if ($this->form_validation->run())
{
// ...
}
}
function _check_zipcode($foo)
{
// ...
}
} |
Ps.: Independent of be necessity of an application, any developer must have the right to say to Form_validation where are their own validation functions (methods), as he wants, and he can do with the statement of the rules by using external config file, you can also take away the functions of its controllers .. leaving the controls just do what they should do, control your application. Ps.: Going to another better think, reuse of callbacks! |
…feature/callback_rules Conflicts: user_guide/changelog.html
fixed conflicts, so ... some feedback? waiting replies... |
Very interessting feature, i'm looking for a way to make callback function to check if a checbox value is TRUE. have you made it work for Pyrocms, i get errors from MY_Form_validation ? |
@khalil-tabbal Can you send me a private message and i'll help you.. ;] |
Get error and messages as array
The naming here is what concerns me the most ... and that's probably the reason it never got reviewed, not just by me. "add_rule_set" doesn't really describe what it does, otherwise it's not such a bad idea. Anyway, being 2 years old this PR would have to be completely rewritten in order to be included anyway. Please resubmit it based on the latest code, if you are still interested. |
Before accept or reject..
Please, give me a feedback if you can understand or not how it works or what should be improved or changed around this feature that i consider required but i'm not sure if i named and/or design correctly. thanks your attention.