我已經閱讀了一些關於此的帖子,但我無法解決我的問題。 當我嘗試驗證我的Zend表單驗證碼時,即使使用正確的文本,它也總是失敗。 這裏是我的代碼:Zend_Form驗證碼不驗證
//這裏我把我的形式
public function contactAction()
{
$this->view->form = new Forms_ContactForm();
}
//我的形式
class Forms_ContactForm extends Twitter_Form
{
public function init()
{
$this->setAction('email/email/contact');
$this->setMethod('post');
$this->addElement('text', 'strFirstName', array(
'required' => true,
'label' => 'Your First Name'));
$this->addElement('text', 'strLastName', array(
'required' => true,
'label' => 'your Last Name'));
$this->addElement('text', 'strEmail', array('validators' => array(
array('EmailAddress')),
'required' => true,
'label' => 'Your Email'));
$this->addElement('textarea', 'strContent', array(
'required' => true,
'label' => 'Your message'));
$this->addElement('captcha', 'captcha', array(
'label' => 'Please enter the 5 letters displayed below:',
'required' => true,
'name' => 'captchaField',
'captcha' => 'image',
'captchaOptions' => array(
'captcha' => 'image',
'font'=> 'static/font/arial.ttf',
'imgDir'=>'static/img/captcha',
'imgUrl'=> 'static/img/captcha/',
'wordLen' => 5,
'fsize'=>20,
'height'=>60,
'width'=>200,
'gcFreq'=>50,
'expiration' => 300)
));
$this->addElement('submit', 'submit', array('class' => 'Submit'));
}
}
//我的行動來發送
public function contactAction()
{
if($this->_request->isPost())
{
$objForm = new Forms_ContactForm();
if($objForm->isValid($_POST))
{
$arrParams = $this->_request->getParams();
if($arrParams['strFirstName'] && $arrParams['strLastName'] && $arrParams['strEmail'] && $arrParams['strContent'])
{
$this->_objEmail = new Zend_Mail();
$this->_objEmail ->setFrom($arrParams['strEmail']);
$this->_objEmail ->setSubject('C');
$this->_objEmail ->setBodyHtml('Message from: '. $arrParams['strFirstName'] . ' ' . $arrParams['strLastName'] .
'<BR>eMail address: ' . $arrParams['strEmail'] . '<BR><BR>'
. $arrParams['strContent']);
$this->_objEmail ->addTo('[email protected]');
$this->view->bolSent = $this->_objEmail->send();
}
}
else
$this->view->form = $objForm;
}
}
看起來,在我的contactAction中,它生成一個新的驗證碼,所以這就是爲什麼它與我所屬的那個匹配但是我不知道如何解決這個問題。
謝謝你的時間和幫助!
剛纔看到的東西不可靠:當我接觸動作傾倒我的$ _ POST這裏是我的結果:
array
'strFirstName' => string 'fghjfghj' (length=8)
'strLastName' => string 'ffffffff' (length=8)
'strEmail' => string '[email protected]' (length=14)
'strContent' => string 'fewfew' (length=6)
'captchaField' => string 'cebfe69ead38dba86a6b557dc8853b24' (length=32)
我剛剛進入dosent甚至會出現,而是我有驗證碼凱驗證碼!! ??
再次編輯
感謝您的重播! 即使有你的改變,仍然不存在,但我認爲我得到了什麼錯誤。 這裏是我的驗證碼現場HTML:
<div class="control-group">
<label class="control-label required" for="captchaField-input">Please enter the 5 letters displayed below:</label>
<div class="controls">
<img width="200" height="60" src="static/img/captcha/ab2d15044a637338064b39cfd2675837.png" alt="">
<input type="hidden" id="captchaField-id" value="ab2d15044a637338064b39cfd2675837" name="captchaField[id]">
<input type="text" value="" id="captchaField-input" name="captchaField[input]">
<input type="text" value="ab2d15044a637338064b39cfd2675837" id="captchaField" name="captchaField">
</div>
</div>
當我看看我的PARAMS發送,這裏是我的了:
captchaField ab2d15044a637338064b39cfd2675837 captchaField [ID] ab2d15044a637338064b39cfd2675837 captchaField [輸入] 6af7u
似乎cptchaField overwright [ID]和[輸入]
我覺得我需要刪除該captchaField但不知道到目前爲止!
我可以做到這一點與JS,但必須有一個乾淨的方式來這樣做!
再次編輯
進出口使用AJAX來提交表單,以連載。這可能是問題,不好看看。
編輯TER
它不是由AJAX造成的。 如果我手動刪除行:
<input type="text" value="ab2d15044a637338064b39cfd2675837" id="captchaField" name="captchaField">
與螢火蟲,一切正常,並驗證碼驗證很好。現在的問題是如何正確刪除此行......
SOLUTION
掙扎了很多之後,這裏是解決方案(刪除裝飾)!
$captcha = new Zend_Form_Element_Captcha('captchaField',
array('label' => "Please enter the 5 letters displayed below:",
'required'=>true,
'captcha' => array(
'captcha' => 'image',
'font'=> 'static/font/arial.ttf',
'imgDir'=>'static/img/captcha',
'imgUrl'=> 'static/img/captcha/',
'wordLen' => 5,
'fsize'=>20,
'height'=>60,
'width'=>200,
'gcFreq'=>50,
'expiration' => 300
)
));
$this->addElement($captcha);
$this->getElement('captchaField')->removeDecorator("viewhelper");
如何在您的驗證碼元素顯示在您的視圖文件? – Haroon
是視圖文件中的代碼還是它是頁面的HTML源代碼? – Haroon
我的查看代碼只是echo $ this-> form 這是源代碼,由zend自動構建, – mokk