Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 3 - Extending Form Validation not working
#4

First, fix the constructor for MY_Form_validation:
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class 
MY_Form_validation extends CI_Form_validation 
{
 
   public function __construct($rules = array())
 
   {
 
       // Pass the $rules to the parent constructor.
 
       parent::__construct($rules);
 
       // $this->CI is assigned in the parent constructor, no need to do it here.
 
   }

 
   public function is_required($str)
 
   {
 
       if (empty($str))
 
       {
 
           $this->CI->form_validation->set_message('is_required''The %s is required');
 
           return FALSE;
 
       }

 
       return TRUE;
 
   }


and the User controller:
PHP Code:
<?php
class User extends MY_Controller {

 
   public function __construct()
 
   {
 
       parent::__construct();
 
       // No need to use get_instance() in a controller 
 
       // which extends a child of CI_Controller.
 
   }

 
   public function login()
 
   {
 
       if ($this->input->post('submit'))
 
       {
 
           $this->form_validation->set_rules('username''Username''is_required');
 
           $this->form_validation->set_rules('password''Password''is_required');
 
           if ($this->form_validation->run() === FALSE)
 
           {
 
               echo " Failed <br/>";
 
           }
 
           else
            
{
 
               echo " Success <br/>";
 
           }
 
       }
 
   }


However, the problem may simply be in your is_required() method's logic, as empty() isn't necessarily going to give you the expected result. This is why CI's Form_validation->required() method looks like this:
PHP Code:
    public function required($str)
    {
        return 
is_array($str) ? (bool) count($str) : (trim($str) !== '');
    } 
Reply


Messages In This Thread
RE: CodeIgniter 3 - Extending Form Validation not working - by mwhitney - 09-04-2015, 07:28 AM



Theme © iAndrew 2016 - Forum software by © MyBB