Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7030317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:39:17+00:00 2026-05-28T00:39:17+00:00

I’ve been having problems created modular reusable components in my Zend Framework app. In

  • 0

I’ve been having problems created modular reusable components in my Zend Framework app. In this case I’m not referring to Zend Framework modules but rather the ability to have a reusable MVC widgety thing if you like. The problems I’m having may be very particular to my implementation, but I’m completely happy to throw it out and start again if someone can point me in the right direction. Anyway, specifics and code will hopefully explain things better and even if what I’m doing is not the best way it should show what I’m trying to achieve:

A simple example is a Mailing List sign up form. I want to include this on several pages of the site which use different Controllers and this presents a few problems in how to process the data and return relevant messages. I don’t want to do either of the following as they really smell:

  1. Create a base controller with the form processing in and extend (Bad)
  2. Duplicate form processing code in relevant controllers (Even worse!)

The clean way to go feels to me to create a new Controller to process the mailing list form data, use a View Helper to easily output the form and relevant markup into the desired pages and then redirect back to the page where signup occurred once the form has been processed. However, I’d like to use the form validation provided by Zend_Form, which means I’d need to pass the form object back to the view helper somehow if validation fails but in the same request. I’m currently doing this by setting it as a variable on the view and then forwarding back to the previous page rather than redirecting, which is ok(ish). If validation is ok then I’d prefer to use a redirect back to the original page. I’m having trouble doing this though as I’d like to pass messages back to the component about the state of signup. Normally I’d use the FlashMessenger Action Helper, I could namespace it in this case so messages didn’t clash with other page data, but I can’t access it from within a View Helper. So currently I’m forwarding in this case too. I’d much prefer a redirect to prevent form resubmissions if a user refreshes the page and to keep the URL clean. I realise I essentially want to have a mini MVC dispatch process within a page and I think that’s what the action stack is for? I really don’t know much about this though and any pointers would be greatly appreciated. Here’s my current code:

Controller:

<?php
class MailingListController extends Zend_Controller_Action {

    public function insertAction() {
        $request = $this->getRequest();
        $returnTo = $request->getParam('return_to');

        if(!$request->isPost() || (!isset($returnTo) || empty($returnTo))) {
            $this->_redirect('/');
        }

        $mailingList = new Model_MailingList();
        $form = new Form_MailingList();

        $returnTo = explode('/', $returnTo);

        if($form->isValid($_POST)) {
            $emailAddress = $form->getValue('email_address');

            $mailingList->addEmailAddress($emailAddress);

            $this->view->mailingListMessages = $mailingList->getMessages();
            $this->view->mailingListForm = "";
        }
        else {
            $this->view->mailingListForm = $form;
        }

        $this->_forward($returnTo[2], $returnTo[1], $returnTo[0]);
    }
}

return_to is a string containing the current URI (module/controller/action), which is generated in the View Helper. I’d prefer to redirect inside the $form->isValid($_POST) block.

View Helper:

<?php
class Zend_View_Helper_MailingList extends Zend_View_Helper_Abstract {

    public function mailingList($form, $messages = "") {
        if(!isset($form)) {
            $request = Zend_Controller_Front::getInstance()->getRequest();
            $currentPage = $request->getModuleName() . '/' . $request->getControllerName() . '/' . $request->getActionName();

            $form = new Form_MailingList();
            $form->setAction('/mailing-list/insert');
            $form->setCurrentPage($currentPage);
        }

        $html = '<div class="mailingList"><h2>Join Our Mailing List</h2>' . $form;
        $html .= $messages;
        $html .= '</div>';

        return $html;
    }

}

Getting an instance of the Front Controller in the View Helper isn’t ideal but I’d prefer to encapsulate as much as possible.

If I have a form object where validation has failed I can pass it back into the helper to output with error messages. If I have some messages to render I can also pass them into the helper.

In my view scripts I’m using the helper like so:

<?=$this->mailingList($this->mailingListForm, $this->mailingListMessages);?>

If neither mailingListForm or mailingListMessages has been set on the view by MailingListController, it will output a new form with no messages.

Any help is greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:39:17+00:00Added an answer on May 28, 2026 at 12:39 am

    Using ajax seems to be an optimal way. View Action Helper is used only for the first load of the mailing form.

    Controller

    class MailingListController extends Zend_Controller_Action {
    
    public function insertAction() {
        $request = $this->getRequest();
    
        $form = new Form_MailingList();
    
        if ($request->isPost()) {
            if ($form->isValid($request->getPost())) {
                $mailingList = new Model_MailingList();
                $emailAddress = $form->getValue('email_address');
                $mailingList->addEmailAddress($emailAddress);
                $form = $mailingList->getMessages();
            }
        }
    
        $this->view->form = $form;
    }
    
    }
    

    view script insert.phtml

    <?php echo $this->form; ?>
    

    Form class

    class Form_MailingList extends Zend_Form {
    
    public function init() {
        //among other things
        $this->setAttrib('id', 'mailing-list-form');
        $this->setAction('/mailing-list/insert');
    }
    
    }
    

    View Helper

    class Zend_View_Helper_MailingList extends Zend_View_Helper_Abstract {
    
    public function mailingList() {
        $this->view->headScript()->appendFile('/js/mailing-list.js');
        return '<div id="mailing-list-wrap">' . $this->view->action('insert', 'mailing-list') . '</div>';
    }
    
    }
    

    JS file mailing-list.js

    $(document).ready(function() {
        $('#mailing-list-form').submit(function() {
            var formAction = $(this).attr('action');
            var formData = $(this).serialize();
    
            $.post(formAction, formData, function(data) {
                //response going in form's parent container
                $(this).parent().html(data);
            });
    
            return false;
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.