Forms ZendFramework表单:如果验证失败,则将select更改为输入类型文本

Forms ZendFramework表单:如果验证失败,则将select更改为输入类型文本,forms,zend-framework,Forms,Zend Framework,编辑:好的,我已经把这个精简到最低限度了 下面的代码是如何设置我试图在纯html/php中完成的内容 如果表单已提交且字段验证未通过,则会显示文本字段;否则,如果表单未提交,则会提供下拉列表 html/php: <form method="post" action=""> <div class="state"> <?php if(!$_POST['submit']){ // show the selec

编辑:好的,我已经把这个精简到最低限度了

下面的代码是如何设置我试图在纯html/php中完成的内容

如果表单已提交且字段验证未通过,则会显示文本字段;否则,如果表单未提交,则会提供下拉列表

html/php:

<form method="post" action="">
  <div class="state">
      <?php
          if(!$_POST['submit']){
              // show the select list of states.
              echo '<select name="state">
                      <option>list of all states</option>
                    </select>';
          }else{
              // show text input box
              echo '<input type="text" value="'.$_POST['select'].'" name="state" />';
          }
      ?>
  </div>
  <input type="submit" name="submit" value="submit" />
但是我不知道如何使用ZendFramework Forms类来设置它,也不知道如何利用它来开始这样做。

如果使用Zend Framework,你真的不应该做这种事情,我的意思是写纯文本表单。您应该使用内置的方法

首先,启用表单并创建表单。然后使用这个非常容易理解的代码。请注意,我没有尝试过它是否100%有效,但这是您所需要的100%逻辑

形式类

控制器类

VIEW-OF-THE-ACTION.phtml

我希望你会感激我为让你理解所做的努力。

如果你使用Zend Framework,你真的不应该做这种事情,我的意思是写纯文本表单。您应该使用内置的方法

首先,启用表单并创建表单。然后使用这个非常容易理解的代码。请注意,我没有尝试过它是否100%有效,但这是您所需要的100%逻辑

形式类

控制器类

VIEW-OF-THE-ACTION.phtml


我希望你会感激我为让你理解所做的努力。

我完全不清楚你在问什么。“你能展示一些代码并更清楚地解释你想做什么吗?”markus添加了一些代码试图解释我在做什么。对我来说,你完全不清楚你在问什么。“你能给我看一些代码,更清楚地解释一下你想做什么吗?”markus添加了一些代码,试图解释一下我在做什么。
class Application_Form_YourFormName extends Zend_Form
{
   public function init()
   {

      $this->setMethod(self::METHOD_POST);
      $this->setAction('THE-URL-WHERE-THIS-FORM-IS-MANAGED');

      $Element = new Zend_Form_Element_Text('state');
      $Element->setLabel('State:');
      $Element->addValidators(array(/*DON'T KNOW WHAT KIND OF VALIDATION YOU NEED*/));
      $Element->addFilters(array(new Zend_Filter_StringTrim(),
          new Zend_Filter_HtmlEntities(array('quotestyle' => ENT_QUOTES))));
      $Element->setRequired();
      $this->addElement($Element);
      unset($Element);

      $this->addElement('reset', 'Reset');
      $this->addElement('submit', 'Submit');
   }

   public function stateNotPresent()
   {
      $this->removeElement('state');

      // Note that getStates() is an hypotetical method of an
      // hypotetical Application_Model_State where you can retrieve an
      // array containing the list of the state you have. This array is
      // needed to fill the Select list.
      $States = Application_Model_State::getStates();
      $Element = new Zend_Form_Element_Select('statelist');
      $Element->setLabel('State:');
      $Element->setMultiOptions($States);
      $Element->addValidator(new Zend_Validate_InArray($States));
      $Element->setRequired();
      $Element->setOrder($this->count() - 2);
      $this->addElement($Element);
      unset($Element);
   }

}
public function name-of-the-action-you-needAction()
{
   $Form = new Application_Form_YourFormName();
   if ($this->_request->isPost())
   {
      if ($Form->isValid($this->_request->getPost()))
      {
         // Do things. A good text has been entered
      }
      else
      {
         $Form->stateNotPresent();
         if ($Form->isValid($this->_request->getPost()))
         {
            // Do things. A good selection has been entered.
         }
         else
         {
            // echo the edited form (the one with the dropdown list)
            $this->view->Form = $Form;
         }      
      }
   }
   // The first time the page is requested.
   // The page with the text box will be printed
   else
      $this->view->Form = $Form;
}
if ($this->Form != null)
   echo $this->Form;