⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Validation rule when update?



jvels
  • profile picture
  • Member

Posted 13 October 2012 - 20:43 PM

Hi

I have tried to test validation with rule to see if record are in the database before create a new record. This works as expected.

But I can't update a row when the rule are enabled in the code. Any idea why it stop with the loading message (see picture)
[sharedmedia=core:attachments:320]


Here are my code:

public function employees()
{
$crud = new grocery_CRUD();

$crud->set_subject('Employee');
$crud->set_table('employees');
$crud->set_theme('datatables');
//$crud->unset_delete();
//$crud->unset_columns('file_url');
//$crud->set_language('danish');
//$crud->set_lang_string('form_save','Save customer into the database');
$crud->set_rules('email', 'Email','trim|required|xss_clean|callback_email_check');
//$crud->add_action('More', '', 'demo/action_more','ui-icon-plus');
$output = $crud->render();

$this->_example_output($output);
}

public function email_check($str)
{
$id = $this->uri->segment(4);
if(!empty($id) && is_numeric($id))
{
$email_old = $this->db->where("id",$id)->get('employees')->row()->email;
$this->db->where("email !=",$username_old);
}

$num_row = $this->db->where('email',$str)->get('employees')->num_rows();
if ($num_row >= 1)
{
$this->form_validation->set_message('email_check', 'The email already exists');
return FALSE;
}
else
{
return TRUE;
}
}


function _example_output($output = null)

{
$this->load->view('our_template.php',$output);
}


Best Regards
Jesper Vels

victor
  • profile picture
  • Member

Posted 14 October 2012 - 08:28 AM

Try it:


public function email_check($str)
{
$id = $this->uri->segment(4);
if(!empty($id) && is_numeric($id))
{
$email_old = $this->db->where("id", $id)->get('employees')->row()->email;
if($str==$email_old)
{
return true;
}
else
{
$query = $this->db->get_where(employees, array('email' => $str));
if($query->$query->num_rows() > 0)
{
$this->form_validation->set_message('email_check', 'The email already exists');
return false;
}
else
{
return true;
}
}
}
else
{
$query = $this->db->get_where("employees", array('email' => $str));
if($query->$query->num_rows() > 0)
{
$this->form_validation->set_message('email_check', 'The email already exists');
return false;
}
else
{
return true;
}
}
}

jvels
  • profile picture
  • Member

Posted 14 October 2012 - 10:05 AM

Hi

Stil the same with the "Loading, updating changes.." and nothing happens.

With your sugestion it is not possible to save a new record it then stop at save with the message "Loading, saving data.."

/Jesper

victor
  • profile picture
  • Member

Posted 14 October 2012 - 10:12 AM

Can you show an error (of log or firebug)?
Or show the table structure

jvels
  • profile picture
  • Member

Posted 14 October 2012 - 10:25 AM

HI

The database are from the sample:

