CodeIgniter Forums
Form validation in codeigniter does not seem DRY - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Form validation in codeigniter does not seem DRY (/showthread.php?tid=63379)

Pages: 1 2


Form validation in codeigniter does not seem DRY - ignitedcms - 10-24-2015

Form Validation in codeigniter does not seem very DRY (Don't repeat yourself)

Hi guys,

This is mainly a gripe I have with form validation in codeigniter, so I was wondering if I was doing something wrong or if there is a better way. At the moment I just find the whole form validation proceedure very repetitive and not very DRY.

For example,

I have a controller which does basic CRUD procedures.

So I have a view_add_entry() form, then I have a save_form()

I was told on form validation you can't simply use redirect to view_add_entry() and pass in the error messages, otherwise set_value() doesn't work when repopulating the view.

So you first have to first query your database if needed, store these in a $data array and then load all your view files again, which might have a header view, body view and footer view. So you would have to repeat these calls in both the form_is_valid method and form_is_not_valid method.

Then you have to create an edit_view() which collects the past data, and and update_form() which does a database update instead of insert. All the while increasing the number of lines and potential for bugs.

That just seems like a needless repeat of writing lines of code aggravating any bugs that might be introduced.

Then there's my other issue in that form_validation works on only one post field, you can NOT validate two post fields easily which might be linked without writing your own custom function. Because this isn't an actual validation_rule, I have to set this as flash data to render in the view!

I've given up on javascript/ajax validation as that just added another unnecessary variable into the equation!!

Basically it just seems so tedious and unnecessarily complicated? Am I missing something?


RE: Form validation in codeigniter does not seem DRY - Martin7483 - 10-24-2015

Do you post your forms to the same URL that loads your forms?


RE: Form validation in codeigniter does not seem DRY - arma7x - 10-24-2015

Why you need 2 controller for insert data. Just put logic operation in controller, etc. if the validation run===false then load view of form & populate error message, else insert data & redirect to view new data. Example in documentation http://www.codeigniter.com/user_guide/tutorial/create_news_items.html


RE: Form validation in codeigniter does not seem DRY - PaulD - 10-24-2015

I tried doing a page recently without using it and it was a real reminder of what a pain these things used to be.

I use the same view and controller for insert and edit. Just need a tiny bit of logic to see if we are inserting or updating data on successful submission. So for me it is entirely DRY.

And you can write error messages for custom callback failure without the need for flash data at all.

So I am sorry but I do not agree, I think it is DRY.

Best wishes,

Paul.


RE: Form validation in codeigniter does not seem DRY - ignitedcms - 10-24-2015

There's some good point brought up here guys! I never thought of creating conditional logic for both insert and update. Although I'm not quite sure it would work in the view files as it is quite complicated and separating does lend itself for easier amends.

@paul yes you can custom call backs but this is only for one _post variable, you can't write a custom call back for two posts or more (where one post is very much dependant on the other) as brought up by narf.

Therefore the only way to circumvent this is to do your own validation and use flashdata to flash a message when you reload the view again for this particular case.


RE: Form validation in codeigniter does not seem DRY - Martin7483 - 10-24-2015

(10-24-2015, 03:53 PM)iamthwee Wrote: There's some good point brought up here guys! I never thought of creating conditional logic for both insert and update. Although I'm not quite sure it would work in the view files as it is quite complicated and separating does lend itself for easier amends.

@paul yes you can custom call backs but this is only for one _post variable, you can't write a custom call back for two posts or more (where one post is very much dependant on the other) as brought up by narf.

Therefore the only way to circumvent this is to do your own validation and use flashdata to flash a message when you reload the view again for this particular case.

You don't place the conditional logic in the view. Your view is the form, and this form is the same for inserting, updating and correcting on failed validation.

In which scenario would you need to process more than one _post variable?


RE: Form validation in codeigniter does not seem DRY - ignitedcms - 10-25-2015

No what I meant is have have two views, one specifically for inserting and one for updating, but then call the same controller which has conditional logic to do either the insert or update.

For processing more than one post variable, there are many examples, I am using the following as some sort of work around. Not sure if it is best practice.

http://stackoverflow.com/questions/4822692/codeigniter-passing-2-arguments-to-callback



And I am using something like this for conditional logic in the view.

http://stackoverflow.com/questions/14382448/codeigniter-form-validation-optional-based-on-value

Would love to hear if you have any better ideas.


RE: Form validation in codeigniter does not seem DRY - Martin7483 - 10-25-2015

When you said validate more than one _post variable, I thought you were talking about more than one $_POST dataset. Reading your first provided link I now see what you mean.

You need two values from your $_POST dataset to validate a specific value. Creating a custom validation rule should do the trick.

I would suggest extending the validation class and adding your custom method.

Example:

PHP Code:
public function my_custom_validation($validate_this$_post_key) {
 
   $ci =& get_instance();
    
// do your validation
 
   $post_fields explode('.'$_post_key);
 
   // Set the fieldnames
 
   foreach($post_fields as $key) {
 
       $$key $this->ci->input->post($key);
 
   }


To use it do:

PHP Code:
$this->form_validation->set_rules('url''URL''trim|xss_clean|my_custom_validation[post_key]'); 

You add my_custom_validation to your rules and between the [] you place the name of the field or fields that must also be used for the validation. When using multiple fields use a . to seperate the names

You should now be able to validate more than one post value within the same validation method


RE: Form validation in codeigniter does not seem DRY - arma7x - 10-25-2015

Why not use this method http://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods


RE: Form validation in codeigniter does not seem DRY - PaulD - 10-25-2015

(10-25-2015, 02:02 PM)arma7x Wrote: Why not use this method http://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods

The example there is working on the single post field 'username'. But the problem was of a validation that depends on two fields, like in the example given, say you want to validate that the username is contained in the email address. (Just as an example based on the given info). You would, ideally, pass both fields through to the callback, do the check, and return true or false.

However, as I suggested earlier, the callback function done on username need only reference the post field for username, do the checks and return as normal.

Best wishes,

Paul