VladimirsBudkins

CI Form Validation

Feb 1st, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.55 KB | None | 0 0
  1. <?php
  2.  
  3. //libraries/MY_Form_validation
  4. class MY_Form_validation extends CI_Form_validation {
  5.  
  6.     public function errors_array() {
  7.         return $this->_error_array;
  8.     }
  9.  
  10. }
  11.  
  12. //core/MY_Model
  13. class MY_Model extends CI_Model {
  14.  
  15.     protected $validation_rules = [];
  16.  
  17.     public function __construct() {
  18.         parent::__construct();
  19.     }
  20.  
  21.     public function validate(array $rulesKeys = null, $returnErrors = false) {
  22.         if (!$rulesKeys) {//skip validation
  23.             return $returnErrors ? [] : true;
  24.         }
  25.         if (!$this->load->is_loaded('form_validation')) {
  26.             $this->load->library('form_validation');
  27.         }
  28.         $rules = array_intersect_key($this->validation_rules, array_flip($rulesKeys));
  29.         foreach ($rules as $rule) {
  30.             $this->form_validation->set_rules(
  31.                     $rule['field'],
  32.                     $rule['label'],
  33.                     $rule['rules'],
  34.                     isset($rule['errors']) ? $rule['errors'] : []
  35.             );
  36.         }
  37.         $valid = $this->form_validation->run();
  38.         return $returnErrors ? $this->form_validation->errors_array() : $valid;
  39.     }
  40.  
  41. }
  42.  
  43. //models/FundsModel
  44. class FundsModel extends MY_Model {
  45.  
  46.     protected $validation_rules = [
  47.         'type' => [
  48.             'field' => 'funds[type]',
  49.             'label' => 'Funds type',
  50.             'rules' => [
  51.                 'required'
  52.             ],
  53.             'errors' => [
  54.                 'required' => 'Please select funds type'
  55.             ]
  56.         ],
  57.         'program' => [
  58.             'field' => 'funds[program]',
  59.             'label' => 'Funds program',
  60.             'rules' => [
  61.                 'required'
  62.             ],
  63.             'errors' => [
  64.                 'required' => 'Please select funds program'
  65.             ]
  66.         ]
  67.     ];
  68.  
  69.     public function __construct() {
  70.         parent::__construct();
  71.     }
  72.  
  73. }
  74.  
  75. //controllers/Funds
  76. class Funds extends PrivateController {
  77.  
  78.     public function __construct() {
  79.         parent::__construct();
  80.         $this->load->model('FundsModel');
  81.     }
  82.  
  83.     public function form() {
  84.         $post = $this->input->post();
  85.         if ($post) {
  86.             $rulesKeys = ['type'];
  87.             if (isset($post['funds']['type']) && $post['funds']['type'] == 'program') {
  88.                 array_push($rulesKeys, 'program');
  89.             }
  90.             if ($this->FundsModel->validate($rulesKeys)) {
  91.                 //save form data, set message and redirect
  92.             }
  93.         }
  94.         $this->load->view('form');
  95.     }
  96.  
  97. }
Add Comment
Please, Sign In to add comment