编辑:好的,我已将其剥离到最低限度。
以下代码是我如何设置我想要在直接的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 Class进行设置,或者如何利用它来开始这样做。
答案 0 :(得分:4)
如果你正在使用Zend Framework,你真的不应该做这种事情(我的意思是写纯文本形式)。您应该使用内置方法。
首先,启用表单并创建表单。然后使用这个非常容易理解的代码。请注意,如果它100%工作,我没试过,但这是你需要的逻辑的100%。
表单类
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;
}
查看-OF-THE-ACTION.phtml 强>
if ($this->Form != null)
echo $this->Form;
我希望你能理解我为让你理解所做的努力。