示例#1
0
 public function isValid($validation_data)
 {
     $errors = "";
     foreach ($validation_data as $name => $value) {
         $rules = explode("|", $value);
         foreach ($rules as $rule) {
             $exploded = explode(":", $rule);
             switch ($exploded[0]) {
                 case 'min':
                     $min = $exploded[1];
                     if (Valid::alpha()->length($min)->Validate($_POST[$name]) == false) {
                         $errors[] = $name . " must be at least " . $exploded[1] . " characters long!";
                     }
                     break;
                 case 'email':
                     if (Valid::email()->validate($_POST[$name]) == false) {
                         $errors[] = $name . " must be a valid email address!";
                     }
                     break;
                 case 'equalTo':
                     if (Valid::equals($_POST[$name])->validate($_POST[$exploded[1]]) == false) {
                         $errors[] = $name . "'s don't match!";
                     }
                     break;
                 default:
                     $errors[] = "No value found!";
                     break;
             }
         }
     }
     return $errors;
 }
示例#2
0
文件: Valid.php 项目: tourze/base
 /**
  * 检查字符串是否只包含字母
  *
  * @param  string $value
  * @return bool
  */
 public static function alpha($value)
 {
     return Validator::alpha()->validate($value);
 }
/**
 * Validates input type
 *
 * @param string           $hook       "validate:type"
 * @param string           $type       "prototyper"
 * @param ValidationStatus $validation Current validation status
 * @param array            $params     Hook params
 * @return ValidationStatus
 */
function prototyper_validate_type($hook, $type, $validation, $params)
{
    if (!$validation instanceof ValidationStatus) {
        $validation = new ValidationStatus();
    }
    $field = elgg_extract('field', $params);
    if (!$field instanceof Field) {
        return $validation;
    }
    $rule = elgg_extract('rule', $params);
    if ($rule != "type") {
        return $validation;
    }
    $value = elgg_extract('value', $params);
    $expectation = elgg_extract('expectation', $params);
    switch ($expectation) {
        case 'text':
        case 'string':
            if (!v::string()->validate($value)) {
                $validation->setFail(elgg_echo('prototyper:validate:error:type:string', array($field->getLabel())));
            }
            break;
        case 'alnum':
        case 'alphanum':
            if (!v::alnum()->validate($value)) {
                $validation->setFail(elgg_echo('prototyper:validate:error:type:alnum', array($field->getLabel())));
            }
            break;
        case 'alpha':
            if (!v::alpha()->validate($value)) {
                $validation->setFail(elgg_echo('prototyper:validate:error:type:alpha', array($field->getLabel())));
            }
            break;
        case 'number':
        case 'numeric':
            if (!v::numeric()->validate($value)) {
                $validation->setFail(elgg_echo('prototyper:validate:error:type:numeric', array($field->getLabel())));
            }
            break;
        case 'integer':
        case 'int':
            if (!v::int()->validate($value)) {
                $validation->setFail(elgg_echo('prototyper:validate:error:type:int', array($field->getLabel())));
            }
            break;
        case 'date':
            if (!v::date()->validate($value)) {
                $validation->setFail(elgg_echo('prototyper:validate:error:type:date', array($field->getLabel())));
            }
            break;
        case 'url':
            if (!v::filterVar(FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)->validate($value)) {
                $validation->setFail(elgg_echo('prototyper:validate:error:type:url', array($field->getLabel())));
            }
            break;
        case 'email':
            if (!v::filterVar(FILTER_VALIDATE_EMAIL)->validate($value)) {
                $validation->setFail(elgg_echo('prototyper:validate:error:type:email', array($field->getLabel())));
            }
            break;
        case 'guid':
        case 'entity':
            if (!elgg_entity_exists($value)) {
                $validation->setFail(elgg_echo('prototyper:validate:error:type:guid', array($field->getLabel())));
            }
            break;
        case 'image':
            $type = elgg_extract('type', $value);
            if (!$type || substr_count($type, 'image/') == 0) {
                $validation->setFail(elgg_echo('prototyper:validate:error:type:image', array($field->getLabel())));
            }
            break;
    }
    return $validation;
}
 public function testValidationNotExistingParameter()
 {
     $notExistingValidator = v::alpha();
     $validators = array('notExisting' => $notExistingValidator);
     $mw = new Validation($validators);
     $next = function ($req, $res) {
         return $res;
     };
     $response = $mw($this->request, $this->response, $next);
     $errors = array('notExisting' => array('null must contain only letters (a-z)'));
     $this->assertTrue($mw->hasErrors());
     $this->assertEquals($errors, $mw->getErrors());
 }
 /**
  * Verifica se o valor possui apenas caracteres de a-Z e espaços
  * @param string $value
  * @param array $params Lista de flags
  * @return boolean
  */
 public function validAlpha($value, $params = array())
 {
     if (!v::alpha()->validate($value)) {
         if (getValueFromArray($params, Flag::NOWHITESPACE, false)) {
             Factory::log()->warn('Valor deve possuir apenas letras');
         } else {
             Factory::log()->warn('Valor deve possuir apenas letras, números e espaços');
         }
         return false;
     }
     return true;
 }