2012-04-26 24 views
5

RedBeanPHP'de doğrulama için FUSE modellerini kullanma fikrini beğeniyorum.Doğrulama

Uygulamalarım bazen birden fazla kaynak (form, dosya vb.) Yoluyla veri kabul etmeyi gerektiriyor; bu nedenle, gerçek sınıf güncellemesinde doğrulamanın yapılması mantıklı.

Redbean sitesindeki örneklere bakıldığında, doğrulama bir istisna atmaya dayanıyor gibi görünüyor.

Tabi ki, yalnızca bir istisna atabilirsiniz, bu yüzden tek tek alanlar ile ilişkili doğrulama mesajlarını saklamak için FUSE sınıfımda "array" tipi ek bir özellik oluşturmanız gerektiğini varsayıyorum.

Daha iyi fikirleri olan var mı? İşte Bunu kendi yöntem ve alanları eklemek RedBean_SimpleModel sınıfını uzatabilirsiniz ... Bugüne kadar

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

?> 

cevap

10

çalışıyorum, bu nedenle tüm modeliyle çalışacak budur. Daha sonra, doğrulamalarınızı yönetmek için işlemleri kullanabilirsiniz. Bu (değil test kodu) gibi görünebilir:

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

Ardından, modeliniz bu gibi görünebilir:

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

İlginç bir yaklaşım. Yine de istisna sınıfını genişletmeye karar verdim. – christian

İlgili konular