gpt4 book ai didi

html - Zend 子表单 View 脚本渲染

转载 作者:搜寻专家 更新时间:2023-10-31 19:27:42 25 4
gpt4 key购买 nike

我宁愿不处理装饰器,因为我的表单设计不是很直接,但我想保留验证表单的功能。

所以我在子表单正常工作的地方设置了它,但是当我尝试在我的 viewscript 中手动设置它的样式时,我得到了没有父表单的名称。我看过其他类似的帖子,但我还没有找到解决方案。

例子:

这是我的 View 脚本

<?php echo $this->form->username->renderViewHelper();?>

然后我得到

<input type="text" value="" id="username" name="username">

渲染时。应该是

<input type="text" value="" id="form1-username" name="form1[username]">

我如何获得 form1 部分?

谢谢!


编辑

好的,所以我找到了一种方法。

通过使用 belongTo,它有效:

    $form1->addElements(array(
new Zend_Form_Element_Text('username', array(
'belongsTo' => 'form1',
'required' => true,
'label' => 'Username:',
'filters' => array('StringTrim', 'StringToLower'),
'validators' => array(
'Alnum',
array('Regex',
false,
array('/^[a-z][a-z0-9]{2,}$/'))
)
))
));

是否有更好的方法或者这是唯一的方法?


编辑2

public function prepareSubForm($spec){
if (is_string($spec)) {
$subForm = $this->{$spec};
} elseif ($spec instanceof Zend_Form_SubForm) {
$subForm = $spec;
} else {
throw new Exception('Invalid argument passed to ' .
__FUNCTION__ . '()');
}
$this->setSubFormDecorators($subForm)
->addSubmitButton($subForm)
->addSubFormActions($subForm);
return $subForm;
}

public function setSubFormDecorators(Zend_Form_SubForm $subForm){
$subForm->setDecorators(array(
'FormElements', \\<--- I tried to change this to PrepareElements before.
array('HtmlTag', array('tag' => 'dl',
'class' => 'zend_form')),
'Form',
));
return $this;
}

最佳答案

我相信你可以通过使用获得你想要的输出:

<?php echo $this->form->username; ?>

在没有 renderViewHelper 的情况下调用它时,我得到了预期的输出。这也没有任何用于装饰器或准备子表单的特殊代码。我所要做的就是将 belongsTo 添加到表单元素。

更新:

如果将其设置为默认装饰器,则可以从渲染中消除 dd/dt 标签,而是使用 div。那么您可能更接近于获得您想要的自定义输出。您可以将 HtmlTag 中的 tagdiv 更改为您想要包装元素的任何标签。这是我最常使用的:

array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description')),
array('HtmlTag', array('tag' => 'div', 'class' => 'form-div')),
array('Label', array('class' => 'form-label', 'requiredSuffix' => '*'))
);

这是 Zend Framework 的默认值:

array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description')),
array('HtmlTag', array('tag' => 'dd', 'id' => array('callback' => $getId)))
array('Label', array('tag' => 'dt'))
);

请注意,文件和提交/按钮元素使用不同的装饰器。

另见 this answer

关于html - Zend 子表单 View 脚本渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7591096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com