douyong5825 2015-07-11 19:03
浏览 40

扩展错误消息变量

Right now I just have one error message variable that pulls the item name from the form input field that is creating the validation error. So if I only had one letter typed in the 'firstname' input field the error message should show up like this...

firstname must be a minimum of 2 characters.

Which is fine, but I want to create new variables, so that I can put something like

$firstname_validate_error

next to the firstname input field and if that input field is throwing the error for the error to come up next to that field.

Is there a way to do this with the code I have without making this a huge task? I'm not sure how to structure a variable to do this with my validate class.

I am getting my validation errors from this:

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

Is there a way I can structure this so I can put some kind of error variable next to each input field in the form so if that input field is throwing the validation error, the error message comes up?

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;
                }
            }
       }
    }
 }  

My validate class that pertains to this.

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;

My form is like this..

<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条回答 默认 最新

  • doujiu3095 2015-07-12 07:24
    关注

    You might use some magic functions like:

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

    which is obviously not defined. firstname matches to the array of field definitions in your validation class. Catch that with something like

        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);
    
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本