2011-03-23 82 views
3

說明如何使用CakePHP的模型驗證消息而不是控制器的setFlash消息?

我有我的網站的每個網頁上的一個簡單的形式 - 一個輸入和一個提交按鈕。這一點只是讓用戶輸入他/她的電子郵件地址並點擊提交。

它適用於數據是電子郵件地址 - 但如果它不是一個有效的電子郵件地址,它提交表單,頁面重新加載(或其他),並且閃光消息出現,而不是我的模型更具體的「不有效的電子郵件「錯誤。

問:

那麼,我該如何使用,而不是控制器的一個通用模型的驗證消息?

形式:

echo $this->Form->create('Email', array('class'=>'form1', 'url'=>'/emails/add', 'inputDefaults'=>array('label'=>false))); 
echo $this->Form->input('Email.email'); 
echo $this->Form->end('SIGN-UP'); 

電子郵件控制器 「添加」 功能(或方法):

function add() { 
    if (!empty($this->data)) { 
     $this->Email->create(); 
     if ($this->Email->save($this->data)) { 
      $this->Session->setFlash(__('The email address has been saved', true)); 
      $this->redirect($this->referer()); 
     } else { 
      $this->Session->setFlash(__('The email address could not be saved. Please, try again.', true)); 
      $this->set('error', 'custom error here'); 
      $this->redirect($this->referer()); 
     } 
    } 
} 

和電子郵件型號:

class Email extends AppModel { 
var $name = 'Email'; 

var $validate = array(
    'email'  => array(
     'is_valid' => array(//named whatever we want 
      'rule'  => 'notEmpty', 
       'rule'  => array('email', true), 
       'message' => 'Please supply a valid email address.', 
       'last'  => true //causes it to not check the next rule if this one fails 
     ), 
     'is_unique'  => array(//named whatever we want 
      'rule'   => 'isUnique', 
      'message'  => 'That email address is already in our database.' 
     ) 
    ), 
); 
} 

回答

3

戴夫

CakePHP會這樣對你,你已經正確設置驗證,誤差重定向如果保存失敗。這丟失後的數據,因此驗證消息

只需

if ($this->Email->save($this->data)) { 
    $this->Session->setFlash(__('The email address has been saved', true)); 
    $this->redirect($this->referer()); 
} else { 
    $this->Session->setFlash(__('There were problems saving, please see the messages below.', true));      
} 
+0

對不起,我以前沒有看到。 雖然我不認爲你已經回答了我的問題 - 你的代碼似乎只是吐出「存在一個問題......」而不是模型驗證中的錯誤,這正是我所要求的。謝謝你的回答,如果我誤解了你的回答,請告訴我。 – Dave 2011-03-24 02:04:59

+0

@Dave問題是即使在出現錯誤的情況下也會重定向,這會導致您丟失錯誤消息。如果您不**重定向,則錯誤消息將自動注入到表單中(只要您正確使用表單助手)。驗證消息不會在頁面刷新中持續存在。 – deceze 2011-03-24 04:14:53

+0

ANSWER REWORDED(ty @Leo):Cake將Model驗證的錯誤放在.error-message div中,並且會出現在頁面上,除非您確實像我在我的問題中所做的那樣,即使在驗證失敗時也重定向頁面。 所以 - 任何人發現這個問題 - 只是不重定向頁面,如果它不保存。感謝Leo和YOMorales - 這兩個答案都很好 - 看起來這似乎一定是這樣做的預期方式。 – Dave 2011-03-24 05:04:28

2

通常情況下,您可以使用$ form幫助程序的錯誤選項。請參閱section of the manual on this

但是,如果要使用驗證消息填充Flash消息,可以使用模型方法invalidFields()。以下是一個示例:

if (!$this->User->save($this->data)) { 
    $validationErrors = $this->User->invalidFields(); 
    $this->Session->setFlash($validationErrors['email']); // named key of the rule 
    $this->redirect('/somewhere'); 
} 

這樣做是爲了獲取寫入失敗的驗證規則的模型中的消息。雖然invalidFields()返回一個數組,但您可以操作其中的值以生成更好的錯誤消息,例如連接不同的錯誤消息。

順便說一句,我注意到你的代碼上面有什麼不好。您將模型類命名爲「Email」,並且有一個名爲Email的核心CakePHP類(這是着名的Email組件),所以我建議您爲此命名整個MVC。

+1

謝謝!!如果您將is_unique更改爲「email」(字段,而不是密鑰),我會將其標記爲正確的答案! TYVM! – Dave 2011-03-24 00:09:30

+0

不要粗魯,但這不是你的問題的答案 – Leo 2011-03-24 01:22:11

+0

@Leo - 你不粗魯,但請解釋一下嗎?也許我錯誤地回答了我的問題?感謝YOMorales的回答,我現在使用來自我的模型驗證的錯誤消息,而不是在控制器中輸入某些內容。 – Dave 2011-03-24 02:00:07