-- Matthew Ishii <[EMAIL PROTECTED]> wrote
(on Friday, 17 October 2008, 10:40 PM -0700):
> Hello, I have a small issue.
>
> I would like to process the form action with a different controller
> than the one containing the form object.
>
> The problem for me is that I dont see how i can call the form
> validation method on the form object from the other controller.
>
> In other words, I have the main controller, IndexController.php. It
> generates the form object and sets the index view with the form.
>
> I then want the action of that form to call the LoginController's
> method 'login'. I can easily access the forms values via POST object,
> but how can i call the form objects validate method to make sure that
> the filters and validators i specified for the form actually return
> valid?
>From the code snippets you've provided, you're already on the right
track. You define the form as a separate class, and instantiate it when
needed. All you need to do is pass the appropriate method and action to
it when instantiating it -- the filters and validators will be the same
regardless of instance as long as you're not customizing them on a
per-action basis (which you don't appear to be doing anyways).
It's simply a matter of calling $form->isValid(...). It's as easy as
that, really. :)
> The whole reason I would like to split the functionality between the
> two is because when the login and logout methods are contained within
> the IndexController, the logout method has serious problems. It
> totally screws up the browser, it hourglasses and takes forever, only
> to tell me the page cannot be displayed. I dont quite understand why
> this is, so I wanted to attempt to split the functionality over two
> controllers.
>
> Here is the IndexController as it stands:
>
>
> <?php
>
> require_once('Zend/Controller/Action.php');
> require_once('Zend/Auth.php');
> require_once('../database/adapters/AuthAdapter.php');
>
> class IndexController extends Zend_Controller_Action
> {
>
>
> public function indexAction()
> {
> $config = Zend_Registry::get('config');
>
> $this->view->title = $config->english->login->title;
> $this->view->baseurl = $config->production->hosturl;
> $this->view->shead = $config->english->login->subheader;
> $this->view->form = $this->getLoginForm();
>
> $this->_helper->layout->disableLayout();
> }
>
> public function manualAction()
> {
> $this->_helper->layout->disableLayout();
>
> }
>
> public function logoutAction()
> {
> Zend_Auth::getInstance()->clearIdentity();
> return $this->_helper->redirector('index', 'index');
> }
>
> public function loginAction()
> {
> // Retreive Request
> $request = $this->getRequest();
>
> // Redirect to index if request is not POST
> if (!$request->isPost())
> {
> return $this->_helper->redirector('index');
> }
>
> // Get form and validate it
> $form = $this->getLoginForm();
>
> if (!$form->isValid($request->getPOST()))
> {
> // Form is not valid - re-set form and re-render index page
> with filter/validator-set errors
> $this->view->form = $form;
> return $this->render('index');
> }
>
> // Get authentication adapter to check input variables
> $adapter = $this->getAuthAdapter($form->getValues());
> $auth = Zend_Auth::getInstance();
> $result = $auth->authenticate($adapter);
>
> if (!$result->isValid())
> {
> // Check reasons for authentication failure
> switch ($result->getCode()) {
>
> case Zend_Auth_Result::FAILURE:
> // Invalid input variables
> $form->setDescription('Invalid Username or Password');
> $this->view->form = $form;
> break;
>
> case Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS:
> // Identity confirmed : But requires a password update
> $form->setDescription('Access Credentials Expired -
> Contact an Administrator for renewal');
> $this->view->form = $form;
> break;
> }
>
> $this->_helper->layout->disableLayout();
> return $this->render('index');
> }
>
>
> // We're authenticated!
> $this->_helper->redirector('index', 'search');
>
> }
>
> public function getLoginForm()
> {
> return new LoginForm(array('action' => '/index/login', 'method'
> => 'post'));
> }
>
>
> public function getAuthAdapter(array $params)
> {
> return new AuthAdapter($params['username'], $params['password']);
> }
>
> }
>
>
> class LoginForm extends Zend_Form
> {
>
> public function init()
> {
> $this->setAttrib('name', 'login_form');
>
> $username = $this->addElement('text', 'username',
> array('filters' => array('StringTrim', 'StringToLower'),
> 'validators' => array('Alpha',
> array('StringLength', false, array(3, 20))),
> 'required' => true, 'label' =>
> 'Username', 'class' => 's_indentbottom'));
>
>
> $password = $this->addElement('password', 'password',
> array('filters' => array('StringTrim'),
> 'validators' => array('Alnum',
> array('StringLength', false, array(6, 20))),
> 'required' => true, 'label' =>
> 'Password'));
>
> $login = $this->addElement('submit', 'login', array('required'
> => false, 'ignore' => true, 'label' => 'Log In', 'class' =>
> 'login_submit_button'));
>
> $this->setDecorators(array('FormElements', array('HtmlTag',
> array('tag' => 'dl', 'class' => 'Zend_form')),
> array('Description', array('placement' =>
> 'prepend')), 'Form'));
>
> }
>
> }
>
--
Matthew Weier O'Phinney
Software Architect | [EMAIL PROTECTED]
Zend Framework | http://framework.zend.com/