如何验证 ORM 中的给定数据?

发布于 2024-11-19 17:28:08 字数 1958 浏览 0 评论 0原文

Kohana 的 ORM 内置了 Kohana 的验证。

据我了解,它验证添加到的字段 数据库。它对我不起作用,因为我需要验证字段 来自 $_POST (简单来说)。

让我举个例子。

在控制器中:

$data = Arr::extract($this->request->post(), array('username', 'password', 'password_repeatedly', 'email'));

try {

   ORM::factory('User')->sign_up($data);

   $this->request->redirect('sign-in');

} catch(ORM_Validation_Exception $exception) {

   $errors = $exception->errors('error_messages');

   echo 'There were errors:<br />';
   echo Debug::dump($errors);

   exit;

}

变量 $data 是我需要验证的数组。方法 sign_up() 是 只是我的 ORM 模型中将创建用户的自定义方法。抱歉 控制器中的“echo'es”和“exit's” - 我只是在调试...

我的 ORM 模型如下所示:

public function rules() {

   return array(
           'username' => array(
                   array('not_empty')
           ),
           'hashed_password' => array(
                   array('not_empty')
           ),
           'email' => array(
                   array('not_empty')
           )
   );

}

public function sign_up($post) {

   $salt            = $this->_hurricane->generate_salt();
   $hashed_password =
   $this->_hurricane->hash_password($post['password'], $salt);

   $this->username        = $post['username'];
   $this->hashed_password = $hashed_password;
   $this->salt            = $salt;
   $this->email           = $post['email'];

   $this->save();

}

我想检查变量 $data 的这三个元素是否是 空!正如我所说,它在 ORM::save() 之前检查元素 叫。如果你仔细看看我的代码......在我的自定义方法中我 已将 hashed_pa​​ssword 设置为某种内容。它将对其进行哈希处理。 问题是,如果用户没有提交任何密码(我称之为 我的 HTML 表单中包含“password”字段,但以下字段中包含“hashed_pa​​ssword”字段 数据库)...如果没有提交密码 - 它将散列空字符串 无论如何,这都会导致哈希。这样 hashed_pa​​ssword 就设置好了!

然后通过 ORM::save() 打开验证并得出结论 - 密码永远不可能为空!这该如何处理呢?额外的 控制器中的验证?你会如何处理?也许有一点 逻辑有点不同?

PS 对我的代码的任何其他建议将不胜感激。谢谢指教!

Kohana's ORM comes with built in Kohana's Validation.

As much as I understood, it validates fields that will be added to
the database. It won't work for me because I need to validate fields
that come from $_POST (in simple speaking).

Let me give you an example.

In controller:

$data = Arr::extract($this->request->post(), array('username', 'password', 'password_repeatedly', 'email'));

try {

   ORM::factory('User')->sign_up($data);

   $this->request->redirect('sign-in');

} catch(ORM_Validation_Exception $exception) {

   $errors = $exception->errors('error_messages');

   echo 'There were errors:<br />';
   echo Debug::dump($errors);

   exit;

}

Variable $data is array I need to validate. Method sign_up() is
just custom method in my ORM model that will create user. Sorry about
"echo'es" and "exit's" in controller - I'm just debugging...

My ORM model looks like this:

public function rules() {

   return array(
           'username' => array(
                   array('not_empty')
           ),
           'hashed_password' => array(
                   array('not_empty')
           ),
           'email' => array(
                   array('not_empty')
           )
   );

}

public function sign_up($post) {

   $salt            = $this->_hurricane->generate_salt();
   $hashed_password =
   $this->_hurricane->hash_password($post['password'], $salt);

   $this->username        = $post['username'];
   $this->hashed_password = $hashed_password;
   $this->salt            = $salt;
   $this->email           = $post['email'];

   $this->save();

}

I want to check that those three elements of variable $data are
NOT empty! As I said, it checks elements before ORM::save() is
called. And if ypu look closer at my code... in my custom method I
have set hashed_password to be something. It will make it hashed.
Problem is that if user haven't submitted any password (I call that
field 'password' in my HTML form, but 'hashed_password' in
database)... if no password is submitted - it will hash empty string
that will lead to hash anyway. So hashed_password is set!

Then validation is turned on by ORM::save() and in conclusion -
password never can be possibly empty! How to deal with this? Extra
validation in controller? How would you deal with it? Maybe a little
bit different logic?

P.S. Any other suggestions to my code will be appreciated. Thanks in advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼波传意 2024-11-26 17:28:08

我不明白你当前的方法有什么“问题”。

您可以添加一个条件(Model_user::signup())来检查请求的密码在散列之前是否为空(ofc,如果是则根本不设置),因此它将保持为空并使验证失败。

我在这里还可以注意到的一件事是,注册方法本身是不明确的,可以使用普通的 create() 结合密码过滤器轻松完成(以便在更改时设置 hashed_pa​​ssword 和 salt)。

恕我直言,根据当前对象的状态使用条件规则/过滤器也是一个很好的做法。

I don't see what is 'wrong' with your current method.

You can add a condition (Model_user::signup()) to check if the requested password is empty before hashing it (ofc, not setting it at all if it is), so it'll remain empty and make validation fail.

One more thing I can notice here is that the signup method itself is ambiguous, it could easily be done using normal create() combined with a filter for password (so that hashed_password and salt are set when it's changed).

Imho it's also a good practice to use conditional rules / filters, depending on the current objects' state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文