Welcome Guest, Not a member yet? Register   Sign In
Custom validation (callback) for blank field value
#11

[eluser]Dan Dar3[/eluser]
No worries, man, I'll try to find some example on the forums. Thanks for your help so far.
#12

[eluser]eangkasa[/eluser]
hi guys, i'm currently facing this problem as well, i have tried to comment the below code in validation.php
but somehow, it still does not work, my callback function isn't getting called without using required in front ,any recommendation ?

i have browsed through the forum for approx 2 hours, i'm getting all sort of inputs but i think by overriding the validation.php is the quickest way, but it wont work for now ... any suggestion? thank you Big Grin

// Is the field required? If not, if the field is blank we'll move on to the next test
if ( ! in_array('required', $ex, TRUE))
{
if ( ! isset($_POST[$field]) OR $_POST[$field] == '')
{
continue;
}
}
#13

[eluser]drewbee[/eluser]
Hi Eangkasa,

This is a limitation of the current CI version.

If you take a look at the new form validation class in the subversion 1.7, you will see callbacks can now be called without any other rules being set.
#14

[eluser]eangkasa[/eluser]
hi drewbee, yup i have noticed that rick has released the version 1.7 for form validation class, i have checked it before from http://ellislab.com/forums/viewthread/89251/P90/#462484

do you know how to get access into it ? is it from this link ?
http://ellislab.com/forums/viewthread/89251/P90/#462484

i wonder if it is safe to overwrite my current validation.php with the 1.7 version, cuz this version is not officially released yet? i wonder if it still has some bugs inside or be conflicted the functionality of my current version of validation.php ?? thanks for the quick reply Smile
#15

[eluser]hvalente13[/eluser]
Hi,

You can get it here: http://dev.ellislab.com/svn/CodeIgniter/...dation.php.

I've downloaded it and till now I didn't get any conflict. But I didn't overwrite the old one, just renamed it Validation_old.php and put the new code in Validation.php.
#16

[eluser]eangkasa[/eluser]
this is really weird ... i have copied the code from the link that you gave me before hvalente13, but it doesn't work ... the callback still not getting called without putting "required" in front.

$rules["whateverfield"] = "callback__some_function";

function _some_function($the_parameter)
{
$this->validation->set_message('_some_function', "Some error message.");
return FALSE;
}

Basically, the code above is a part of my code snippet, it is a very simple to start with cuz it will always return FALSE "IF" the callback function is being called on the first place.
#17

[eluser]drewbee[/eluser]
I would recomend downloading everything. Their were parts of helper files that got used within the new validation class as well.
#18

[eluser]herringtown[/eluser]
Having the same problems here and looking more in depth at the Validation class, it seems like no further rules can be executed if there's an empty field and no 'required' rule. Here's what I'm seeing:

Code:
if ( ! isset($_POST[$field]))
            {            
                if (in_array('isset', $ex, TRUE) OR in_array('required', $ex))
                {
                    if ( ! isset($this->_error_messages['isset']))
                    {
                        if (FALSE === ($line = $this->CI->lang->line('isset')))
                        {
                            $line = 'The field was not set';
                        }                            
                    }
                    else
                    {
                        $line = $this->_error_messages['isset'];
                    }
                    
                    // Build the error message
                    $mfield = ( ! isset($this->_fields[$field])) ? $field : $this->_fields[$field];
                    $message = sprintf($line, $mfield);

                    // Set the error variable.  Example: $this->username_error
                    $error = $field.'_error';
                    $this->$error = $this->_error_prefix.$message.$this->_error_suffix;
                    $this->_error_array[] = $message;
                }
                        
                continue;
            }


I'm using the 1.7.1 codebase. To expound on my problem, I'm trying to use the Validation classes to accomplish the following -- and very basic -- validations :

1) I've got a dropdown field that has "team names". There's a text field beneath it if your team name is not in the "team names" list and want to provide it manually. Now, the way the validation works, obviously you've got to EITHER specify your team name in the dropdown OR provide it in the manual field.

Since I can't make either required (either is optional -- you just have to provide one), I added a callback function for the dropdown field to check for the presence of an "other" team name if the dropdown is not selected :

Code:
// ...
    $rules['club_name']         = "callback_other_check[club_name_other]";
    $rules['club_name_other']    = "trim";
        //..

function other_check($str, $field)
{
    if (empty($_REQUEST[$field]) && empty($str))
    {
    $this->validation->set_message('other_check','Please choose a selection for %s or provide a name in the field below');

    return false;
    }
        
    return true;
}

Now, this sadly doesn't work Sad. I'd really like to figure out how to do this though -- the hyper-simplistic "required" filter just ain't cuttin' it. I suppose I could extend the Validation class...eh.

On to the next scenario ... :

2) Again, with the "required"'s. So, I've got 5 TOS checkboxes that need checking off. If they are not checked, I need the error message to be something like "Hey, dillweed, you forgot to check this checkbox and signify you agree to my TOS: %s". Now, basically, I've got other text fields that use the "required" rule -- where the default "The <field_name> field is required" text is reasonable. BUT. I need different text for the checkboxes -- semantically, just more helpful from a UI standpoint.

So, instead of "required", i thought I would create a custom callback called "callback_required_tos" which worked like this :

Code:
function required_tos($val)
   {
      if (empty($val))
      {
          $this->validation->set_message('required_tos','Hey, dillweed, you forgot to check this checkbox and signify you agree to my TOS: %s');
          return false;
      }
      return true;
   }

Now, of course, I've found that this callback will never be arrived if the checkbox isn't clicked (because its empty and the run() routine continue's out (above) to begin with. So...I"m forced to use the required flag, which is suboptimal. Fortunately, I dug in and found that the 'isset' error message is what gets set with radios/checkboxes so I'm able to have 2 different required messages with :

Code:
$this->validation->set_message('isset', 'You need to check : %s');


The good news here is that this is my first day using CI and I'm probably doing something complete retarded Smile. So, hopefully someone can point out what obvious mistake I'm making and show me how to correctly accomplish what I'm trying to do. If not, I'm going to be bummed....I was hoping that CI could fill in for symfony on projects that don't require as much complexity.

Help!

Thanks in advance,
Greg
#19

[eluser]herringtown[/eluser]
ah...ok, so bonehead thing #1 I just noticed is that I'm using the old "Validation" class (deprecated) instead of the "Form_Validation" class. I'll re-examine my scenario using the new class and see if that changes anything -- i suspect it will Smile




Theme © iAndrew 2016 - Forum software by © MyBB