Skip to content

Instantly share code, notes, and snippets.

@tubalmartin
Created December 20, 2012 15:13
Show Gist options
  • Save tubalmartin/4345844 to your computer and use it in GitHub Desktop.
Save tubalmartin/4345844 to your computer and use it in GitHub Desktop.
Sistema reglas validación multi-form
<?php
class MY_Model extends CI_Model {
// Form validation rules
protected $rules;
// Sets form validation rules
protected function set_rules($field_name)
{
if (is_string($field_name) && array_key_exists($field_name, $this->rules))
{
$this->form_validation->set_rules($field_name, $this->rules[$field_name]['label'], $this->rules[$field_name]['rules']);
}
else if (is_array($field_name))
{
foreach ($field_name as $fname)
{
if (array_key_exists($fname, $this->rules))
{
$this->form_validation->set_rules($fname, $this->rules[$fname]['label'], $this->rules[$fname]['rules']);
}
}
}
}
}
class Brand_model extends MY_Model
{
// Form validation rules
protected $rules = array(
'name' => array('label' => 'lang:company_name', 'rules' => 'trim|required'),
'email' => array('label' => 'lang:email', 'rules' => 'trim|required|valid_email'),
'vat_number' => array('label' => 'lang:vat_number', 'rules' => 'trim|required|valid_vat_number'),
'logo' => array('label' => 'lang:logo', 'rules' => ''), // File
'country_id' => array('label' => 'lang:country', 'rules' => 'trim|required|external_callbacks[common,is_country]'),
'state_id' => array('label' => 'lang:state', 'rules' => 'trim|required|external_callbacks[common,is_state]'),
'website' => array('label' => 'lang:website', 'rules' => 'trim|valid_url'),
'facebook_page' => array('label' => 'lang:facebook_page', 'rules' => 'trim|valid_url'),
'twitter_page' => array('label' => 'lang:twitter_page', 'rules' => 'trim|valid_url')
);
// Podrían crearse tb en el constructor usando $this->rules = array(...)
public function validate_company_info()
{
// File validation rules & file uploading
$logo_config = base64_encode(serialize(array(
'field_name' => 'logo',
// CI upload config
'upload_path' => WEBPATH . 'data/logo/',
'allowed_types' => 'png|gif|jpg|jpeg',
'max_size' => 1024 / 2, // 0.5 Mb
'encrypt_name' => TRUE
)));
// Incluso si necesitas pasar parametros dinamicamente, es posible.
$this->rules['logo']['rules'] = 'external_callbacks_nopost[common_model,upload,'.$logo_config.']';
// Set validation rules
$this->set_rules(array('name', 'email', 'vat_number', 'logo'));
return $this->form_validation->run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment