Welcome Guest, Not a member yet? Register   Sign In
Custom validation (callback) for blank field value
#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


Messages In This Thread
Custom validation (callback) for blank field value - by El Forum - 09-02-2008, 06:19 AM
Custom validation (callback) for blank field value - by El Forum - 09-02-2008, 06:59 AM
Custom validation (callback) for blank field value - by El Forum - 09-02-2008, 07:04 AM
Custom validation (callback) for blank field value - by El Forum - 09-02-2008, 07:32 AM
Custom validation (callback) for blank field value - by El Forum - 09-02-2008, 07:46 AM
Custom validation (callback) for blank field value - by El Forum - 09-02-2008, 07:49 AM
Custom validation (callback) for blank field value - by El Forum - 09-02-2008, 07:52 AM
Custom validation (callback) for blank field value - by El Forum - 09-02-2008, 08:07 AM
Custom validation (callback) for blank field value - by El Forum - 09-02-2008, 08:27 AM
Custom validation (callback) for blank field value - by El Forum - 09-02-2008, 09:00 AM
Custom validation (callback) for blank field value - by El Forum - 09-02-2008, 09:03 AM
Custom validation (callback) for blank field value - by El Forum - 10-22-2008, 09:51 AM
Custom validation (callback) for blank field value - by El Forum - 10-22-2008, 11:00 AM
Custom validation (callback) for blank field value - by El Forum - 10-22-2008, 11:54 AM
Custom validation (callback) for blank field value - by El Forum - 10-22-2008, 12:07 PM
Custom validation (callback) for blank field value - by El Forum - 10-22-2008, 12:34 PM
Custom validation (callback) for blank field value - by El Forum - 10-22-2008, 12:39 PM
Custom validation (callback) for blank field value - by El Forum - 03-14-2009, 11:12 PM
Custom validation (callback) for blank field value - by El Forum - 03-15-2009, 12:08 PM



Theme © iAndrew 2016 - Forum software by © MyBB