【问题标题】:Validation in Redbean PHPRedbean PHP 中的验证
【发布时间】:2012-05-06 17:33:00
【问题描述】:

我喜欢在 RedbeanPHP 中使用 FUSE 模型进行验证的想法。

我的应用程序有时需要通过多个来源(表单、文件等)接受数据,因此将验证放在实际的类更新上是有意义的。

查看 Redbean 网站上的示例,验证似乎基于抛出异常。

当然,你只能抛出一个异常,所以我假设我需要在我的 FUSE 类中创建一个“数组”类型的附加属性来保存与各个字段关联的验证消息。

有没有人有更好的想法?这是我迄今为止一直在尝试的......

<form action="" method="post">
    <p>your name: <input name="name" type="text"></p>

    <p>your email: <input name="email" type="text"></p>

    <p>your message:</p>
    <textarea name="message" id="" cols="30" rows="10"></textarea>
    <input name="send" value="send message" type="submit">
</form>

<?php

/**
 * @property $name string
 * @property $email string
 * @property $message string
 */
class Model_Comment extends RedBean_SimpleModel{
    public $invalid = array();
    public function update(){
        if(empty($this->name)) $this->invalid['name'] = "field is empty";
        if(empty($this->email)) $this->invalid['email'] = "field is empty";
        if(empty($this->message)) $this->invalid['message'] = "field is empty";
        if(count($this->invalid) > 0) throw new Exception('Validation Failed!');
    }
    public function getInvalid(){
        return $this->invalid;
    }
}

if(isset($_POST['send'])){

    $comment = R::dispense('comment');
    /* @var $comment Model_Comment */
    $comment->import($_POST,'name,email,message');

    try{
        R::store($comment);
    }
    catch(Exception $e){
        echo $e->getMessage();
        $invalid = $comment->getInvalid();
        print_r($invalid);
        exit;
    }
    echo '<p>thank you for leaving a message.</p>';
}
echo "<h2>What people said!</h2>";

$comments = R::find('comment');
/* @var $comments Model_Comment[] */

foreach($comments as $comment){
    echo "<p>{$comment->name}: {$comment->message}</p>";
}

?>

【问题讨论】:

    标签: php validation exception-handling redbean


    【解决方案1】:

    您可以扩展RedBean_SimpleModel 类以向其中添加您自己的方法和字段,因此它适用于您的所有模型。然后,您可以使用事务来管理您的验证。它可能看起来像这样(代码未经测试):

    class RedBean_MyCustomModel extends RedBean_SimpleModel {
      private $errors = array();
      public function error($field, $text) {
        $this->errors[$field] = $text;
      }
      public function getErrors() {
        return $this->errors;
      }
      public function update() {
        $this->errors = array(); // reset the errors array
        R::begin(); // begin transaction before the update
      }
      public function after_update() {
        if (count($this->errors) > 0) {
          R::rollback();
          throw new Exception('Validation failed');
        }
      }
    }
    

    然后,您的模型可能如下所示:

    class Model_Comment extends RedBean_MyCustomModel {
        public function update(){
            parent::update();
            if(empty($this->name)) $this->error('name', 'field is empty');
            if(empty($this->email)) $this->error('name', 'field is empty');
            if(empty($this->message)) $this->error('name', 'field is empty');
        }
        public function getInvalid(){
            return $this->invalid;
        }
    }
    

    【讨论】:

    • 有趣的方法。我想我已经决定扩展异常类了。
    猜你喜欢
    • 1970-01-01
    • 2011-06-07
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多