Welcome Guest, Not a member yet? Register   Sign In
Need help changing the form_validation library
#1

[eluser]php_princess[/eluser]
I often don't like the default error messages and set_message() makes me set the message for that rule on ALL fields in that script. That usually doesn't work out if I need a different message to be displayed depending on what field it was. I'm not too fond of callback functions either because that really seems like way more typing than necessary. I also can't help but wonder if it's also an unnecessary drain on the CPU--small probably, but still unnecessary.

Anyway, I'd like to be able to do this:

Code:
$avatar_url_messages = array(
'min_image_size' => 'Your avatar cannot be smaller than 50 x 50 pixels.',
'max_image_size' => 'Your avatar must be no larger than 150 x 150 pixels.'
); //messages I want to customize for the field get customized in the array

$this->form_validation->set_rules('avatar_url', 'avatar URL', 'valid_url|min_image_size[50,50]|max_image_size[150,150]', $avatar_url_messages);
//default error for valid_url() would be "Your avatar URL was not valid."


$profile_banner_url_messages = array(
'min_image_size' => 'Your profile banner cannot be smaller than 400 x 60 pixels.',
'max_image_size' => 'Your profile banner cannot be bigger than 560 x 80 pixels.'
);

$this->form_validation->set_rules('profile_banner_url', 'profile banner URL', 'valid_url|min_image_size[400,60]|max_image_size[560,80]', $profile_banner_url_messages);
//once again, this doesn't need a custom valid_url message because my default one will do


So, how could I change the form validation class to let me do this? Note: I'm not sure what version of CodeIgniter it is, but I just downloaded it like 3 months ago. I think it's 2.1.something
#2

[eluser]CroNiX[/eluser]
It might be easier to just create separate callbacks for the avatar and banner image validation. Then you could make the message specific to that field and determine which message (cannot be bigger than... or cannot be smaller than...) to set based off of the result of your checking within the callback itself.

You could even reuse your existing min_image_size() and max_image_size() rules within those new callback rules so you don't have duplicate code. Just pass the values received by your new callback to that callback and get the result back. Set the error message depending on that result.
#3

[eluser]php_princess[/eluser]
Thanks for the reply but isn't there still a way to alter the codeigniter core so I could do the above? I've read up on callbacks and it looks like I'd be copying and pasting the function code from script to script. My max_image_size() and min_image_size() functions are in MY_form_validation and I wanted to keep them there, if at all possible.

#4

[eluser]Aken[/eluser]
You would need to store your array of error messages depending on field and rule names. Then, whenever error messages are set, you'd need to check that array to see if a custom message has been specified. This would involve redefining various methods from CI_Form_validation into MY_Form_validation, and then either duplicating most of their code, or using parent::method() if possible. Start there, and post if you have any specific issues.
#5

[eluser]php_princess[/eluser]
Wow, I'm looking at the form_validation library (Form_validation.php) that came with CI and I don't understand most of what is going on. What would you do if you were me? Should I ask you guys a million questions about the code, until I understand it, or should I find some other way to accomplish what I want to do?
#6

[eluser]Aken[/eluser]
CI is fairly well commented. Read the comments, look up the PHP functions, and use some code debugging to see how things are changed. You should be able to pick up most of it just by reading through and researching what you don't recognize. If you get stuck, ask here.
#7

[eluser]skunkbad[/eluser]
You are probably trying to think too hard.

You might take a look at what I've done here:
http://ellislab.com/forums/viewthread/205469/

Using my method of handling external callbacks, you could do something like this:

Code:
public function _is_not_stupid( $value, $arr )
{
// $arr[0] will identify the specific error message to send if there is one

$error_messages = array(
  'a' => 'The value for %s is not acceptable.',
  'b' => 'The value for %s is rediculous.',
  'c' => 'The value for %s is so stupid, you should go home while you still have a chance.',
);

if( $value == 'stupid' )
{
  $this->form_validation->set_message(
   'external_callbacks',
   $error_messages[$arr[0]]
  );

  return FALSE;
}

return $value;
}

For testing type "a", your form validation rule would look something like this:

Code:
external_callbacks[some_model,_is_not_stupid,a]

Simple solution to a simple issue.




Theme © iAndrew 2016 - Forum software by © MyBB