[eluser]aryan_[/eluser]
Here's my signup code
Code: function signup(){
$this->load->library('form_validation');
$data['page'] = 'account/signup';
$data['nav'] = 0;
$data['error_msg'] = '';
//set form validation
$this->form_validation->set_rules('fname','First Name', 'required|xss|trim');
$this->form_validation->set_rules('lname','Last Name', 'xss|trim');
$this->form_validation->set_rules('email','Email', 'required|xss|trimcallback_validate_email|valid_email|');
$this->form_validation->set_rules('rdisease','Rare Disease', 'required|xss|trim');
$this->form_validation->set_rules('state','State', 'required|xss|trim');
$this->form_validation->set_rules('country','Country', 'required|xss|trim');
$this->form_validation->set_rules('zip','Zip', 'required|xss|trim');
$this->form_validation->set_rules('age','Age', 'required|xss|trim|numeric');
$this->form_validation->set_rules('uname','User Name', 'required|xss|trim|callback_validate_un|alpha_dash');
$this->form_validation->set_rules('passw','Password', 'required|xss|trim|min_length[6]');
$this->form_validation->set_rules('opttc','Terms & Condition', 'required');
if($this->uri->segment(3) != 0){
$this->form_validation->set_rules('ukey','Key', 'required|trim|xss|validate_key');
}
$this->form_validation->set_error_delimiters('<li>', '</li>');
if($this->form_validation->run() == TRUE){
$val = array();
//get(if user exists) or generate unique key
if($this->uri->segment(3) != 0){
$uid = $this->input->post('ukey');
}
else{
$uid = uniqid();
$val['key'] = $uid;
}
//collect all table fields value
$val['fname'] = $this->input->post('fname');
$val['lname'] = $this->input->post('lname');
$val['email'] = $this->input->post('email');
$val['diseasetype'] = $this->input->post('rdisease');
$val['city'] = $this->input->post('city');
$val['state'] = $this->input->post('state');
$val['country'] = $this->input->post('country');
$val['zip'] = $this->input->post('zip');
$val['age'] = $this->input->post('age');
$val['username'] = $this->input->post('uname');
$val['password'] = md5($this->input->post('passw'));
$val['optkelf'] = $this->input->post('optkelf');
$val['optkelfp'] = $this->input->post('optkelfp');
$val['datesignup'] = time();
//upload artists image
if($_FILES['ufile']['name'][0] != ''){
$config['upload_path'] = realpath('uploads/' . $uid);
$config['allowed_types'] = 'jpg|png|gif';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
if(!$this->upload->do_upload('ufile')){
echo('error');
$data['error_msg'] = $this->upload->display_errors('<li>', '</li>');
}
$filedata = $this->upload->data();
$val['image'] = $filedata['file_name'];
}
//insert or update db
if($this->uri->segment(3) != 0){
$this->db->where('key', $uid);
$this->db->update('artists', $val);
}
else{
$this->db->insert('artists', $val);
}
}
$this->load->view('tpl', $data);
}
I have defined callback functions "validate_email", "validate_key" and "validate_un" in the same controller.
When I submit form data is inserted/updated event if there is an error. Am I missing something?
[eluser]LifeSteala[/eluser]
Hi there,
From what I can see you have the following errors:
Code: // Error line
$this->form_validation->set_rules('email','Email', 'required|xss|trimcallback_validate_email|valid_email|');
// Fix line to
$this->form_validation->set_rules('email','Email', 'required|xss|trim|callback_validate_email|valid_email');
// also valid_email may not be required if you have a callback doing the same thing?
// Error line
$this->form_validation->set_rules('ukey','Key', 'required|trim|xss|validate_key');
// Fix line to
$this->form_validation->set_rules('ukey','Key', 'required|trim|xss|callback_validate_key');
Then somewhere in your view, add this line of code to show any validation errors.
Code: echo validation_errors();
I hope that helps.
[eluser]aryan_[/eluser]
I fixed errors, but whenever I submit the form, it give me error- username already exist.
Here's my custom validation function:
Code: function validate_un($un){
if($this->accountmodel->check_field('artists', 'username', $un)){
$this->form_validation->set_message('validate_un', 'The %s already exists. Please choose a different one.');
return FALSE;
}
else{
return TRUE;
}
}
function validate_email($email){
if($this->accountmodel->check_field('artists', 'email', $email)){
$this->form_validation->set_message('validate_email', 'The %s already exists. Please choose a different one.');
return FALSE;
}
else{
return TRUE;
}
}
function validate_key($ukey){
if($this->accountmodel->check_field('artists', 'key', $ukey)){
return TRUE;
}
else{
$this->form_validation->set_message('validate_key', 'The %s does not exists.');
return FALSE;
}
}
And here's model:
Code: function check_field($table, $field, $value){
$sql = "SELECT `{$field}` FROM `{$table}` WHERE `{$field}` = '{$value}' LIMIT 1";
//die($sql);
$query = $this->db->query($sql);
if($query->num_rows() > 0){
return TRUE;
}
else{
return FALSE;
}
}
[eluser]LifeSteala[/eluser]
You are possibly getting that error because maybe that user name exists in your database? Check that your query is working just fine.
Tip: those backticks (``) are not required.
[eluser]aryan_[/eluser]
Query is working fine. Validation is also working fine, but it inserts data even on form/validation error. Then it gives error that username/email already exists.
Seems strange!
[eluser]aryan_[/eluser]
It seems return false from custom validation doesn't stop further processing?
[eluser]LifeSteala[/eluser]
Can you trying taking out LIMIT 1 from your query?
In your model, check_field(), after the $query = $this->db->query($sql) add these lines of code:
Code: $result = $query->result();
print_r($result);
Please post us what you get on screen from print_r.
Thanks
|