• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation ... callback doesn't get triggered

#11
[eluser]FinalFrag[/eluser]
Ok, that's a more useful reply. I will changing the language file.

I'm not up to a level in CodeIgniter that I can do the MY_Validation thing Smile I wouldn't know where to start :p

I'll continue looking for a working solution...

#12
[eluser]FinalFrag[/eluser]
I checked this language file, but it's not really what I'm looking for.

What I want is to create my own validation function. So I don't have to use the required, trim, etc functions.

So what I'm actually looking for is a way to call this function within the validation->run() function... How do I do this?

#13
[eluser]xwero[/eluser]
The problem is you can't get past the empty string check in the run method.
Code:
if ( ! in_array('required', $ex, TRUE))
            {
                if ( ! isset($_POST[$field]) OR $_POST[$field] == '')
                {
                    continue;
                }
            }
This check is even before the rules are known in the run method.

But i don't understand why the fields aren't required? If someone logs in they need to add at least their login data?

#14
[eluser]FinalFrag[/eluser]
They are required. But I don't want the CI error message.

I would like to create my own validation function so that I have more control over which messages get shown.

Example:
If you leave both fields empty and set the rules to required, you will get the following:

Code:
The username field is required.
The password field is required.

So I get 2 messages...


If I can write my own validation function, I can bundle these messages to just one:

Code:
Please fill in all fields before submitting.

#15
[eluser]xwero[/eluser]
I think the only solution for this problem is to overload the required rule
Code:
class MY_Validation Extends CI_Validation
{
   function required($str,$callback=FALSE)
   {
        if(is_string($callback) && method_exists($this, $callback)
        {
            return $callback();
        }
        if ( ! is_array($str))
        {
            return (trim($str) == '') ? FALSE : TRUE;
        }
        else
        {
            return ( ! empty($str));
        }
   }
}
Then you have to make your rule
Code:
$rules['password'] = 'trim|required[_check_user_name]';

#16
[eluser]wiredesignz[/eluser]
Giving this some thought. Tongue


Empty fields are not $_POST'd by the form, so validation has nothing to check against other than required.

You could however create a hidden username field located before the actual field in the form and giving it a default value to test against in your callback_function.

This value would be replaced by a valid username, or you could use a bit of javascript magic to set its value when both username and password are filled in.

#17
[eluser]FinalFrag[/eluser]
Hidden fields are not the correct way to do stuff. Those are easily tampered by evil users. I will try the MY_Validation thing when I get home though...

I'll keep you posted...

#18
[eluser]xwero[/eluser]
I don't like the hidden fields approach it adds more fields than needed just to make your code work.

BTW echoing is a weak typed function try var_dump instead Wink

#19
[eluser]FinalFrag[/eluser]
Didn't know about var_dump... I'll check it out as well...

#20
[eluser]wiredesignz[/eluser]
Another solution would be to alter the Validation language message for `required` to suit your requirements and then only use the first error message as a warning in the form.

Code:
list($error_message) = split("\n", $this->validation->error_string);


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.