Hello I'm having some strange behaviour with a form containing 3 simple elements (input box, recaptcha element and submit button) that is submitted via an ajax call. If the values of either the input box or the recaptcha are wrong the form is briefly displayed with the error messages before the page goes blank, and the status bar says "transferring data from www.google.com".
Here is the form:
<?php
class My_Form_Subscription_Subscribe extends Zend_Form
{
public function init(){
$this->setMethod('post')
->setLegend('Subscribe')
->setAttribs(array('id' => 'subscribeForm',
'class'
=> 'form')
)
->setAction('/subscription/');
$email = $this->createElement('text', 'email');
$email->setAllowEmpty(false)
->setLabel('Email Address:')
->setRequired(true)
->setAttrib('id', 'subscriptionEmail')
->addValidator(new
Zend_Validate_EmailAddress())
->addFilter(new Zend_Filter_StringTrim())
->addFilter(new Zend_Filter_StripTags());
$captcha = new Zend_Form_Element_Captcha('captcha',
array('label' => 'Enter the following words:',
'captcha' => 'ReCaptcha',
'captchaOptions' => array('captcha' => 'ReCaptcha',
'service' => $this->getRecaptchaService(),
'theme' => 'clean'
)
)
);
$this->addElement($captcha);
$this->addElements(array($email,
$this->createElement('submit', 'submit')->setAttrib('class',
'submit'))
);
$this->addDisplayGroup(array('email', 'captcha',
'submit'), 'subscribe',
array('legend' => 'Subscribe'));
}
protected function getRecaptchaService(){
$config = Zend_Registry::getInstance()->get('config');
$captchaConfig = $config->get('captcha');
return $captchaService = new
Zend_Service_ReCaptcha($captchaConfig->publicKey,
$captchaConfig->privateKey);
}
}
?>
...and here is the controller code:
class SubscriptionController extends Zend_Controller_Action
{
public function init(){
parent::init();
$ajaxContext = $this->getHelper('AjaxContext');
$ajaxContext->addActionContext('index', array('html'))
->addActionContext('remove',
array('html'))
->addActionContext('unsubscribe',
array('html'))
->initContext();
}
public function indexAction(){
$viewMessages = array();
$form = new My_Form_Subscription_Subscribe();
if($this->getRequest()->isPost()){
if($form->isValid($this->getRequest()->getPost())){
$form->reset();
$mapper = new Subscription_Mapper();
/**
* check to see if subscriber already
exists
*/
if($mapper->getSubscriber($this->getRequest()->getPost('email'))){
$viewMessages['error'][] = 'The
email address ' .
$this->getRequest()->getPost('email') . ' is already subscribed.';
}else{
$subscriber = new
Subscription_Model();
$subscriber->setEmail($this->getRequest()->getPost('email'));
try{
$mapper->insert($subscriber);
$viewMessages['ok'][] =
'You are now successfully subscribed!';
}catch(exception $e){
$viewMessages['error'][] = $e->getMessage();
}
}
}else{
$viewMessages['error'][] = 'There was
an error with email address you
entered - you are not yet subscribed!';
}
}
$this->view->assign('messages', $viewMessages);
$this->view->assign('form', $form->render());
}
}
Any ideas on what's causing this?
--
View this message in context:
http://zend-framework-community.634137.n4.nabble.com/strange-form-behaviour-when-using-recpatcha-and-submitting-via-ajax-tp2265373p2265373.html
Sent from the Zend Framework mailing list archive at Nabble.com.
