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 987979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:34:14+00:00 2026-05-16T05:34:14+00:00

Is there any good solution for the following requirements: A form with one field

  • 0

Is there any good solution for the following requirements:

  • A form with one field for zip code and default validators like number, length, etc.
  • After submission, the form is checked against a database.
  • If the zip code is not unique we have to ask for an city.

Examples:

Case 1: Submited zip code is unique in database. Everything is okay. Process form

Case 2: Submited zip code is not unique. Add a second field for city to the form. Go back to form.

We want to handle this in an generic way (not inside an controller). We need this logic for
a lot of forms. First thought was to add it to isValid() to every form or write a
validator with logic to add fields to the form. Subforms are not possible for us, because we need this for different fields (e.g. name and street).

  • 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-16T05:34:15+00:00Added an answer on May 16, 2026 at 5:34 am

    Currently I’m using isValid method inside my forms for an User Form to verify the password and confirm password field. Also, when the form is displayed in a New Action, there are no modifications, but when displayed in an Edit Action, a new field is added to the form.

    I think that is a good option work on the isValid method and add the field when the validation return false, and if you want something more maintainable, you should write your own validatator for that purpose.

    Take a look at my code:

    class Admin_Form_User extends Zf_Form 
    {
        public function __construct($options = NULL)
        {
            parent::__construct($options);
            $this->setName('user');
    
            $id = new Zend_Form_Element_Hidden('id');
    
            $user = new Zend_Form_Element_Text('user');
            $user->setLabel('User:')
            ->addFilter('stripTags')
            ->addFilter('StringTrim')
            ->setAllowEmpty(false)
            ->setRequired(true);
    
            $passwordChange = new Zend_Form_Element_Radio('changePassword');
            $passwordChange->setLabel('Would you like to change the password?')
            ->addMultiOptions(array(1 => 'Sim', 2 => 'Não'))
            ->setValue(2)
            ->setSeparator('');
    
            $password = new Zend_Form_Element_Password('password');
            $password->setLabel('Password:')
            ->addFilter('stripTags')
            ->addFilter('StringTrim')
            ->setRequired(true);
    
            $confirm_password = new Zend_Form_Element_Password('confirm_password');
            $confirm_password->setLabel('Confirm the password:')
            ->addFilter('stripTags')
            ->addFilter('StringTrim')
            ->addValidator('Identical')
            ->setRequired(true);
    
            $submit = new Zend_Form_Element_Submit('submit');
            $submit->setLabel('Save');
    
            $this->addElements(array($id,$name,$lastname,$group,$user,$passwordChange,$password,$confirm_password,$submit));
    
            $this->addDisplayGroup(array('password','confirm_password'),'passwordGroup');
            $this->submit->setOrder(8);
    
            $this->setDisplayGroupDecorators(array(
                'FormElements',
                array('HtmlTag', array('tag' => 'div','id' => 'div-password'))
                )
            );
    
            $passwordChange->clearDecorators();
    
        }
    
        public function addPasswordOption()
        {
            $this->changePassword->loadDefaultDecorators();
    
            $this->getDisplayGroup('passwordGroup')
            ->addDecorators(array(
                array('HtmlTag', array('tag' => 'div','id' => 'div-password'))
                )
            );
    
            $this->password->setRequired(false);
            $this->confirm_password->setRequired(false);
        }
    
        public function setPasswordRequired($flag = true)
        {
            $this->password->setRequired($flag);
            $this->confirm_password->setRequired($flag);
        }
    
        public function isValid($data)
        {
            $confirm = $this->getElement('confirm_password');
            $confirm->getValidator('Identical')->setToken($data['password']);
            return parent::isValid($data);
        }
    
    }
    

    So, in my controller:

    public function newAction()
        {
            $this->view->title = "New user";
            $this->view->headTitle($this->view->title, 'PREPEND');
    
            $form = $this->getForm();
    
            if($this->getRequest()->isPost()) 
            {
                $formData = $this->_request->getPost();
    
                if($form->isValid($formData))
                {
                    $Model = $this->getModel();
                    $id = $Model->insert($formData);
    
                    $this->_helper->flashMessenger('The user data has beed updated.');
                    $this->_helper->redirector('list');
                }
            }
    
            $this->view->form = $form;
        }
    
        public function editAction()
        {       
            $this->view->title = "Edit user";
            $this->view->headTitle($this->view->title, 'PREPEND');
    
            $id = $this->getRequest()->getParam('id');
    
            $form = $this->getForm();
    
            // Add yes or no password change option
            $form->addPasswordOption();
    
            $Model = $this->getModel();
    
            if($this->getRequest()->isPost()) 
            {
                $formData = $this->getRequest()->getPost();
    
                // Change password?
                if($formData['changePassword'] == 2) $form->setPasswordRequired(false);
    
                if($form->isValid($formData)) 
                {
                    $Model->update($formData);
    
                    $this->_helper->flashMessenger('The user data has beed updated.');
                    $this->_helper->redirector('list');
                } 
    
            }
    
            $data = $Model->getById($id)->toArray();
    
            $form->populate($data);
    
            $this->view->form = $form;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any good documentation on implementing new refactorings in Eclipse? Specifically, I'd like
Is there any good tutorial or code snippet out there on how to use
Are there any good reasons for why you would include JavaScript like this: <script
Are there any good technical solutions for extremely long term archiving of data, for
Are there any good examples of mvc routing wherein every 404 page not found
Are there any good open source frameworks for developing computer system emulators? I am
Is there any good way of truncating text with plain HTML and CSS, so
Are there any good books on the subject worth reading and still up-to-date with
Is there any good practice for this? I wish I could solve the problem
Are there any good resources for learning C++ that a C# user could use,

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.