ZendFramework zend_form登录验证码

Standard
<?php
class LoginForm extends Zend_Form
{
    public function init()
    {
        $username = $this->addElement('text', 'username', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
                'Alpha',
                array('StringLength', false, array(3, 20)),
            ),
            'required'   => true,
            'label'      => '用户名:',
        ));
 
        $password = $this->addElement('password', 'password', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                'Alnum',
                array('StringLength', false, array(6, 20)),
            ),
            'required'   => true,
            'label'      => '密 码:',
        ));
 
        $captcha = $this->addElement('captcha', 'captcha', array(
            'captcha' =>  array(  
                // First the type...  
                'captcha' => 'Image',
                // Length of the word...  
                'wordLen' => 6,
                'fontsize'=>16,
                'width' => 100,
                'height' => 38,
                'dotNoiseLevel'=>2,
                // Captcha timeout, 5 mins  
                'timeout' => 300,  
                // What font to use...  
                'font' => $_SERVER["DOCUMENT_ROOT"].'/webapp/views/scripts/arial.ttf',  
                // Where to put the image  
                'imgDir' => $_SERVER["DOCUMENT_ROOT"].'/webroot/images/',  
                // URL to the images  
                // This was bogus, here's how it should be... Sorry again :S  
                'imgUrl' => 'http://yemaosheng.com/webroot/images/',
            ),
            'label' => '验证码:'
        ));
 
        $login = $this->addElement('submit', 'login', array(
            'required' => false,
            'ignore'   => true,
            'label'    => '登录',
        ));
 
        // We want to display a 'failed authentication' message if necessary;
        // we'll do that with the form 'description', so we need to add that
        // decorator.
        $this->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')),
            array('Description', array('placement' => 'prepend')),
            'Form'
        ));
    }
}
?>
<?php
public function loginAction() {
 
	$request = $this->getRequest();
 
        if (!$request->isPost()) {
            return $this->_helper->redirector('index');
        }
 
	/* 用$form->isValid的话这一段可以不用
	// Get out from the $_POST array the captcha part...
	$captcha = $request->getPost('captcha');
	// Actually it's an array, so both the ID and the submitted word
	// is in it with the corresponding keys
	// So here's the ID...
	$captchaId = $captcha['id'];
	// And here's the user submitted word...
	$captchaInput = $captcha['input'];
	// We are accessing the session with the corresponding namespace
	// Try overwriting this, hah!
	$captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_'.$captchaId);
	// To access what's inside the session, we need the Iterator
	// So we get one...
	$captchaIterator = $captchaSession->getIterator();
	// And here's the correct word which is on the image...
	$captchaWord = $captchaIterator['word'];
	// Now just compare them...
	if($captchaInput != $captchaWord){
		echo "验证码Error";
		exit;
	} 
	*/
 
        $form = $this->getForm();
        if (!$form->isValid($request->getPost())) {
            // Invalid entries
            $this->view->form = $form;
            return $this->render('index'); // re-render the login form
        }
}
public function getForm()
{
	return new LoginForm(array(
            'action' => '/user/login',
            'method' => 'post',
	));
}
?>

One thought on “ZendFramework zend_form登录验证码

  1. Jenna

    Pretty good post. I just came by your site and wanted to say
    that I have really liked reading your posts. Any way
    I’ll be subscribing to your blog and I hope you post again soon!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.