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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:55:29+00:00 2026-05-27T03:55:29+00:00

So I have a form set up in the following manner: In my forms

  • 0

So I have a form set up in the following manner:

In my forms directory:

Address.php

class Address extends Zend_Form{
    // Creates an address input box including address/country/state/zip
    // The states is created as a drop down menu 
    public function init() {
         // relevant code to question
         $this->addElements(array(
            array('select', $names['state'], array(
                'label'        => "State",
                'class'        => 'state',
                'multiOptions' => array('' => '') + AddressHelper::stateList(),
                'required'     => $this->_required,
            )),
         ));
    }
}

MyForm.php:

class MyForm extends Zend_Form {
  public function init() {
    //set-up some general form info 

    // this is the relevant part for my question
    // $opt is a predefined variable
    $this->addSubForms(array(
            'info' => new SubForm($opts),
            'mailing' => new Address($opts + array(
                'legend' => 'Address', 
                'isArray' => false, 
                'required' => true,
        )),
    ));  
   }
}

Survey.php

class Survey extends MyForm{
    // initialize parent (MyForm) and add additional info for the Survey form
}

Okay, so when survey is submitted, if it fails validation, I need to change the Address state element from a select to an input type=text.

So in my controller, under the action that checks for validation I have the following:

public function createAction(){
     if ($this->_form->isValid($post)) {
        $this->_saveDraft($post, $this->_submissionType);
        $this->addSessionMessage('Submission created!');
        return $this->redirector->gotoRouteAndExit(array(), 'home', true);
     }else{
        /* IMPORTANT */
        // I need to change the Address select field to a text field here!

        $errors[] = 'There was a problem';
        $this->view->assign(compact('form', 'errors', 'submission'));
        $this->_viewRenderer->renderScript('update.phtml');
     }
}

So, would I just create a method in the Address class and somehow call it to swap out. I’m just not sure how to go about this.

  • 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-27T03:55:30+00:00Added an answer on May 27, 2026 at 3:55 am

    You would be looking at using removeElement() to remove the select element, and then addElement() to replace it with the text only version.

    The problem you are going to have is that when the validation fails, the select element is changed to a text element and the form is re-displayed. Now, upon resubmission, you need to make the change again prior to calling isValid() because the form uses text input for state instead of select. So you need to make the change twice. Once after failed validation prior to re-displaying the form, and once prior to calling isValid(), but only if there was a previously failed submission.

    Now why is it that if the form fails validation, you want the select element for state to be text? Can’t it work just the same with a select element and you just pre-select the correct state for them?

    EDIT:

    You use the form object to call add/removeElement.

    $removed = $form->getSubForm('mailing')->removeElement('state_select');
    $form->getSubForm('mailing')->addElement($text_state_element);
    

    That call should work to remove an element from a subform.

    Without subforms, it is just:

    $form->removeElement('username');
    $form->addElement($someNewElement);
    

    You can use getElement() in a similar way if you need to get an element from a form to make changes (e.g. remove/add validators, change description, set values)

    $el = $form->getElement('username');
    $el->addValidator($something)
       ->setLabel('Username:');
    

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 forms set up. In the first form I have the following
I have the following class: class Address { public string City {get; set;} public
I have simple form set up like the following: <form> <input type=text name=first_number id=first_number
I have set up a datepicker in a form using the following js: $(#Expiry).datepicker({
I have the swiss data set provided by R, which has the following form:
I have set the form decorators in this way: <?php $this->setElementDecorators(array( 'Label', array(array('labelTd' =>
I have asp.mvc project. Currently routing is set to form following url's: site/school/45 site/school/45/courses
I have a data set that is organized in the following manner: Timestamp|A0001|A0002|A0003|A0004|B0001|B0002|B0003|B0004 ...
Please consider the following scenario. I have a form with a property: class MyForm
I have the following problem: I have a form with three text input fields,

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.