我使用Zend_Form和Ajax有点失落。我在一个类中有一个表单,它扩展了从我的控制器调用的Zend_Form,这样:
GoodAddGroup.php
class Default_Form_GoodAddGroup extends Zend_Form {
(...)
public function init()
{
$this->setMethod('post');
$this->setAction("process-add-group");
$this->setName("addgroupgood");
// Load Elements class
require "Form/Elements.php";
$magElements = new Elements();
// Category
$categoryElement = $magElements->getCategorySelectField();
$categoryElement->setDecorators($this->elementDecorators);
// Barcode
$barcodeElement = $magElements->getGoodBarcodeTextField();
$barcodeElement->setDecorators($this->elementDecorators);
(...)
// Add elements to the form
$this->addElements(array(
$categoryElement,
//$codeElement,
$barcodeElement,
$serialElement,
$warehouseElement,
$submitButtonElement
));
$this->setDecorators($this->formDecorators);
}
}
在 GoodsController.php
中private function getAddGroupForm()
{
return new Default_Form_GoodAddGroup();
}
public function addGroupAction()
{
// Initialize the form for the view.
$this->view->form = $this->getAddGroupForm();
}
public function processAddGroupAction()
{
$form = $this->getAddGroupForm();
(...)
if ($_POST)
{
if ($form->isValid($_POST))
{
// Do things
} else {
$this->view->form = $form;
}
}
}
基本上,表单有一个类别选择字段,当选择一个类别时,会添加第二个“代码”选择器,其中填充了与该类别相关的项目。当显示带有表单的页面时(http:// myapp / goods / add-group),一切正常,ajax调用完成它的工作,第二个选择字段被添加并充分馈送,但正如你所看到的,使用processAddGroupAction()完成表单处理,此方法获取表单实例以获取其值并在出现问题时重新显示它。但是这样,我的“新”选择字段不再存在,所以我永远无法验证表单。
这是我第一次尝试将ajax / json与Zend一起使用,我想我需要帮助。
谢谢。
编辑:按要求添加了视图代码
<script>
$(function(){
$("#cats").change(function(){
getSelectBox(this);
});
$("#code").parent().parent().css('display', 'none');
getSelectBox($("#cats"));
});
function getSelectBox(element)
{
if($(element).val() != '')
{
$("#code").parent().parent().css('display', 'block');
if ($('#code').length <= 0) {
$("#cats").after('<select name="code" id="code" style="margin-left:10px"></select>');
}
$.getJSON("/goods/json-get-codes-from-category", {id: $(element).val(), ajax: "true"}, function(j){
console.log(j);
var options = "";
jQuery.each(j, function(i, val) {
options += '<option value="' + i + '">' + i + val + '</option>';
});
$("#code").html(options);
});
}
}
</script>
<?php echo $this->form; ?>
答案 0 :(得分:1)
您可以在表单中添加select元素'code',但不要在视图中显示它(它将从js创建)。因此,当表单发布时,“代码”也将被验证,因为它在$ _POST中
发布后,您必须显示没有$("#cats").change({..})
的选择框。您可以通过将js代码吐入函数
<script>
$(function(){
$("#cats").change(function(){
getSelectBox(this);
});
getSelectBox($("#cats"));
});
function getSelectBox(element)
{
if($(element).val() != '')
{
if ($('#code').length <= 0) {
$("#cats").after('<select name="code" id="code" style="margin-left:10px"></select>');
}
$.getJSON("/goods/json-get-codes-from-category", {id: $(element).val(), ajax: "true"}, function(j){
console.log(j);
var options = "";
jQuery.each(j, function(i, val) {
options += '<option value="' + i + '">' + i + val + '</option>';
});
$("#code").html(options);
});
}
}
</script>
希望这有帮助
答案 1 :(得分:0)
好的,我找到了解决方案!我发布它,以防它对其他人有用。 我知道问题是什么,但无法知道如何实现它。
在表单处理时,操作不存在新的select元素,因为它已使用javascript添加到视图中。要解决这个问题,我们需要告知有关新字段的操作,为此,我们首先需要在表单类中覆盖Zend_Form的方法“isValid”:
public function isValid($values)
{
$values = $this->_modifyElements($values);
return parent::isValid($values);
}
然后创建一个方法“_modifyElements”,它将通过添加新元素来修改表单:
protected function _modifyElements($values)
{
// Search for codes
$dbu = new DbUtils();
$codes = $dbu->getCodesFromCategory($values['cats']);
// Remove the current code element
$this->removeElement('code');
// Create a new element
$codeElement = new Zend_Form_Element_Select('code', array());
$codeElement->setLabel(_('Code :'));
$codeElement->setRequired(true);
$codeElement->addMultiOptions($codes);
$codeElement->setValue($values['code']);
$codeElement->setDecorators($this->elementDecorators);
$this->addElement($codeElement);
return $values;
}
我们必须覆盖“填充”方法:
public function populate(array $values)
{
$values = $this->_modifyElements($values);
return parent::populate($values);
}
瞧瞧。它对我有用;&gt; 所有关于此的信用都归Mohamed所有:http://jamandcheese-on-phptoast.com/2009/12/13/on-fly-elements-in-zend_form/