扩展错误消息变量

时间:2015-07-11 19:03:03

标签: php forms validation

现在我只有一个错误消息变量,它从创建验证错误的表单输入字段中提取项目名称。因此,如果我只在“" firstname'”中输入了一个字母。输入字段错误消息应该显示如下...

firstname必须至少包含2个字符。

哪个好,但我想创建新的变量,这样我就可以添加像

这样的东西
$firstname_validate_error

在firstname输入字段旁边,如果该输入字段引发错误,该错误将出现在该字段旁边。

有没有办法用我的代码执行此操作而不会使这个任务变得艰巨?我不确定如何使用我的验证类来构造变量。

我收到了验证错误:

} else {
                foreach($validation->errors() as $error) {
                    $error;
                }

有没有一种方法可以构建这个,所以我可以在表单中的每个输入字段旁边放置一些错误变量,这样如果该输入字段抛出验证错误,就会出现错误信息?

if(Input::exists()) {
   if(Token::check(Input::get('token'))) {

    if(Input::exists()) {
        $validate = new Validate();
        $validation = $validate->check($_POST, array(
            'firstname' => array(
                'required' => true,
                'min' => 2,
                'max' => 50
            ),
            'lastname' => array(
                'required' => true,
                'min' => 2,
                'max' => 50
            ),
            'email' => array (
                'required' => true,
                'min' => 2,
                'max' => 50
            ),
            'phone_number' => array (
                'required' => true,
                'min' => 7,
                'max' => 15
            ),
            'username' => array(
                'required' => true,
                'min' => 4,
                'max' => 20,
                'unique' => 'users'
            ),
            'password' => array(
                'required' => true,
                'min' => 6
            ),
            'password_again' => array(
                'required' => true,
                'matches' => 'password'
            )

        ));
        if($validation->passed()) {
            $user = new User();

            $salt = Hash::salt(32);

            try {
                $user->create(array(
                    'firstname' => Input::get('firstname'),
                    'lastname' => Input::get('lastname'),
                    'email' => Input::get('email'),
                    'phone_number' => Input::get('phone_number'),
                    'username' => Input::get('username'),
                    'password' => Hash::make(Input::get('password'), $salt),
                    'salt' => $salt,
                    'joined' => date('Y-m-d H:i:s'),
                    'group' => 1
                ));
                $success = "You have successfully created an account. We will notify you once the account has been approved. Then you will be able to login.";
                echo $success;
            } catch(Exception $e) {
                    die($e->getMessage());
                }
            } else {
                foreach($validation->errors() as $error) {
                    $error;
                }
            }
       }
    }
 }  

我的验证类与此有关。

public function check($source, $items = array()) {
    foreach($items as $item => $rules) {
        foreach($rules as $rule => $rule_value) {

            $value = trim($source[$item]);
            $item = escape($item);

            if($rule === 'required' && empty($value)) {
                $this->addError("{$item} is required");
            } else if(!empty($value)) {
                switch($rule) {
                    case 'min':
                        if(strlen($value) < $rule_value) {
                            $this->addError("{$item} must be a minimum of {$rule_value} characters.");
                        }
                    break;
                    case 'max':
                        if(strlen($value) > $rule_value) {
                            $this->addError("{$item} must be a maximum of {$rule_value} characters.");
                        }
                    break;
                    case 'matches':
                        if($value != $source[$rule_value]) {
                            $this->addError("{$rule_value} must match {$item}");
                        }
                    break;
                    case 'unique':
                        $check = $this->_db->get($rule_value, array($item, '=', $value));
                        if($check->count()) {
                            $this->addError("{$item} already exists.");
                        }
                    break;

我的表格是这样的..

<form action="" method="POST">
    <div class="field">
        <label for="firstname">First Name</label>
        <input type="text"  class="inputbar" name="firstname" value="<?php echo escape(Input::get('firstname')); ?>" required>
    </div>
    <div class="field">
        <label for="lastname">Last Name</label>
        <input type="text"  class="inputbar" name="lastname" value="<?php echo escape(Input::get('lastname')); ?>" required>
    </div>

1 个答案:

答案 0 :(得分:0)

你可以使用一些神奇的功能,如:

 $validate = new <classname of your validation class>;
 ....
 $validate->field_firstname();

显然没有定义。 firstname与验证类中的字段定义数组匹配。赶上

之类的东西
    public function __call($key, $params = array(null))
    {
        if ( substr($key, 0, 6) == "field_" )
        {
            // extract field name
            $field = substr($key, 6);

            // validate $field now:
            // ...

            return;
        }

        throw new Exception('Unknown method ' . $key);

    }