Welcome Guest, Not a member yet? Register   Sign In
Multiple Custom validation callbacks only work if using "required" rule?
#1

[eluser]lebe0024[/eluser]
Has anybody experienced this before? For one field, I am using two custom validation callbacks. The trouble is that the second one listed never gets called. CI just assumes it passes. The problem does not occur if "required" precedes them both. Weird.

Code:
function _loadFormValidation()
  {
    // Load the validation library and set the fields and rules.
    $this->load->library('validation');
    $this->fields['ptrId']         = 'PTR ID';
    $this->rules ['ptrId']         = "trim|required|callback_isWord|callback_classifyPTR";
    $this->fields['phase']         = 'Requirement Phase';
    $this->rules ['phase']         = "trim|callback_isWord";
    $this->fields['requirementId'] = 'Requirement ID';
    $this->rules ['requirementId'] = "trim|callback_isValidRequirement";
    $this->validation->set_fields($this->fields);
    $this->validation->set_rules($this->rules);
    $this->validation->set_message('isWord', 'Only letters, numbers, and underscore are allowed.');
    $this->validation->set_message('isValidRequirement', 'That requirement and phase pair does not exist.');
  }

  function isWord($text)
  {
    return (preg_match('/^\w+$/', $text) != 0);
  }

  function classifyPTR($ptrId)
  {
    $ptr = $this->ptrs->getLike($ptrId);

    if ($ptr)
    {
      $this->validPTRMapping->ptrId     = $ptr->id;
      $this->validPTRMapping->validated = true;
    }
    else
    {
      $this->validPTRMapping->ptrId     = $ptrId;
      $this->validPTRMapping->validated = false;
    }

    return true;
  }

  function isValidRequirement($phase)
  {
    // This is because of a bug in CI.  For some reason, if you do not have the 'required' rule, the second
    // callback listed never gets called.
    if (!$this->isWord($phase))
    {
      $this->validation->set_message('isValidRequirement', 'Only letters, numbers, and underscore are allowed.');
      return false;
    }

    if ($this->validation->requirementId or $this->validation->phase)
    {
      $requirement =
        $this->requirements->getLike($this->validation->requirementId,
                                     $this->validation->phase);
      if ($requirement)
      {
        $this->validPTRMapping->requirementId = $requirement->id;
        $this->validPTRMapping->phase         = $requirement->phase;
      }
      else
        return false;
    }
    return true;
  }
#2

[eluser]tonanbarbarian[/eluser]
this seems to be intensional (although I cannot see why but the logic of it may escape me at the moment)

from Validation class line 278:
Code:
// If the field isn't required and we just processed a callback we'll move on...
if ( ! in_array('required', $ex, TRUE) AND $result !== FALSE)
{
  continue 2;
}

so if the field is not required and a callback didnt return false it will stop processing rules
might want some of the developers to explain the logic behind this I guess
#3

[eluser]Derek Allard[/eluser]
I confess that I haven't looked through your code, but this sounds familiar to a change we made with coolfactor's input. Try checking the validation library out of the svn, and let me know if you still experience this.
#4

[eluser]yongkhun[/eluser]
Derek, I have checked out the validation library from the SVN but the problem still remain. The 2nd custom validation will not trigger if the rule does not contain "required".

Please help.




Theme © iAndrew 2016 - Forum software by © MyBB