Posted by nick on May 5, 2012 at 8:00 am
I was able to get a recaptcha working on my Zend Framwork application, but as soon as I added the “‘ssl’ => true” to my zend_form, the captcha disappeared. Here is the code used in zend_form:
//captcha
$this->addElement('captcha', 'captcha', array(
'label' => 'Please Type:',
'required' => true,
'captcha' => array(
'pubkey' => RECAPTCHA_PUBLIC_KEY,
'privkey' => RECAPTCHA_PRIVATE_KEY,
'captcha' => 'reCaptcha',
'ssl' => true
)
));
The reason ssl didn’t work with recaptcha is because the secure URL was out of date in my zend framework library. To update, open /Zend/Service/ReCaptcha.php and update the server constants at the top to
/**
* URI to the regular API
*
* @var string
*/
const API_SERVER = 'http://www.google.com/recaptcha/api';
/**
* URI to the secure API
*
* @var string
*/
const API_SECURE_SERVER = 'https://www.google.com/recaptcha/api';
/**
* URI to the verify server
*
* @var string
*/
const VERIFY_SERVER = 'http://www.google.com/recaptcha/api/verify';
Continue Reading
Posted by nick on January 12, 2012 at 2:31 pm
I was pleasantly surprised at how simple it was to integrate a good captcha solution with Zend_Form. Your form doesn’t have to extended a special object or anything; you just need to add a “captcha” element like so:
$this->addElement('captcha', 'captcha', array(
'label' => 'Please Type:',
'required' => true,
'captcha' => array(
'pubkey' => RECAPTCHA_PUBLIC_KEY,
'privkey' => RECAPTCHA_PRIVATE_KEY,
'captcha' => 'reCaptcha'
)
));
Be sure to change or set the defined variables for public and private keys provided to you with your recaptcha account. You don’t have to do anything to validate if the user entered the correct letters in your controller as Zend has already taken care of that part for you. As long as you’re using Zend’s isValid() method like so:
if ($form->isValid($request->getPost())) {
Continue Reading
Posted by nick on October 28, 2011 at 9:31 am
If the inline errors from zend validator are not displaying after submitting your form, you may need to add the “Errors” decorator to the form like
//apply decorators to each element.
foreach ($elements as $fieldName => $element) {
$element->setDecorators(array('ViewHelper','Errors',array()));
}
Also be sure the validation is taking place on your form after submission. The controller should look something like this (use the isValid() method)
public function createNewUserAction()
{
$form = new User_Form_CreateUserForm();
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
$params = $form->getValues();
//save the user
$this->view->messages = array('User successfully saved.');
$this->_redirect('user/user-admin');
}
}
$this->view->form = $form;
}
Continue Reading
Posted by nick on July 2, 2011 at 3:07 pm
I came across the following error today while reorganizing my zend framework application:
“Action Helper by name Agent not found”. This error occurred when I tried to use an action helper in a controller file. When it looked for the “redirector” helper, it decided to look for a helper named “_agent” instead.
How did I get here? Previously on this site, I was not using Zend_Auth, so I called php’s session_start() in the index file. I had to remove session_start() because I implemented Zend_Auth in one of the modules today. Apparently, helpers do not work if the session has not been started. Start the session by whichever method you choose and the error along with the missing helper issue will go away.
Continue Reading
Posted by nick on at 1:26 pm
I got to thinking that it didn’t make sense to have a common /forms directory that would share forms between two modules when the forms would not be shared. A /forms directory within the specific module directory seemed more appropriate. The documentation on this isn’t very clear, but after some digging and trial and error I figured it out.
First, the directory structure. Notice the location of the forms directories.
/Application
/modules
/default
/controllers
/forms
/views
/admin
/controllers
/forms
/views
First, add the two below lines to your application.ini file.
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = ""
Second, create a boostrap file for each module. The file, named Bootstrap.php, should be placed in the root of the module directory and the class name should be {module name}_Boostrap. This bootstrap file will cause zend framework to automatically add the new forms directory to the autoloader.
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap {}
Third, add for form class to the /forms directory. A login form would have a filename of Login.php and a class name of {module name}_Form_Login.
class Admin_Form_Login extends Zend_Form
Lastly, call your form from a controller file from within the same module.
$form = new Admin_Form_Login();
Be careful if you’re adding a Bootstrap.php file to the default module! From Zend:
The reason that the default module is skipped is because the documented use case – particularly how we setup an application in Zend_Tool – is that the default module is directly beneath application/ and contains the application bootstrap. If we then allow loading the default module’s bootstrap… we get into a recursive loop.
Continue Reading