Welcome Guest, Not a member yet? Register   Sign In
Form validation callback return value
#1

[eluser]Anonymous[/eluser]
hi there,

Anyone knows why my callback return value is being ignored ?

Code:
$this->form_validation->set_rules('groningen', 'groningen', 'callback_username_check');

the callback
Code:
function username_check() {    
        return 'testing';
    }

Code:
var_dump($this->input->post('groningen'));
gives my: boolean false, instead of: string 'testing' as what it is supposed to do.
#2

[eluser]Eric Barnes[/eluser]
Not really sure what the goal is by returning a string of testing but that is not how it is designed. Please see:
http://ellislab.com/codeigniter/user-gui...#callbacks
#3

[eluser]adityamenon[/eluser]
I don't think setting a form_validation rule will automatically change the POSTed data... why are you var_dump()ing $this->input->post('groningen')?
#4

[eluser]Anonymous[/eluser]
the documentation says it should work that way.

"If your callback returns anything other than a boolean TRUE/FALSE it is assumed that the data is your newly processed form data."
#5

[eluser]adityamenon[/eluser]
Are you dumping the data _after you've done this: $this->form_validation->run()?
#6

[eluser]mr lister[/eluser]
I think you are misunderstanding the documentation.

In your callback function, carry out what ever conditional statements you need on the POSTed data, then return the boolean result as either TRUE/FALSE depending on the result.

I agree with adityamenon, the form validation / callback function will not change your POSTed form data, you are only validating the form data based on set criteria.

The example:
Code:
if ($str == 'test')
{
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
will return - set the message - text as part of your validation for the field that called the function.

The documentation gives the above example, as Eric pointed out with the user guide link.
#7

[eluser]Ashes-to-Ashes[/eluser]
It was working for me, with return $value instead of return false. But now it's not so I think there may be more to this.
#8

[eluser]Ashes-to-Ashes[/eluser]
In fact, I just verified that YES you can return a value instead of true or false. It is working on one of my callbacks but not the other.

Non working function
Code:
public function _tag_number_check($str)
{
  echo '<br>CHECKING TAG NUMBER WITH HIDDEN TAG NUMBER CHECK IN MAIN CONTROLLER';
  print_r ($_POST);
  if ($str == '') { $str = $_POST['tag']; }
  if ($str == '') {
   echo 'String is Blank';
   $this->form_validation->set_message('_tag_number_check', '%s is blank.');
   return false;
  } // if blank, but required, then handled in form area
  $this->load->model ('tags_model');
  // 9/5/2012 Jonathan seems this is already loaded if check being called
  //$this->load->library ('form_validation');
  if (!is_numeric($str))
  {
   echo "'$str' not numeric";
   $this->show->line (' Tag number not numeric, attempting look up as a string of words or words seperated by "&" symbol ');
   $query = $this->tags_model->select_query_from_search ($str);
   if ($query)
   {
    echo "Query returned results <br>";
    if ($query->num_rows > 1)
    {
     $this->form_validation->set_message('_tag_number_check', 'More than one tag number found for search');
     foreach ($query->result() as $row)
     {
      $this->show->line ($row->number. ' -- '.$row->label);
     }
     return FALSE;
    }
    elseif ($query->num_rows == 1)
    {
     echo "Only one match found. Tag number from this match.<br>";
     $row = $query->row();
     echo "Number is: $row->number<br>";
     return $row->number;
     // TASK: Figure out why this value is dropped somewhere between current and form using data
     // Currently have to rewrite the data in three places instead of just here
    }
    else
    {
     $this->form_validation->set_message('tag_number_check', 'No tags found using text search ');
     return FALSE;
    }
   } // query true
   else { $this->form_validation->set_message ('tag_number_check', 'No results for that search');
   }
  } // is numeric
  else
  {
   if ($this->tags_model->select_tag ($str))
   {
    return true;
   }
   else
   {
    $this->form_validation->set_message ('tag_number_check', 'No results for that tag number');
    return FALSE;
   }
  }
  return false;
  // TASK: look for valid tag number
} // function tag_number_check

Working callback
Code:
public function _account_number_check($str)
{
  if ($str == ``) {
   return true;
  } // if blank, but required, then handled in form area
  echo 'CHECKING ACCOUNT NUMBER HIDDEN FUNCTION MAIN CONTROLLER';
  $this->load->model ('accounts_model');
  $this->load->library ('form_validation');
  if (!is_numeric($str))
  {
   $this->show->line (' Account number not numeric, attempting look up as a string ');
   $query = $this->accounts_model->select_query_from_search ($str);
   if (!empty ($query))
   {
    if ($query->num_rows > 1)
    {
     $this->form_validation->set_message('_account_number_check', 'More than one account found for search');
     foreach ($query->result() as $row)
     {
      $this->show->line ($row->number. ' -- '.$row->name);
     }
     return FALSE;
    }
    elseif ($query->num_rows === 1)
    {
     $row = $query->row();
     return $row->number;
    }
    else
    {
     $this->form_validation->set_message('account_number_check', 'No accounts found using text search (use word & word & word .....)');
     $this->show->link ('Right click on this link, and open in new tab to create account
       then close that tab when done, and re-try this form',
       array ('account', 'action', 'new'));
     // TASK: pop-up account creation with pick root...
     // When window closes insert that number in the field, and use
     // JAVA to show name?
     return FALSE;
    }
   }
   return false;
  }
  return true; // TASK: check for bad account number
}

I am guessing that my particular error is is somewhere else, but I don't know where or how.
#9

[eluser]Ashes-to-Ashes[/eluser]
Okay, my callback is now correctly returning the tag number as the new post data. It was a bug in my call back rule, I had specified the wrong field name.

But, this should prove that it will work for you given that you have the correct CodeIgniter version. I have 2.0.1
#10

[eluser]Aken[/eluser]
Callbacks can indeed return a revised or completely new value for the provided form element. As long as it is not boolean. Remember that validation rules are run in order from left to right, and that have to call $this->form_validation->run() in order for the new value to show up.




Theme © iAndrew 2016 - Forum software by © MyBB