我正在开发一个zend框架项目,我需要用户选择学校,然后转到下一个表单,然后选择成绩。
例如,用户选择ABC高中,然后选择“8年级”
学校和成绩下拉菜单均采用软编码方式从数据库中提取数据。
我的问题是,当用户选择一所学校,然后在下一年级表单上,如果他们没有选择任何值并单击“提交”,则返回验证错误“值是必需的,不能为空”这是正确的,但下拉菜单会变空。
如果表单未验证,我想知道如何将值重新填充回成绩下拉菜单。
非常感谢
这是我的代码
这是我生成成绩值的函数(从数据库中获取)
public function processSchoolSelectionAction()
{
$form = $this->getSchoolSelectionForm();
if ($form->isValid($_POST))
{
// getting the values
$schoolId = $form->getValue('school');
$schoolYear = new Application_Model_DbTable_SchoolYear();
$schoolYearValues = $schoolYear->getYearValues($schoolId);
array_unshift($schoolYearValues, array ('key' =>'' , 'value' =>'Please Specify'));
$form = $this->getYearSelectionForm();
$form->year->addMultiOptions($schoolYearValues);
$form->schoolId->setValue($schoolId);
$this->view->form = $form;
}
else
{
$data = $form->getValues();
$form->populate($data);
$this->view->form = $form;
}
}
代码处理年份选择表
public function processYearSelectionAction() {
$form = $this->getYearSelectionForm();
if ($form->isValid($_POST))
{
// getting the values
$schoolId = $form->getValue('schoolId');
$yearId = $form->getValue('year');
$textbookList = new Application_Model_DbTable_TextbookList();
if ($textbookList->checkTextbookExist($schoolId, $yearId))
{ // check if textbookExist
}
else
{
$this->view->message = "Sorry, But the list you requested is currently not available for ordering online.";
}
}
else
{
$data = $form->getValues();
$form->populate($data);
$this->view->form = $form;
}
}
学校选择表
<?php
class Application_Form_SchoolSelection extends ZendX_JQuery_Form
{
public function init()
{
$this->setName('schoolSelection');
$school = new Application_Model_DbTable_School;
$schoolValues = $school->getSchoolValues();
array_unshift($schoolValues, array ('key' =>'' , 'value' =>'Please Specify'));
$schoolElement = new Zend_Form_Element_Select('school');
$schoolElement->addMultiOptions($schoolValues);
$schoolElement->setLabel('School');
$schoolElement->setRequired(true);
$schoolElement->setRegisterInArrayValidator(false);
$submitElement = new Zend_Form_Element_Submit('submit');
$submitElement->setLabel("Next");
$this->addElements(array(
$schoolElement,
$submitElement
));
}
}
?>
年级(年)选择表
<?php
class Application_Form_YearSelection extends ZendX_JQuery_Form
{
public function init()
{
$this->setName('yearSelection');
$yearElement = new Zend_Form_Element_Select('year');
$yearElement->setLabel('Year');
$yearElement->setRequired(true);
$yearElement->setRegisterInArrayValidator(false);
$schoolIdElement = new Zend_Form_Element_Hidden('schoolId');
$submitElement = new Zend_Form_Element_Submit('submit');
$submitElement->setLabel("Next");
$this->addElements(array(
$yearElement,
$schoolIdElement,
$submitElement
));
}
}
?>
答案 0 :(得分:1)
我就是这样做的:
在表单创建时,在Controller中,从请求传递数据:
$some_selected_data = $this->_getParam('param_from_request'); // you need to validate this
$form = new Application_Form_SchoolSelection( array('some_data' => $some_selected_data) );
然后,在Form Class中获取如下值:
$data = $this->getAttrib('some_data'); // the key value of array above
然后问问
if($data) {
// get value from DB and
//SET VALUE TO Zend_Form_Element
}
答案 1 :(得分:0)
显然,您需要重新填充“选择”字段的选项。
在processYearSelectionAction
中,在验证失败部分,只需抓住您存储在隐藏字段中的schoolId,并使用与processSchoolSelectionAction
中填充字段选项相同的方式使用它。