validation require problem(Empty Field) |
[eluser]uresh[/eluser]
I got problem, when i m putting any field EMPTY, which is not mandatory to be filled. though the requirement error message will prompt. Please Reply Thank You Uresh Patel
[eluser]pistolPete[/eluser]
Which CI version are you using? Please post your controller / validation code.
[eluser]uresh[/eluser]
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Donor_registration extends Controller { function Donor_registration() { parent::Controller(); } function index() { $this->browse(); } function showall() { $this->browse(); } function browse() { $start = $this->uri->segment(3,0); $limit_per_page = 5; $this->load->model('donor_registrationmodel'); $the_results['donor_registration_list'] = $this->donor_registrationmodel->findAll($start, $limit_per_page); $this->load->library('pagination'); $this->load->helper('url'); $config['base_url'] = site_url('donor_registration/showall/'); // or just /donor_registration/ $config['total_rows'] = $this->donor_registrationmodel->table_record_count; $config['per_page'] = $limit_per_page; $this->pagination->initialize($config); $the_results['page_links'] = $this->pagination->create_links(); $this->load->library('layout'); $this->layout->render_page('/donor_registration/donor_registrationgrid', $the_results); } function add() { $this->load->helper('url'); $this->load->library('form_validation'); $this->form_validation->set_rules('Name', 'Name', 'trim|required|min_length[1]|max_length[25]'); $this->form_validation->set_rules('BloodGroup', 'BloodGroup', 'trim|required'); $this->form_validation->set_rules('Email', 'Email', 'trim|valid_email'); $this->form_validation->set_rules('DonationDate', 'Date', 'trim|required|date'); $this->form_validation->set_rules('Mobile', 'Not Valid Number','trim|is_natural'); $this->form_validation->set_rules('Address', 'Address','trim'); $this->form_validation->set_rules('Address2', 'Address','trim'); $this->form_validation->set_rules('Age', 'Not Valid Age','trim|is_natural'); $this->form_validation->set_rules('Sex', 'Gender','trim'); $this->form_validation->set_rules('Village', 'Village','trim|alpha'); $this->form_validation->set_rules('District', 'District','trim|alpha'); $this->form_validation->set_rules('State', ' State','trim|alpha'); $this->form_validation->set_rules('Phone', 'Not Valid Phone Number','trim|is_natural'); $this->form_validation->set_rules('BirthDay', 'Date', 'trim|date'); $this->form_validation->set_rules('LastDonationDate', 'LastDonationDate', 'trim|date'); $this->form_validation->set_rules('NoofDonation', 'Only Number','trim'); if ($this->form_validation->run() == true) { $submit = $this->input->post('Submit'); if ( $submit != false ) { $this->load->model('donor_registrationmodel'); $data['InsertedDate'] = $this->input->post('InsertedDate', TRUE); $data['InsertedDate']= date("Y-m-d h:i",strtotime($data['InsertedDate'])); // XXS Filtering enforced for user input $data['RegistrationNo'] = $this->input->post('RegistrationNo', TRUE); $data['Category'] = $this->input->post('Category', TRUE); $data['DonationDate'] = $this->input->post('DonationDate', TRUE); $data['DonationDate']= date("Y-m-d h:i",strtotime($data['DonationDate'])); $data['Name'] = $this->input->post('Name', TRUE); $data['Address'] = $this->input->post('Address', TRUE); $data['Address2'] = $this->input->post('Address2', TRUE); $data['Village'] = $this->input->post('Village', TRUE); $data['District'] = $this->input->post('District', TRUE); $data['State'] = $this->input->post('State', TRUE); $data['Mobile'] = $this->input->post('Mobile', TRUE); $data['Phone'] = $this->input->post('Phone', TRUE); $data['Email'] = $this->input->post('Email', TRUE); $data['BirthDay'] = $this->input->post('BirthDay', TRUE); $data['BirthDay']= date("Y-m-d",strtotime($data['BirthDay'])); $data['Age'] = $this->input->post('Age', TRUE); $data['Sex'] = $this->input->post('Sex', TRUE); $data['BloodGroup'] = $this->input->post('BloodGroup', TRUE); $data['RH'] = $this->input->post('RH', TRUE); $data['LastDonationDate'] = $this->input->post('LastDonationDate', TRUE); $data['LastDonationDate']= date("Y-m-d",strtotime($data['LastDonationDate'])); $data['NoofDonation'] = $this->input->post('NoofDonation', TRUE); $data['EntityId'] = $this->input->post('EntityId', TRUE); // $data = $this->_get_form_values(); $this->donor_registrationmodel->add($data); // $this->load->helper('url'); redirect('/donor_registration/', 'location'); } } else { $data = $this->_clear_form(); $data['action'] = 'add'; $this->load->library('layout'); $this->layout->render_page('/donor_registration/donor_registrationdetails', $data); } } function delete() { $idField = $this->uri->segment(3); $this->load->model('donor_registrationmodel'); $the_results = $this->donor_registrationmodel->delete_by_pkey($idField); $this->load->helper('url'); redirect('/donor_registration/', 'location'); } } ?>
[eluser]uresh[/eluser]
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Donor_registration extends Controller { function Donor_registration() { parent::Controller(); } function index() { $this->browse(); } function showall() { $this->browse(); } function add() { $this->load->helper('url'); $this->load->library('form_validation'); $this->form_validation->set_rules('Name', 'Name', 'trim|required|min_length[1]|max_length[25]'); $this->form_validation->set_rules('BloodGroup', 'BloodGroup', 'trim|required'); $this->form_validation->set_rules('Email', 'Email', 'trim|valid_email'); $this->form_validation->set_rules('DonationDate', 'Date', 'trim|required|date'); $this->form_validation->set_rules('Mobile', 'Not Valid Number','trim|is_natural'); $this->form_validation->set_rules('Address', 'Address','trim'); $this->form_validation->set_rules('Address2', 'Address','trim'); $this->form_validation->set_rules('Age', 'Not Valid Age','trim|is_natural'); $this->form_validation->set_rules('Sex', 'Gender','trim'); $this->form_validation->set_rules('Village', 'Village','trim|alpha'); $this->form_validation->set_rules('District', 'District','trim|alpha'); $this->form_validation->set_rules('State', ' State','trim|alpha'); $this->form_validation->set_rules('Phone', 'Not Valid Phone Number','trim|is_natural'); $this->form_validation->set_rules('BirthDay', 'Date', 'trim|date'); $this->form_validation->set_rules('LastDonationDate', 'LastDonationDate', 'trim|date'); $this->form_validation->set_rules('NoofDonation', 'Only Number','trim'); if ($this->form_validation->run() == true) { $submit = $this->input->post('Submit'); if ( $submit != false ) { $this->load->model('donor_registrationmodel'); $data['InsertedDate'] = $this->input->post('InsertedDate', TRUE); $data['InsertedDate']= date("Y-m-d h:i",strtotime($data['InsertedDate'])); // XXS Filtering enforced for user input $data['RegistrationNo'] = $this->input->post('RegistrationNo', TRUE); $data['Category'] = $this->input->post('Category', TRUE); $data['DonationDate'] = $this->input->post('DonationDate', TRUE); $data['DonationDate']= date("Y-m-d h:i",strtotime($data['DonationDate'])); $data['Name'] = $this->input->post('Name', TRUE); $data['Address'] = $this->input->post('Address', TRUE); $data['Address2'] = $this->input->post('Address2', TRUE); $data['Village'] = $this->input->post('Village', TRUE); $data['District'] = $this->input->post('District', TRUE); $data['State'] = $this->input->post('State', TRUE); $data['Mobile'] = $this->input->post('Mobile', TRUE); $data['Phone'] = $this->input->post('Phone', TRUE); $data['Email'] = $this->input->post('Email', TRUE); $data['BirthDay'] = $this->input->post('BirthDay', TRUE); $data['BirthDay']= date("Y-m-d",strtotime($data['BirthDay'])); $data['Age'] = $this->input->post('Age', TRUE); $data['Sex'] = $this->input->post('Sex', TRUE); $data['BloodGroup'] = $this->input->post('BloodGroup', TRUE); $data['RH'] = $this->input->post('RH', TRUE); $data['LastDonationDate'] = $this->input->post('LastDonationDate', TRUE); $data['LastDonationDate']= date("Y-m-d",strtotime($data['LastDonationDate'])); $data['NoofDonation'] = $this->input->post('NoofDonation', TRUE); $data['EntityId'] = $this->input->post('EntityId', TRUE); // $data = $this->_get_form_values(); $this->donor_registrationmodel->add($data); // $this->load->helper('url'); redirect('/donor_registration/', 'location'); } } else { $data = $this->_clear_form(); $data['action'] = 'add'; $this->load->library('layout'); $this->layout->render_page('/donor_registration/donor_registrationdetails', $data); } } function delete() { $idField = $this->uri->segment(3); $this->load->model('donor_registrationmodel'); $the_results = $this->donor_registrationmodel->delete_by_pkey($idField); $this->load->helper('url'); redirect('/donor_registration/', 'location'); } } ?>
[eluser]uresh[/eluser]
************MODEL---------------- <? class Donor_registrationModel extends Model { var $table_record_count; var $RegistrationNo; var $Category; var $DonationDate; var $Time; var $Name; var $Address; var $Address2; var $Village; var $District; var $State; var $Mobile; var $Phone; var $Email; var $BirthDay; var $Age; var $Sex; var $BloodGroup; var $RH; var $LastDonationDate; var $NoofDonation; var $EntityId; var $InsertedDate; function Donor_registrationModel() { parent::Model(); $this->obj =& get_instance(); $this->_init_Donor_registration(); } function retrieve_by_pkey($idField) { $results = array(); // Load the db library $this->load->database(); $query = $this->db->query("SELECT * FROM donor_registration WHERE RegistrationNo = '$idField' LIMIT 1"); if ($query->num_rows() > 0) { $row = $query->row_array(); $query_results['RegistrationNo'] = $row['RegistrationNo']; $query_results['Category'] = $row['Category']; $query_results['DonationDate'] = $row['DonationDate']; $query_results['Time'] = $row['Time']; $query_results['Name'] = $row['Name']; $query_results['Address'] = $row['Address']; $query_results['Address2'] = $row['Address2']; $query_results['Village'] = $row['Village']; $query_results['District'] = $row['District']; $query_results['State'] = $row['State']; $query_results['Mobile'] = $row['Mobile']; $query_results['Phone'] = $row['Phone']; $query_results['Email'] = $row['Email']; $query_results['BirthDay'] = $row['BirthDay']; $query_results['Age'] = $row['Age']; $query_results['Sex'] = $row['Sex']; $query_results['BloodGroup'] = $row['BloodGroup']; $query_results['RH'] = $row['RH']; $query_results['LastDonationDate'] = $row['LastDonationDate']; $query_results['NoofDonation'] = $row['NoofDonation']; $query_results['EntityId'] = $row['EntityId']; $query_results['InsertedDate'] = $row['InsertedDate']; $results = $query_results; } else { $results = false; } return $results; } function add( $data ) { $this->load->database(); $sql = $this->db->insert_string('donor_registration', $data); $query = $this->db->query($sql); return $this->db->insert_id(); } function modify($keyvalue, $data) { $this->load->database(); $where = "RegistrationNo = $keyvalue"; $sql = $this->db->update_string('donor_registration', $data, $where); $query = $this->db->query($sql); } function delete_by_pkey($idField) { $this->load->database(); $query = $this->db->query("DELETE FROM donor_registration WHERE RegistrationNo = '$idField' "); return true; }
[eluser]pistolPete[/eluser]
Which fields exactly provoke the error? Btw: In your model you could easily save some lines: Code: function retrieve_by_pkey($idField) I would also either autoload the database library or put the loading into the model constructor! There is no need to use the following line: Code: $this->obj =& get_instance(); Please use code tags in future!
[eluser]uresh[/eluser]
I have not problem with model. I have problem when the empty fields are which are not mandatory , though the validation code shows me error.
[eluser]uresh[/eluser]
Code: $this->form_validation->set_rules(‘Name’,‘Name’,trim|required|min_length[1]|max_length[25]’); /******************************************/ this fields are must me validate but not compulsory... please give your email-id. and your good name ..' my id :: [email protected] /******************************************/ |
Welcome Guest, Not a member yet? Register Sign In |