我已经阅读了很多关于此的帖子,但我无法解决我的问题。 当我尝试验证我的zend表单验证码时,即使使用正确的文本也总是失败。 这是我的代码:
//我在哪里打电话给我的表格
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( 'mail@gmail.com' );
$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 'fvhkbno@biu.fr' (length=14)
'strContent' => string 'fewfew' (length=6)
'captchaField' => string 'cebfe69ead38dba86a6b557dc8853b24' (length=32)
我刚刚输入的验证码甚至出现了,而我有验证码kay !! ??
修改
再次感谢您的重播! 即使你的改变仍然没有,但我认为我错了。 这是我的验证码字段的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重叠[id]和[输入]
我觉得我需要删除这个captchaField,但不知道到目前为止!
我可以用JS做到这一点,但必须有一个干净的方法来做到这一点!
再次编辑
我使用ajax提交表单,并使用序列化。这可能是问题,看看有点不对劲。
编辑TER
它不是由ajax引起的。 如果我手动删除该行:
<input type="text" value="ab2d15044a637338064b39cfd2675837" id="captchaField" name="captchaField">
使用firebug,一切正常,验证码验证得很好。现在的问题是如何正确删除这一行......
解
经过多次努力之后,这里有解决方案(删除装饰器)!
$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");
答案 0 :(得分:4)
经过艰苦的努力,这里有解决方案(我只需要删除装饰器)!
$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的帮助和时间:)
答案 1 :(得分:1)
我认为您的代码需要如下所示,我认为在contactAction.php文件中,public function contactAction()
应该处理表单发布时的表单和操作:
//Display the form
$objForm = new Forms_ContactForm();
$this->view->form = $objForm;
//Handle the form, when it has been posted including validation etc
if( $this->_request->isPost() )
{
if( $objForm->isValid($_POST) )
{
//processing logic etc
}
}
$objForm = new Forms_ContactForm();
$this->view->form = $objForm;
//Handle the form, when it has been posted including validation etc
if( $this->_request->isPost() )
{
if( $objForm->isValid($_POST) )
{
//processing logic etc
}
}
目前,您的代码正在生成一个新的Captcha来确认数据输入,因为您在表单发布后将实例化表单。这个表单的实例需要在表单发布之前完成,正如我在上面的代码示例中所示。
修改强>
试试这个:
$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);
编辑后的这段新代码对我来说使用Zend Framework 1.11.1版
你在唱什么版本的Zend Framework?你的视图文件中是如何显示Captcha的?
当var转储我们的输出时,对于验证码,您应该期望类似于下面的内容,其中“5g4ef”是您输入到Captcha输入元素的数据:
$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);