OOP表单验证器实现长度规则

时间:2017-03-04 13:13:54

标签: php oop

所以我有一个非常基本的oop表单验证器类。但我很难尝试实现max_length和min_length规则。我的规则像这个rule1 | rule2等工作,所以|在规则之间分开。我试图这样做需要| max_length,60 | min_length,10。 `

public function validate($field,$value,$rules)
   {
     $field = ucfirst($field);
     $rules = explode('|',$rules);
     foreach($rules as $rule)
     {
        switch($rule)
         {
            case 'required':
                $this->shouldBeRequired($field, $value);
                break;
            case 'string':
                $this->shouldBeString($field,$value);
                break;
            case 'email':
                $this->shouldBeEmail($field, $value);
                break;
            case 'number':
                $this->shouldBeNumber($field,$value);
            default:
                throw new ErrorException("$rule doesn't exist");
        }
     }
   }`

我不知道如何添加max_length和min_length case

2 个答案:

答案 0 :(得分:2)

将您的规则拆分为由,分隔的部分:

public function validate($field,$value,$rules)
{
     $field = ucfirst($field);
     $rules = explode('|',$rules);
     foreach($rules as $rule)
     {

       $ruleParts = explode(",", $rule);
        switch($ruleParts[0])
         {
            case 'required':
                $this->shouldBeRequired($field, $value);
                break;
            case 'string':
                $this->shouldBeString($field,$value);
                break;
            case 'email':
                $this->shouldBeEmail($field, $value);
                break;
            case 'number':
                $this->shouldBeNumber($field,$value);
                break; // You forgot this one!
            case 'min-length':
                if (isset($ruleParts[1]) && is_numeric($ruleParts[1]))
                    $this->restrictMinLength($ruleParts[1]);
                else
                    //Parameter forgotten or invalid
                break;
            default:
                throw new ErrorException("$rule doesn't exist");
        }
     }
   }

对于最大长度,它是相同的。

顺便说一下,你忘记了一个break如图所示注释内联

答案 1 :(得分:1)

请注意,您应该实施新的两项功能:shouldBeLessThanshouldBeGreaterThan

public function validate($field,$value,$rules)
{
    $field = ucfirst($field);
    $rules = explode('|',$rules);
    foreach($rules as $rule)
    {
        $rule = explode(',', $rule);
        switch($rule[0])
        {
            case 'required':
                $this->shouldBeRequired($field, $value);
                break;
            case 'string':
                $this->shouldBeString($field,$value);
                break;
            case 'email':
                $this->shouldBeEmail($field, $value);
                break;
            case 'number':
                $this->shouldBeNumber($field,$value);
                break;
            case 'max_length':
                $this->shouldBeLessThan($field, $value, $rule[1]);
                break;
            case 'min_length':
                $this->shouldBeGreaterThan($field, $value, $rule[1]);
                break;
            default:
                throw new ErrorException("$rule doesn't exist");
        }
    }
}