Welcome Guest, Not a member yet? Register   Sign In
CI 2.1.0 form validation external callbacks via the newly available param
#1

[eluser]skunkbad[/eluser]
I've done my share of bitching about CI lately, nevertheless, I thought I'd share a little code that makes working with bigger applications more manageable. CI does not by itself accommodate external callbacks for form validation, and models are a great place to store them, since the CI super object is accessible from inside them.

What I've done is created a generic external_callbacks method in MY_Controller. The method explodes the comma seperated string that contains the model name, method, and any arguments that should be passed along as an array.

Here is the method:

Code:
/*
* external_callbacks method handles form validation callbacks that are not located
* in the controller where the form validation was run.
*
* $param is a comma delimited string where the first value is the name of the model
* where the callback lives. The second value is the method name, and any additional
* values are sent to the method as a one dimensional array.
*
* EXAMPLE RULE:
*  callback_external_callbacks[some_model,some_method,some_string,another_string]
*/
public function external_callbacks( $postdata, $param )
{
$param_values = explode( ',', $param );

// Make sure the model is loaded
$model = $param_values[0];
$this->load->model( $model );

// Rename the second element in the array for easy usage
$method = $param_values[1];

// Check to see if there are any additional values to send as an array
if( count( $param_values ) > 2 )
{
  // Remove the first two elements in the param_values array
  array_shift( $param_values );
  array_shift( $param_values );

  $argument = $param_values;
}

// Do the actual validation in the external callback
if( isset( $argument ) )
{
  $callback_result = $this->$model->$method( $postdata, $argument );
}
else
{
  $callback_result = $this->$model->$method( $postdata );
}

return $callback_result;
}

/*******************************************************************/
#2

[eluser]ajustesen[/eluser]
CroNIX... do you mind giving a quick example of how you used it with a controller and a model, can't seem to get your Form_validation library version to work.
#3

[eluser]erikig[/eluser]
Hey Brian,

Just wanted to say thanks, its a mighty elegant fix. This was one of the more annoyances of Form Validation (the other being that it only works on _POST instead on any input i.e _GET, _COOKIE etc)

E
#4

[eluser]CroNiX[/eluser]
@ajustesen I'm sorry I didn't see your post til now. Please start a new thread and post what you have tried and I will help. I don't want to mess with Brians thread and I apologize to Brian for posting in his.
#5

[eluser]skunkbad[/eluser]
[quote author="erikig" date="1328234983"]Hey Brian,

Just wanted to say thanks, its a mighty elegant fix. This was one of the more annoyances of Form Validation (the other being that it only works on _POST instead on any input i.e _GET, _COOKIE etc)

E[/quote]

I've never heard of submitting a form with cookie data, but of course $_GET functionality would be useful. Glad you found the code useful.
#6

[eluser]theshiftexchange[/eluser]
just wanted to say thanks - this code is so awesome
#7

[eluser]chenx[/eluser]
Hello.

Thanks for skunkbad for solving my problem with Form Validation.

But still, I have some things needed to be solve here, it's the error message.

Because we're using the function external_callback, the error message only return the error for external_callback.

How can I return the error message from each of my model function?

Sorry for my bad english. Thanks for helping.
#8

[eluser]theshiftexchange[/eluser]
[quote author="chenx" date="1334897650"]
Because we're using the function external_callback, the error message only return the error for external_callback.

How can I return the error message from each of my model function?
[/quote]

You always use the 'external_callback' error message - it will display the error for each of your models as needed
#9

[eluser]LeMec[/eluser]
Hello,

Thanks, for this skunkbad. I looked at your community auth and tried to take the My_form_validation and use it in my project.

I am using HMVC so it did not work.

I also have my models organized in sub directories so the code could not find the specified model.

I have made a few changes that seemed to fix the problem for me...

Code:
public function external_callbacks( $postdata, $param )
{
  $param_values = explode( ',', $param );

  // Make sure the model is loaded
  $model = $param_values[0];

  //Alain: See if there is a path...
  $model_path= explode( '/',$model );
  $model_segments= count($model_path );
  if ($model_segments > 1)
  {
   $modelObj=$model_path[$model_segments -1];
  }
  else
  {
   $modelObj=$model;
  }

  $this->CI->load->model( $model );

  // Rename the second element in the array for easy usage
  $method = $param_values[1];

  // Check to see if there are any additional values to send as an array
  if( count( $param_values ) > 2 )
  {
   // Remove the first two elements in the param_values array
   array_shift( $param_values );
   array_shift( $param_values );

   $argument = $param_values;
  }

  // Do the actual validation in the external callback
  if( isset( $argument ) )
  {
   //Alain: now using $modelObj
   $callback_result = $this->CI->$modelObj->$method( $postdata, $argument );
  }
  else
  {
   //Alain: now using $modelObj
   $callback_result = $this->CI->$modelObj->$method( $postdata );
  }

  return $callback_result;
}

Edit: I also need to enter the full path of where my model is in my validation rules...

So, if I did not enter a path, then this would not find the model, but If I entered a path, then it would find the model load it but when trying to use it it would reference the object with the path.
#10

[eluser]skunkbad[/eluser]
I've revised the way I'm using external callbacks. I just wanted to be able to use libraries and models at the same time. See comments:

Code:
/**
* Generic callback used to call callback methods for form validation.
*
* @param  string  the value to be validated
* @param  string  a comma separated string that contains the model name,
*                 method name and any optional values to send to the method
*                 as a single parameter.
*
*                 - First value is the external class type (library or model)
*                 - Second value is the name of the class.
*                 - Third value is the name of the method.
*                 - Any additional values are values to be
*                   sent in an array to the method.
*
*      EXAMPLE RULE FOR CALLBACK IN MODEL:
*  external_callbacks[model,model_name,some_method,some_string,another_string]
*  
*      EXAMPLE RULE FOR CALLBACK IN LIBRARY:
*  external_callbacks[library,library_name,some_method,some_string,another_string]
*/
public function external_callbacks( $postdata, $param )
{
$param_values = explode( ',', $param );

// Load the model or library holding the callback
$class_type = $param_values[0];
$class_name = $param_values[1];
$this->CI->load->$class_type( $class_name );

// Apply method name to variable for easy usage
$method = $param_values[2];

// Check to see if there are any additional values to send as an array
if( count( $param_values ) > 3 )
{
  // Remove the first three elements in the param_values array
  $argument = array_slice( $param_values, 3 );
}

// Do the actual validation in the external callback
if( isset( $argument ) )
{
  $callback_result = $this->CI->$class_name->$method( $postdata, $argument );
}
else
{
  $callback_result = $this->CI->$class_name->$method( $postdata );
}

return $callback_result;
}




Theme © iAndrew 2016 - Forum software by © MyBB