Skip to content

Instantly share code, notes, and snippets.

@willmendesneto
Last active July 18, 2016 12:17
Show Gist options
  • Save willmendesneto/6000734 to your computer and use it in GitHub Desktop.
Save willmendesneto/6000734 to your computer and use it in GitHub Desktop.
Validation in Codeigniter + Grocery CRUD
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Annotations extends MY_Controller {
/**
* Class Constructor
*
* @return void
*/
public function __construct()
{
parent::__construct();
// Setting the validation rules
$this->setValidationRules( array(
array(
'field' => 'description'
,'label' => 'Description'
,'rules' => 'trim|required'
),
));
}
/**
* Render CRUD from "tbsys_qualification" table in Database
* @return void
*/
public function index() {
try {
// If you use more than one databases
$this->db = $this->load->database('default', true);
// Setting page title
$this->util->set_title('Notes');
$this->grocery_crud->columns('id', 'description')
->set_table('annotations')
->order_by('description', 'ASC')
->set_subject('Annotations');
if( in_array($this->grocery_crud->getState(), array('insert', 'insert_validation', 'update', 'update_validation')) ) {
$this->grocery_crud->set_rules($this->getValidationRules());
}
$output = $this->grocery_crud->render();
$this->load->view($output);
}catch(Exception $e){
show_error("Message: {$e->getMessage()}\n Trace as String: {$e->getTraceAsString()}");
}
}
}
/* End of file Annotations.php */
/* Location: .//D/projects/timeforteam/codigo/sistema/controllers/annotations.php */
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
/**
* Validation rules
* @param array $_rules
*/
private $_validationRules = array();
public function __construct()
{
parent::__construct();
}
/**
* Getting the CRUD validation rules
*
* @return array
*/
public function getValidationRules() {
return $this->_validationRules;
}
/**
* Setting the CRUD validation rules
*
* @param array $newvalidationRules Validation rules array
*/
public function setValidationRules($_validationRules) {
$this->_validationRules = $_validationRules;
return $this;
}
}
/* End of file MY_Controller.php */
/* Location: ./application/controllers/MY_Controller.php */
@gperez-vzla
Copy link

Real library Grocery CRUD is'nt used in this example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment