English 中文(简体)
ZendFramework Form:如果核查失败,变化选择输入类型案文
原标题:ZendFramework Form: Change select to input type text if verification fails

EDIT: Okay,我把这 stripped倒到最低限度。

下面的法典是我如何确定我试图在直截了当的html/php中实现的目标。

如果提交表格,对田地的核查没有通过,则案文领域出现,否则,如果提交NOT表格,则提供下降。

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表格类别建立这种关系,也不知道如何利用它来这样做。

最佳回答

You really should not do this kind of stuff (I mean write plain-text form) if you re using Zend Framework. You should use the built-in methods.

首先,使表格能够形成并形成形式。 然后使用这一非常易懂的代码。 请注意,如果我的工作达到100%,我不会受到审判,但这是你需要的100%逻辑。

Form class

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;
}

VIEW-OF-THE-ACTION.phtml>

if ($this->Form != null)
   echo $this->Form;

I hope you ll appreciate the effort I made to let you understand.

问题回答

暂无回答




相关问题
C# Form Problem: new form losing control and randomly hiding

I m encountering strange behavior with forms on a c# 3.5 app. On a button click, my form1 hides itself, creates a new form2, and shows form2. Form1 also contains the event method triggered when ...

TCPlistener.BeginAcceptSocket - async question

Some time ago I have payed to a programmer for doing multithread server. In the meantime I have learned C# a bit and now I think I can see the slowndown problem - I was told by that guy that nothing ...

RoR: before_save on nested object in form?

I have a form with a nested object (customer < order), and it works except that it keeps creating a new customer record. I d like to have it check to see if an existing customer is already ...

Receive POST from External Form

I have a form on another website (using a different backend) that I want to be able to POST to my Rails application (on a different domain). How do I generate a valid authenticity token for the ...

Getting posted values in MVC PartialView

I ve created a PartialView which I render with Html.RenderPartial, passing the name of the view and the strongly-typed data item to bind to (below): <% Html.RenderPartial("...

Recaptcha - Form Customization

Does anyone know if recaptcha can be fully customize without the default frame. I need the recaptcha image to only be a certain width as well as the input field. Has anyone done this before with ...

Better way to retain form data on manual back redirect?

I have a form that, on submit, requires the customer to look over the data, and then confirm the changes before saving the data. However, I m using a really rough way of retaining data if the user ...