Welcome Guest, Not a member yet? Register   Sign In
form_validation->set_message issue
#1

[eluser]jcjc[/eluser]
I'm trying to set a custom error message for a function that checks whether user input field matches a record from the database.

the query runs fine and returns a valid/invalid result.

I'm trying to set a custom error message and it doesn't seem to be working.

the input field is:

Code:
$this->form_validation->set_rules('registerCode', 'Reg Code', 'trim|required|callback__checkRegCode');

My query to check regCode is:

Code:
if(!$checkRegCode = $this->userModel->_checkRegCode())
   {
    echo "invalid code";
    $this->form_validation->set_message('_checkRegCode', 'Reg Code invalid');
   }

I get a blank screen - form_validation is autoloaded as well as xss_filtering is set to true in config.

function to check for regCode is:

Code:
function _checkRegCode()
{
  $regCodeInput = $this->input->post('registerCode');

  $this->db->select('RegCode');
  $this->db->where('RegCode', $regCodeInput);

  $checkRegCode = $this->db->get('RegCode');
  
  if($checkRegCode->num_rows == 1)
  {
   return $checkRegCode;
  }
}

thanks
#2

[eluser]Tpojka[/eluser]
Code:
if($checkRegCode->num_rows() == 1)
#3

[eluser]jcjc[/eluser]
[quote author="Tpojka" date="1396962087"]
Code:
if($checkRegCode->num_rows() == 1)
[/quote]

doesn't need closing brackets as will run without, added them and still non working.
#4

[eluser]Tpojka[/eluser]
Could be CI expects from callback function to be true or false. Can you adapt it like that and make a try?
#5

[eluser]Tim Brownlaw[/eluser]
There are a few ways to do this... This is but one way which you can elaborate on to suit...
I've not tested this code so it's "As is"...

Your Model Method
Code:
/**
* The Model method
*
**/
function _checkRegCode($regCodeInput)
{
$this->db->select('RegCode');
$this->db->where('RegCode', $regCodeInput);

$checkRegCode = $this->db->get('RegCode');

if($checkRegCode && $checkRegCode->num_rows() == 1)
{

  return TRUE; // The Registration Code Exists
}

return FALSE; // Couldn't find it
}

And your Callback method in your Forms Controller...

Code:
/**
* The Form Callback Method in your Forms Controller
*
**/
function _checkRegCode($registerCode)
{

  $checkRegCode = $this->userModel->_checkRegCode($registerCode);
  
  if($checkRegCode === FALSE)
  {
$this->form_validation->set_message('_checkRegCode', 'Reg Code invalid');

return FALSE;  // Tell the Form Validation we have an Issue!
  }

return TRUE;  // Everything is Honkey Dorey!
}

If that all goes 'pear shaped'.... Let me know!
Give it a once over for typos and things... it's the wee hours (3am) here!

Cheers
#6

[eluser]jcjc[/eluser]
Thanks - it worked.

Also in frustration built my own message handling function and loads into every view.





Theme © iAndrew 2016 - Forum software by © MyBB