【问题标题】:kohana ORM user model can't validate other users rows, for example Name, Last Name etckohana ORM 用户模型无法验证其他用户行,例如姓名、姓氏等
【发布时间】:2012-11-09 10:38:32
【问题描述】:

在哪些方面进行了如此检查?我不想检查密码字段的兼容性重复字段。

我在 Kohana 用户控制器中的添加功能

public function action_add() {
        $title = 'Add User';
        $this->template->title = $title;
        $this->template->content = View::factory('action/users/add')
                ->bind('title', $title)
                ->bind('message', $message)
                ->bind('errors', $errors);

        if (HTTP_Request::POST == $this->request->method()) {
            try {

                // Create the user using form values
                $user = ORM::factory('user')->create_user($this->request->post(), array(
                    'username',
                    'password',
                    'email',
                    'last_name',
                    'first_name',
                    'middle_name'
                        ));

                // Grant user login role
                $user->add('roles', ORM::factory('role', array('name' => 'login')));

                // Reset values so form is not sticky
                $_POST = array();

                Session::instance()->set('message', "You have added user '{$user->username}' to the database");
                Request::current()->redirect('/admin/' . $this->request->param('lang') . '/action/users' );
            } catch (ORM_Validation_Exception $e) {

                // Set failure message
                $message = 'There were errors, please see form below.';

                // Set errors using custom messages
                $errors = $e->errors('models');
            }
        }
    }

【问题讨论】:

    标签: validation kohana admin add


    【解决方案1】:

    我认为你可以直接使用 ORM::create() 方法,

    http://kohanaframework.org/3.3/guide-api/Model_Auth_User#create_user

    ORM::create_user() 所做的是验证密码,这正是您想要跳过的内容

    http://kohanaframework.org/3.3/guide-api/Model_Auth_User#create_user

    所以你应该使用类似的东西

      $user = ORM::factory('user')->values($this->request->post(), array(
                    'username',
                    'password',
                    'email',
                    'last_name',
                    'first_name',
                    'middle_name'
                        ))->create();
    

    避免调用create_user,您也将避免密码长度验证,因此通常您会在模型中添加此规则,但由于密码获取哈希您应该在控制器处理它,我看到您没有使用任何验证或过滤 $POST 数据,我建议你使用 Validator 对象

    http://kohanaframework.org/3.3/guide-api/Validation

    你可以限制密码长度

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      • 2016-02-22
      相关资源
      最近更新 更多