forms - Codeigniter ignore empty array items on update database record -
hey guys i've been working on few days can't seem work :(
here situation.
i have "edit profile" page. on page can, name implies, edit profile information.
for example here i'm trying do.
database record:
||-------||----------||-----------|| || name || surname || email || ||-------||----------||-----------|| || amy || nuts || an@no.com || ||-------||----------||-----------||
i have form gives user ability enter own data , change of fields.
the form fields have names corrospond database fields.
<input name="name" ...... <input name="surname" ...... <input name="email" .....
this logical , easy.
the problem have when submit form. build website html5 , use placeholders form fields. because looks nice imo , can experiment it. when want update surname, other fields stay empty. codeigniter default returns "false" in case of empty post item.
maybe can see problem.
when send post data model , updates database record, remove data "name" , "email" fields , update surname.
the array looks this:
array( "name" => "surname" => new "email" => )
after running through update function of codeigniter database record looks this
||-------||----------||-----------|| || name || surname || email || ||-------||----------||-----------|| || || new || || ||-------||----------||-----------||
what want know is, if there posibility let codeigniter ignore array items not have data.
i hope guys can out. i'm @ loss here.
use array_filter($data) remove empty fields before sending database.
also looks dont validate data update ?
can please show code in model store updates ?
example
$data['name']=$this->input->post('name'); $data['lastname']=$this->input->post('lastname'); $data=array_filter($data); //will remove empty keys or if($this->input->post('name')) $data['name']=$this->input->post('name'); if($this->input->post('lastname')) $data['name']=$this->input->post('lastname');
in cases should validate data before submitting database;
just general note:
i recommend have 2 layers of validations;
- at controller validate form (using ci form_validation)
- at model validate data (also using ci form_validation)
never trust controller alone. best dry method use in ci in model use variable
models/demo_model.php
public $validate =array( ['field' => 'login','label' => 'lang:user_login','rules' => 'trim|required|min_length[5]|max_length[100]|xss_clean'], ['field' => 'password','label' => 'lang:user_password','rules' => 'trim|required|min_length[5]|max_length[35]|xss_clean']); function is_valid($data,$mode=null){ if(!$this->validate || empty($data) )return false; //nothing validate ! $config=array(); //if updating normal variables missing //so lets apply rules data going update; make sure xss_clean, not exceed database size, trim, etc;; if($mode = 'update'){ foreach($this->validate $rule)if( ! empty( $data[ $rule['field'] ] )$config[]=$rule }else{ $config = $this->validate } $this->form_validation->reset_validation(); $this->form_validation->set_data($data); #we not using $_post; $this->form_validation->set_rules($config); return ($this->form_validation->run() === true) }
now in every database call before use $this->db->insert; or update; u use
$this->is_valid($data,'update'); // dont set 2nd param if inserting , want rules apply;
and validate requests;
also note codeigniter form_validation can used modifing submitted values also; example using trim rule, or md5 rule on password field
well sorry might hv extended answer long, hope answered question.
Comments
Post a Comment