/**
  * Test for Form::loadRuleType method.
  *
  * @return void
  */
 public function testLoadRuleType()
 {
     // Test error handling.
     $this->assertThat(FormHelper::loadRuleType('bogus'), $this->isFalse(), 'Line:' . __LINE__ . ' Loading an unknown rule should return false.');
     // Test loading a custom rule.
     FormHelper::addRulePath(__DIR__ . '/_testrules');
     $this->assertThat(FormHelper::loadRuleType('custom') instanceof Rule, $this->isTrue(), 'Line:' . __LINE__ . ' Loading a known rule should return a rule object.');
     // Test all the stock rules load.
     $this->assertThat(FormHelper::loadRuleType('boolean') instanceof Rule, $this->isTrue(), 'Line:' . __LINE__ . ' Loading the boolean rule should return a rule object.');
     $this->assertThat(FormHelper::loadRuleType('email') instanceof Rule, $this->isTrue(), 'Line:' . __LINE__ . ' Loading the email rule should return a rule object.');
     $this->assertThat(FormHelper::loadRuleType('equals') instanceof Rule, $this->isTrue(), 'Line:' . __LINE__ . ' Loading the equals rule should return a rule object.');
     $this->assertThat(FormHelper::loadRuleType('options') instanceof Rule, $this->isTrue(), 'Line:' . __LINE__ . ' Loading the options rule should return a rule object.');
     $this->assertThat(FormHelper::loadRuleType('color') instanceof Rule, $this->isTrue(), 'Line:' . __LINE__ . ' Loading the color rule should return a rule object.');
     $this->assertThat(FormHelper::loadRuleType('tel') instanceof Rule, $this->isTrue(), 'Line:' . __LINE__ . ' Loading the tel rule should return a rule object.');
 }
예제 #2
0
 /**
  * Method to validate a Field object based on field data.
  *
  * @param   \SimpleXMLElement  $element  The XML element object representation of the form field.
  * @param   string             $group    The optional dot-separated form group path on which to find the field.
  * @param   mixed              $value    The optional value to use as the default for the field.
  * @param   Registry           $input    An optional Registry object with the entire data set to validate
  *                                       against the entire form.
  *
  * @return  mixed  Boolean true if field value is valid, Exception on failure.
  *
  * @since   1.0
  * @throws  \InvalidArgumentException
  * @throws  \UnexpectedValueException
  */
 protected function validateField(\SimpleXMLElement $element, $group = null, $value = null, Registry $input = null)
 {
     $valid = true;
     // Check if the field is required.
     $required = (string) $element['required'] == 'true' || (string) $element['required'] == 'required';
     if ($required) {
         // If the field is required and the value is empty return an error message.
         if ($value === '' || $value === null) {
             if ($element['label']) {
                 $message = Text::_($element['label']);
             } else {
                 $message = Text::_($element['name']);
             }
             $message = Text::sprintf('JLIB_FORM_VALIDATE_FIELD_REQUIRED', $message);
             return new \RuntimeException($message);
         }
     }
     // Get the field validation rule.
     if ($type = (string) $element['validate']) {
         // Load the Rule object for the field.
         $rule = FormHelper::loadRuleType($type);
         // If the object could not be loaded return an error message.
         if ($rule === false) {
             throw new \UnexpectedValueException(sprintf('%s::validateField() rule `%s` missing.', get_class($this), $type));
         }
         // Run the field validation rule test.
         $valid = $rule->test($element, $value, $group, $input, $this);
         // Check for an error in the validation test.
         if ($valid instanceof \Exception) {
             return $valid;
         }
     }
     // Check if the field is valid.
     if ($valid === false) {
         // Does the field have a defined error message?
         $message = (string) $element['message'];
         if ($message) {
             $message = Text::_($element['message']);
             return new \UnexpectedValueException($message);
         } else {
             $message = Text::_($element['label']);
             $message = Text::sprintf('JLIB_FORM_VALIDATE_FIELD_INVALID', $message);
             return new \UnexpectedValueException($message);
         }
     }
     return true;
 }