CREATE TABLE IF NOT EXISTS `employees` (
`employeeNumber` int(11) NOT NULL AUTO_INCREMENT,
`lastName` varchar(50) NOT NULL,
`firstName` varchar(50) NOT NULL,
`extension` varchar(10) NOT NULL,
`email` varchar(100) NOT NULL,
`officeCode` varchar(10) NOT NULL,
`file_url` varchar(250) CHARACTER SET utf8 NOT NULL,
`jobTitle` varchar(50) NOT NULL,
PRIMARY KEY (`employeeNumber`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1703 ;


INSERT INTO `employees` (`employeeNumber`, `lastName`, `firstName`, `extension`, `email`, `officeCode`, `file_url`, `jobTitle`) VALUES
(1002, 'Murphy', 'Diane', 'x5800', 'dmurphy@classicmodelcars.com', '1', '', 'President'),
(1056, 'Patterson', 'Mary', 'x4611', 'mpatterso@classicmodelcars.com', '1', '', 'VP Sales'),
(1076, 'Firrelli', 'Jeff', 'x9273', 'jfirrelli@classicmodelcars.com', '1', '', 'VP Marketing'),
(1088, 'Patterson', 'William', 'x4871', 'wpatterson@classicmodelcars.com', '6', '', 'Sales Manager (APAC)'),
(1102, 'Bondur', 'Gerard', 'x5408', 'gbondur@classicmodelcars.com', '4', 'pdftest.pdf', 'Sale Manager (EMEA)'),
(1143, 'Bow', 'Anthony', 'x5428', 'abow@classicmodelcars.com', '1', '', 'Sales Manager (NA)'),
(1165, 'Jennings', 'Leslie', 'x3291', 'ljennings@classicmodelcars.com', '1', '', 'Sales Rep'),
(1166, 'Thompson', 'Leslie', 'x4065', 'lthompson@classicmodelcars.com', '1', '', 'Sales Rep'),
(1188, 'Firrelli', 'Julie', 'x2173', 'jfirrelli@classicmodelcars.com', '2', 'test-2.pdf', 'Sales Rep'),
(1216, 'Patterson', 'Steve', 'x4334', 'spatterson@classicmodelcars.com', '2', '', 'Sales Rep'),
(1286, 'Tseng', 'Foon Yue', 'x2248', 'ftseng@classicmodelcars.com', '3', '', 'Sales Rep'),
(1323, 'Vanauf', 'George', 'x4102', 'gvanauf@classicmodelcars.com', '3', '', 'Sales Rep'),
(1337, 'Bondur', 'Loui', 'x6493', 'lbondur@classicmodelcars.com', '4', '', 'Sales Rep'),
(1370, 'Hernandez', 'Gerard', 'x2028', 'ghernande@classicmodelcars.com', '4', '', 'Sales Rep'),
(1401, 'Castillo', 'Pamela', 'x2759', 'pcastillo@classicmodelcars.com', '4', '', 'Sales Rep'),
(1501, 'Bott', 'Larry', 'x2311', 'lbott@classicmodelcars.com', '7', '', 'Sales Rep'),
(1504, 'Jones', 'Barry', 'x102', 'bjones@classicmodelcars.com', '7', '', 'Sales Rep'),
(1611, 'Fixter', 'Andy', 'x101', 'afixter@classicmodelcars.com', '6', '', 'Sales Rep'),
(1612, 'Marsh', 'Peter', 'x102', 'pmarsh@classicmodelcars.com', '6', '', 'Sales Rep'),
(1619, 'King', 'Tom', 'x103', 'tking@classicmodelcars.com', '6', '', 'Sales Rep'),
(1621, 'Nishi', 'Mami', 'x101', 'mnishi@classicmodelcars.com', '5', '', 'Sales Rep'),
(1625, 'Kato', 'Yoshimi', 'x102', 'ykato@classicmodelcars.com', '5', '', 'Sales Rep'),
(1702, 'Gerard', 'Martin', 'x2312', 'mgerard@classicmodelcars.com', '4', '', 'Sales Rep');


/Jesper

victor
  • profile picture
  • Member

Posted 14 October 2012 - 11:08 AM

You have an error:


$this->db->where("id", $id)

You haven't field "id"!
you have a field "employeeNumber" as primary key

jvels
  • profile picture
  • Member

Posted 14 October 2012 - 11:15 AM

Hi Victor

Thanks!

It work with your code, when I changed it

Best Regards
Jesper

victor
  • profile picture
  • Member

Posted 14 October 2012 - 11:49 AM

You can change the $config['log_threshold'] (application/config/config.php) and CI will write messages to log files.
more info: http://codeigniter.c...ral/errors.html

Harasya
  • profile picture
  • Member

Posted 27 March 2013 - 14:41 PM

how about two rules in the first example a function name and email???


Harasya
  • profile picture
  • Member

Posted 28 March 2013 - 16:15 PM

function jadwal() {
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');
        $crud->set_table('jadwal');
        $crud->set_subject('Jadwal');
        $crud->display_as('thakademik_idthakademik', 'Tahun Akademik');
        $crud->display_as('dosen_nik', 'Dosen');
        $crud->set_subject('Jadwal Pelajaran');
        $crud->set_relation('matakuliah_kode', 'matakuliah', 'nama');
        $crud->set_relation('dosen_nik', 'dosen', 'nama');
        $crud->set_relation('thakademik_idthakademik', 'thakademik', '{tahun_awal}/{tahun_akhir} / {semester}');
        $crud->set_rules('hari', 'Hari', 'required');
        $crud->set_rules('jam_mulai', 'Jam Mulai', 'required|callback_jadwal_check');
        $crud->set_rules('jam_selesai', 'Jam Selesai', 'required');
        $crud->set_rules('kelas', 'Kelas', 'required|callback_jadwal_check');
        $crud->set_rules('thakademik_idthakademik', 'Tahun Akademik', 'required');
        $crud->set_rules('matakuliah_kode', 'Matakuliah', 'required');
        $crud->set_rules('dosen_nik', 'Dosen', 'required');
        $crud->unset_print();
        $crud->unset_export();
        $output = $crud->render();
        $this->_administrator_output($output);
    }

    public function jadwal_check($kelas, $jam_mulai) {
        $jdwl = $this->uri->segment(4);
        if (!empty($jdwl) && is_numeric($jdwl)) {
            $jdwl_old = $this->db->where("thakademik_idthakademik", $jdwl)->get('jadwal')->row()->jam_mulai;
            $this->db->where("jam_mulai !=", $jdwl_old);
            $jdwl_old_kelas = $this->db->where("thakademik_idthakademik", $jdwl)->get('jadwal')->row()->kelas;
            $this->db->where("kelas !=", $jdwl_old_kelas);
        }

        $num_row = $this->db->where('jam_mulai', $jam_mulai)->get('jadwal')->num_rows();
        $num_row_kelas = $this->db->where('kelas', $kelas)->get('jadwal')->num_rows();
        if (($num_row >=1)&&( $num_row_kelas >= 1)) {
            $this->form_validation->set_message('jadwal_check', 'Jadwal sudah dipakai');
            return FALSE;
        } else {
            return TRUE;
        }
    }

 

no work in me


jaorsoftware
  • profile picture
  • Member

Posted 16 May 2013 - 23:01 PM

Hi !

 

I am testing the code ans i Found error

 

the sentence

 

if($query->$query->num_rows() > 0)

 

Should be

 

if($query->num_rows() > 0)

 

 

Regards !