Question

I’m very new to CodeIgniter, and I have a question about prepping/purifying/manipulating the input fields. When I say “prepping/purifying/manipulating” fields, I mean:

  1. if an integer field is not entered by the user (leave blank), if user clicks on “submit” it should be set to NULL. ie. $this->input->post(‘integer_field’) should be NULL instead of ‘’ (empty string), so that when saving data to the database, it won’t cause any MySQL error

  2. for example http://ellislab.com/forums/member/edit_userpass is a form which allows you to set the screen name, change the password (only if you enter the new password and confirm new password). When submitting this form without changing the password, both $this->input->post(‘password’) and $this->input->post(‘password_confirm’) will be ‘’ (empty string). So obviously when using the model to save to the database, these two fields shouldn’t be inserted into database and they both need to unset.

So, I’m trying to find out the best place to perform the above tasks, and I think config/form_validation.php will be the best place to do so (form_validation can both validate fields and prepping fields. Also instead of calling form_validation->run in controller I would like to put all of my validations in one place, thus config/form_validation.php), however I have a few concerns:

  • based on my understanding on system/library/Form_validation.php, rules won’t be called on any field if it’s blank and it’s not a “required” field. So how am I perform #1, if the integer field is not required?

  • for #2, I think I need to use the “callback” in order to unset $this->input->post(‘password’) and $this->input->post(‘password_confirm’), however, will it screw up any of the code in Form_validation.php

Also if you think config/form_validation.php is not an appropriate file, which file should be used to prepping/purifying/manipulating fields?

Était-ce utile?

La solution

 $integer_field = $this->input->post(‘integer_field’, TRUE) ; 

 if ( $integer_field == '') { $integer_field = NULL ;  } 

configs for validation rules are great but -- get your forms working first, then refactor when you know its working. you can do form validation in models which is my preference. i would also suggest having a separate method which builds the specific array for the insert or update. then you can have very simple insert and update methods that take any array.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top