我使用以下代码向Zend Framework 2表单添加一个email元素:
$form->add(array(
'type' => 'ZendFormElementEmail',
'name' => 'email',
'options' => array(
'label' => 'Email'
),
));
默认情况下,此元素的getInputSpecification()方法将required设置为true。但是元素对象不包含任何必需的属性,因此标记也不包含。
我怎么能添加标记到我的表单我的css能够添加所需的后缀?或者至少:自定义视图助手如何获取"所需"设置?
我意识到我可以添加一个required属性,但这感觉不对,因为它可能会与内部元素'required'设置不同步。
尽管Sam和Cellulosa的反应是一个很好的解决方案。
我担心随着ZF2框架的发展,这些将无法扩展。尽管他们扩展了ZendFormViewHelperFormLabel,但__invoke方法中有很多重复的代码。由于对框架和原始帮助器进行了更改,我们必须不断更新新的表单视图帮助器,以复制原始__invoke方法中的任何更改。
一个更简单的解决方案是使用提供的参数在ViewHelper中调用parent::__invoke(),以避免重复代码。
所以,这是解决方案:
在Application/src/Application/Form/View/Helper/RequiredMarkInFormLabel.php创建ViewHelper
<?php
namespace ApplicationFormViewHelper;
use ZendFormViewHelperFormLabel as OriginalFormLabel;
use ZendFormElementInterface;
/**
* Add mark (*) for all required elements inside a form.
*/
class RequiredMarkInFormLabel extends OriginalFormLabel
{
/**
* Invokable
*
* @return str
*/
public function __invoke(ElementInterface $element = null, $labelContent = null, $position = null)
{
// invoke parent and get form label
$originalformLabel = parent::__invoke($element,$labelContent,$position);
// check if element is required
if ($element->hasAttribute('required')) {
// add a start to required elements
return '<span class="required-mark">*</span>' . $originalformLabel;
}else{
// not start to optional elements
return $originalformLabel;
}
}
}
记得在Application/config/module.config.php中注册ViewHelper
'view_helpers' => array(
'invokables'=> array(
'formlabel' => 'ApplicationFormViewHelperRequiredMarkInFormLabel'
)
),
你基本上是对的。你绝对需要自己的ViewHelper。实现这一点的最简单方法是从ZendFormViewHelperFormLabel
扩展并覆盖__invoke()
函数。这只是一个快速的尝试,但我猜这有可能正确工作;)
public function __invoke(ElementInterface $element = null, $labelContent = null, $position = null)
{
// Implement all default lines of ZendFormViewHelperFormLabel
// Set $required to a default of true | existing elements required-value
$required = ($element->hasAttribute('required') ? $element->getAttribute('required') : true);
if (true === $required) {
$labelContent = sprintf(
'<span class="im-required">(*)</span> %s',
$labelContent
);
}
return $openTag . $labelContent . $this->closeTag();
}
记住在您的Module#getViewHelperConfig()
中注册您自己的ViewHelper
,像这样:
public function getViewHelperConfig()
{
return array(
'factories' => array(
'myFormLabel' => function($sm) {
return new MynamespaceFormViewHelperMyFormLabel;
},
),
);
}
我实际上有点惊讶这样的事情没有实现,虽然我确信有一个原因:)
使用Sam提供的指南,我设法将其排序如下:
我创建了一个Application/src/Application/Form/View/Helper/RequiredMarkInFormLabel.php
文件:
<?php
namespace ApplicationFormViewHelper;
use ZendFormViewHelperFormLabel as OriginalFormLabel;
use ZendFormElementInterface;
use ZendFormException;
class RequiredMarkInFormLabel extends OriginalFormLabel
{
public function __invoke(ElementInterface $element = null, $labelContent = null, $position = null)
{
...
// Set $required to a default of true | existing elements required-value
$required = ($element->hasAttribute('required') ? true : false);
if (true === $required) {
$labelContent = sprintf(
'%s<span class="required-mark">*</span>',
$labelContent
);
}
return $openTag . $labelContent . $this->closeTag();
}
}
我通过在Application/Module.php
中添加以下代码来启用:
public function getViewHelperConfig()
{
return array(
'invokables' => array(
'formlabel' => 'ApplicationFormViewHelperRequiredMarkInFormLabel',
),
);
}
希望对大家有所帮助!div;)
我所做的是基于InputFilter动态地在Elements上添加所需的属性。
On my formHelper:
public function render(Traversable $fieldset)
{
...
elseif ($element instanceof ElementInterface) {
if ($fieldset instanceof ZendInputFilterInputFilterProviderInterface){
$ifs = $fieldset->getInputFilterSpecification();
$start = strrpos($element->getName(), '[')+1;
$end = strpos($element->getName(),']');
$elementName = substr($element->getName(),$start,$end-$start);
if (isset($ifs[$elementName]['required'])){
$element->setAttribute('required', $ifs[$elementName]['required']);
}
}
$form .= $elementHelper->render($element);
...
return $form;
}
在ElementHelper public function render(ElementInterface $element){
...
$required = ($element->hasAttribute('required') ? $element->getAttribute('required') : false);
...
if (true === $required && ($label != '' || $label != null || $label != false)) {
$label .= '*';
}
因此,您不必担心像@Sam
好吧,我已经厌倦了继续看下去…因此,我简单地解析了elementname,在括号....之间查找最深的字符串以下是我的查看器,以twitter bootstrap的"form-horizontal"表单布局呈现表单的所有元素,并为必填项添加星号图标(在inputfilter中设置必填项,而不是元素属性)
FormControlGroup.php (ViewHelper渲染表单元素为a)
<?php
namespace ApplicationViewHelper;
use ZendInputFilterInputFilter;
use ZendFormElementInterface;
use ZendFormElement;
use ZendFormFieldset;
use ZendFormViewHelperAbstractHelper;
use ZendFormViewHelperFormLabel;
use ZendFormViewHelperFormElement;
use ZendFormViewHelperFormElementErrors;
use ApplicationViewHelperFormControlGroupFieldset;
use ZendDebugDebug;
class FormControlGroup extends AbstractHelper
{
public function __invoke(ElementInterface $elem = null,InputFilter $inputFilter = null){
if(!$elem){ return $this; }
// back up for fieldsets
if($elem instanceof Fieldset){
if($this->getView()){
$fcgf = new FormControlGroupFieldset();
$fcgf->setView($this->getView());
return $fcgf->__invoke($elem,$inputFilter);
} else {
throw new ExceptionDomainException(sprintf('FormControlGroup ViewHelper expects either an Element or a Fieldset. No View Could be found in the provided Object.'));
}
}
// add control-group container
$out = '<div class="control-group">';
// add control-label class to label if label exists
$out .= $this->renderLabel($elem,$inputFilter);
// add controls container
$out .= '<div class="controls">';
// ensure renderer for used viewhelpers
if($this->getView()){
// render element
$el = new FormElement();
$el->setView($this->getView());
$out .= $el->__invoke($elem);
unset($el);
// render element errors
$er = new FormElementErrors();
$er->setView($this->getView());
$out .= $er->__invoke($elem,array('class' => 'help-inline'));
unset($er);
} else {
$out .= 'No renderer found';
}
// close containers
$out .= '</div></div>';
return $out;
}
/**
*
* @param ElementInterface $elem
* @return string rendered Label with control-label class
*/
protected function renderLabel(ElementInterface &$elem,InputFilter &$inputFilter = null){
if($elem->getLabel()){
$lblAttr = $elem->getLabelAttributes();
if($lblAttr && key_exists('class', $lblAttr)){
if(stripos($lblAttr['class'],'control-label')){
$lblAttr['class'] .= 'control-label';
}
} else {
$lblAttr['class'] = 'control-label';
}
$elem->setLabelAttributes($lblAttr);
// check whether this element is required to fill out
if($inputFilter){
$inputs = $inputFilter->getInputs();
if(key_exists($this->getRealElementName($elem),$inputs)){
if($inputs[$this->getRealElementName($elem)]->isRequired()){
$elem->setLabel($elem->getLabel().'<i class="icon-asterisk"></i>');
}
}
}
$lbl = new FormLabel();
return $lbl->__invoke($elem);
} else {
return '';
}
}
public function getRealElementName($e){
$start = strrpos($e->getName(), '[')+1;
$end = strpos($e->getName(),']');
$elName = substr($e->getName(),$start,$end-$start);
return $elName;
}
}
FormControlGroupFieldset.php (ViewHelper渲染表单字段集作为FormControlGroupFieldset调用)
<?php
namespace ApplicationViewHelper;
use ZendInputFilterInputFilter;
use ZendFormFieldset;
use ZendFormViewHelperAbstractHelper;
use ApplicationViewHelperFormControlGroup;
use ZendDebugDebug;
class FormControlGroupFieldset extends AbstractHelper
{
public function __invoke(Fieldset $fs=null,InputFilter $inputFilter = null){
if(!$fs){ return $this; }
$out = '<fieldset>';
if($fs->getLabel()){
$out .= '<legend>'.$fs->getLabel().'</legend>';
}
if($this->getView() && sizeof($fs->getElements()) > 0){
$cg = new FormControlGroup();
$cg->setView($this->getView());
if($inputFilter){
$inputs = $inputFilter->getInputs();
$fsInput = $inputs[$fs->getName()];
}
foreach($fs->getElements() as $e){
$out .= $cg->__invoke($e,$fsInput);
}
unset($cg);
} else {
$out .= "No Fieldset renderer found.";
}
$out .= '</fieldset>';
return $out;
}
public function getRealElementName($e){
$start = strrpos($e->getName(), '[')+1;
$end = strpos($e->getName(),']');
$elName = substr($e->getName(),$start,$end-$start);
return $elName;
}
}
您可能需要调整名称空间…
如果有人想知道…有一个简单的解决方案,在伪类之后使用css:在标签后面添加"*"。
label.required:after{
content:'*';
}
不可能比这更简单了!