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;
  }


Messages In This Thread
Multiple Custom validation callbacks only work if using "required" rule? - by El Forum - 12-01-2007, 03:01 AM



Theme © iAndrew 2016 - Forum software by © MyBB