Welcome Guest, Not a member yet? Register   Sign In
Form validation in codeigniter does not seem DRY
#1
Tongue 
(This post was last modified: 10-24-2015, 01:47 AM by ignitedcms.)

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?
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#2

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

(This post was last modified: 10-24-2015, 10:11 AM by arma7x.)

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/tu...items.html
Keep calm.
Reply
#4

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.
Reply
#5

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.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#6

(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?
Reply
#7

(This post was last modified: 10-25-2015, 01:05 AM by ignitedcms.)

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/48226...o-callback



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

http://stackoverflow.com/questions/14382...d-on-value

Would love to hear if you have any better ideas.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#8

(This post was last modified: 10-25-2015, 11:37 AM by Martin7483.)

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
Reply
#9

Why not use this method http://www.codeigniter.com/user_guide/li...on-methods
Keep calm.
Reply
#10

(10-25-2015, 02:02 PM)arma7x Wrote: Why not use this method http://www.codeigniter.com/user_guide/li...on-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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB