views:

233

answers:

1

Hi, I'm new to Zend Framework, I have a question is that if I have two Zend_Form_Element_Text in a form, and I want make either of them to be filled by the user.

For example, phone number and mobile number. People only need enter one of them to continue.

How am I going to do this? Thanks

A: 

Hi i think you can do the following.

If you're initalizing your form in an Controller Action try this:

/**
 * IndexAction
 *
 * @return void
 */
public function indexAction() {

    // initalize your form
    $form = new MyForm();

    // get post data
    $post = $this->_request->getPost();

    // check if phone number is empty

    if (! empty($post['phone_number'])) {

        // remove validator from mobile phone element
        $mobile = $form->getElement('mobile');
        $mobile->removeValidator('notEmpty');
        $mobile->setRequired(false);
    }

    if ($form->isValid($this->getRequest()->getPost())) {
        $input = $form->getValues();

        // do something with the input
        print_r($input, true);
    }
}
ArneRie