2

Im carrying out some form validation with codeigniter using a custom validation callback.

$this->form_validation->set_rules('testPost', 'test', 'callback_myTest');

The callback runs in a model and works as expected if the return value is TRUE or FALSE. However the docs also say you can return a string of your choice.

For example if I have a date which is validated, but then in the same function the format of the date is changed how would I return and retrieve this new formatted value back in my controller?

Thanks for reading and appreiate the help.

2
  • a vote down without a comment? A bit harsh. I did look to see if the question exists. the closest I found was: stackoverflow.com/questions/9486274/… which didnt answer my question Commented May 16, 2012 at 22:37
  • I have no idea why you got votedown but here's +1 from me. This question is a good question. Commented May 17, 2012 at 2:31

5 Answers 5

1

I'm not entirely sure I got what you were asking, but here's an attempt.

You could define a function within the constructor that serves as the callback, and from within that function use your model. Something like this:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Controllername extends CI_Controller {

   private $processedValue;

   public function index()
   {
        $this->form_validation->set_rules('testpost','test','callback');

        if ($this->form_validation->run()) {
         //validation successful
         echo $this->processedValue; //outputs the value returned by the model
        } else {
         //validation failed
        }
   }

   private function callback($input)
   {
        $this->load->model('yourmodel');
        $return = $this->yourmodel->doStuff($input);

        //now you have the user's input in $input
        // and the returned value in $return

        //do some checks and return true/false

        $this->processedValue = $return;
   }

}
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry about it being unclear. Maybe thats why someone downvoted. What I meant was when a value is validated in the callback function which is in my model it is also processed, in this case the format of the submitted date is changed. I wanted to return this new value so that I can then use it in my controller. thanks for your help
In my example above, when your model returns the value to the callback in the controller, you can assign this processed value to a private variable. The value will then be accessible as you need. See edit.
@fl3x7: Did that solve your issue? Was that what you were looking for?
yep that works although what im thinking of doing is moving the callabck functions into the controller and just assign the new variable to $this->data['something']. Although I know this goes against the skinny controller fat model principal. Thanks for your help mate :)
1
public function myTest($data){ // as the callback made by "callback_myTest"
     // Do your stuff here
    if(condition failed)
    {
        $this->form_validation->set_message('myTest', "Your string message");
        return false;
    } 
    else 
    {
       return true;
    }
}

Please try this one.

Comments

0

I looked at function _execute in file Form_validation of codeigniter. It sets var $_field_data to the result of callback gets(If the result is not boolean). There is another function "set_value". Use it with the parameter which is name of your field e.g. set_value('testPost') and see if you can get the result.

1 Comment

Thanks for your reply. So im returning the new string and then echoing set_value('testPost') but the value is still the original pre-processed value.
0

The way Tank_Auth does this in a controller is like so

$this->form_validation->set_rules('login', 'Login', 'trim|required|xss_clean');

if ($this->form_validation->run()) {        
// validation ok

$this->form_validation->set_value('login')

}

Using the set_value method of form_validation is undocumented however I believe this is how they get the processed value of login after it has been trimmed and cleaned.

I don't really like the idea of having to setup a new variable to store this value directly from the custom validation function.

Comments

0

edit: sorry, misunderstood the question. Use a custom callback, perhaps. Or use the php $_POST collection (skipping codeigniter)...apologies haven't tested, but I hope someone can build on this...

eg:

function _is_startdate_first($str)
{
           $str= do something to $str;

            or 

            $_POST['myinput'} = do something to $str;

    }

================

This is how I rename my custom callbacks:

$this->form_validation->set_message('_is_startdate_first', 'The start date must be first');

.....

Separately, here's the callback function:

function _is_startdate_first($str)
{
    $startdate = new DateTime($this->input->post('startdate'), new DateTimeZone($this->tank_auth->timezone()));
    $enddate = new DateTime($this->input->post('enddate'), new DateTimeZone($this->tank_auth->timezone()));

    if ($startdate>$enddate) {
        return false;
        } else {
        return true;
        }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.