예제 #1
0
 /**
  * Test isEmptyAllowed method
  *
  * @return void
  */
 public function testIsEmptyAllowed()
 {
     $def = array('rule' => 'aRule', 'allowEmpty' => true);
     $Rule = new CakeValidationRule($def);
     $this->assertTrue($Rule->isEmptyAllowed());
     $def = array('rule' => 'aRule', 'allowEmpty' => false);
     $Rule = new CakeValidationRule($def);
     $this->assertFalse($Rule->isEmptyAllowed());
     $def = array('rule' => 'notEmpty', 'allowEmpty' => false, 'on' => 'update');
     $Rule = new CakeValidationRule($def);
     $this->assertTrue($Rule->isEmptyAllowed());
     $Rule->isUpdate(true);
     $this->assertFalse($Rule->isEmptyAllowed());
     $def = array('rule' => 'notEmpty', 'allowEmpty' => false, 'on' => 'create');
     $Rule = new CakeValidationRule($def);
     $this->assertFalse($Rule->isEmptyAllowed());
     $Rule->isUpdate(true);
     $this->assertTrue($Rule->isEmptyAllowed());
 }
 /**
  * Fetches the correct error message for a failed validation
  *
  * @param string $name the name of the rule as it was configured
  * @param CakeValidationRule $rule the object containing validation information
  * @return string
  */
 protected function _processValidationResponse($name, $rule)
 {
     $message = $rule->getValidationResult();
     if (is_string($message)) {
         return $message;
     }
     $message = $rule->message;
     if ($message !== null) {
         $args = null;
         if (is_array($message)) {
             $result = $message[0];
             $args = array_slice($message, 1);
         } else {
             $result = $message;
         }
         if (is_array($rule->rule) && $args === null) {
             $args = array_slice($rule->rule, 1);
         }
         $args = $this->_translateArgs($args);
         $message = __d($this->_validationDomain, $result, $args);
     } elseif (is_string($name)) {
         if (is_array($rule->rule)) {
             $args = array_slice($rule->rule, 1);
             $args = $this->_translateArgs($args);
             $message = __d($this->_validationDomain, $name, $args);
         } else {
             $message = __d($this->_validationDomain, $name);
         }
     } else {
         $message = __d('cake', 'This field cannot be left blank');
     }
     return $message;
 }
 /**
  * Test isEmptyAllowed method
  *
  * @return void
  */
 public function testIsEmptyAllowed()
 {
     $def = array('rule' => 'aRule', 'allowEmpty' => TRUE);
     $Rule = new CakeValidationRule($def);
     $this->assertTrue($Rule->isEmptyAllowed());
     $def = array('rule' => 'aRule', 'allowEmpty' => FALSE);
     $Rule = new CakeValidationRule($def);
     $this->assertFalse($Rule->isEmptyAllowed());
     $def = array('rule' => 'notBlank', 'allowEmpty' => FALSE, 'on' => 'update');
     $Rule = new CakeValidationRule($def);
     $this->assertTrue($Rule->isEmptyAllowed());
     $Rule->isUpdate(TRUE);
     $this->assertFalse($Rule->isEmptyAllowed());
     $def = array('rule' => 'notBlank', 'allowEmpty' => FALSE, 'on' => 'create');
     $Rule = new CakeValidationRule($def);
     $this->assertFalse($Rule->isEmptyAllowed());
     $Rule->isUpdate(TRUE);
     $this->assertTrue($Rule->isEmptyAllowed());
 }