如何将 class 添加到字段集?
How to add class to fieldset?
我在 ZF2 中有一个表单,其中添加了以下元素:
$this->add(array(
'name' => 'animals',
'type' => 'radio',
'attributes' => array(
'id' => 'animals',
'class' => 'form-control',
),
'options' => array(
'label' => 'Favourite animal',
'options' => array(
'cat' => 'Cat',
'dog' => 'Dog',
'fish' => 'Fish',
),
),
));
在我的视图脚本中有以下行:
<?php echo $this->formrow($form->get('animals')); ?>
生成以下 html:
<fieldset>
<legend>Favourite Animal</legend>
<label><input type="radio" name="animals" id="animals" class="form-control input-error" value="cat">Cat</label>
<label><input type="radio" name="animals" class="form-control input-error" value="dog">Dog</label>
<label><input type="radio" name="animals" class="form-control input-error" value="fish">Fish</label>
</fieldset>
如何将 class 添加到字段集中?
我尝试将以下内容添加到 options
数组、attributes
数组,并作为主数组的一个选项,但它没有将 class 添加到字段集:
'fieldset_attributes' => array(
'class' => 'form-group',
),
[编辑]
查看代码 (\Zend\Form\View\Helper\FormRow::render
) 我发现了这个:
...
// Multicheckbox elements have to be handled differently as the HTML standard does not allow nested
// labels. The semantic way is to group them inside a fieldset
if ($type === 'multi_checkbox' || $type === 'radio' || $element instanceof MonthSelect ) {
$markup = sprintf('<fieldset><legend>%s</legend>%s</fieldset>', $label, $elementString);
}
...
这意味着将 class 添加到字段集(或图例,如果需要)的唯一方法是扩展视图助手。
我遵循发布的答案 )。
来自答案(根据我的要求进行了修改):
Create the Application\Form\View\Helper\FormRow.php
helper class like
below:
<?php
/**
* Extend zend form view helper formrow to allow class to be added to fieldset / legend
*/
namespace Application\Form\View\Helper;
use Zend\Form\View\Helper\FormRow as ZendFormRow;
class FormRow extends ZendFormRow
{
/**
* Utility form helper that renders a label (if it exists), an element and errors
*
* @param ElementInterface $element
* @throws \Zend\Form\Exception\DomainException
* @return string
*/
public function render(\Zend\Form\ElementInterface $element)
{
//... other code here
// Multicheckbox elements have to be handled differently as the HTML standard does not allow nested
// labels. The semantic way is to group them inside a fieldset
if ($type === 'multi_checkbox'
|| $type === 'radio'
|| $element instanceof MonthSelect
) {
$fieldset_class = $legend_class = '';
if($class = $element->getOption('fieldset_class')) {
$fieldset_class = sprintf(' class="%s"', $class);
}
if($class = $element->getOption('legend_class')) {
$legend_class = sprintf(' class="%s"', $class);
}
$markup = sprintf(
'<fieldset%s><legend%s>%s</legend>%s</fieldset>',
$fieldset_class,
$legend_class,
$label,
$elementString);
}
//... other code here
return $markup;
}
}
And override the factory in the onBootstrap() method of the Module.php
file like below:
namespace Application;
use Zend\Mvc\MvcEvent;
use Zend\View\HelperPluginManager;
class Module
{
/**
* On bootstrap for application module.
*
* @param MvcEvent $event
* @return void
*/
public function onBootstrap(MvcEvent $event)
{
$services = $event->getApplication()->getServiceManager();
// The magic happens here
$services->get('ViewHelperManager')->setFactory('formrow', function (HelperPluginManager $manager) {
return new \Application\Form\View\Helper\FormRow();
});
}
}
并这样添加 类:
$this->add(array(
'name' => 'animals',
'type' => 'radio',
'attributes' => array(
'id' => 'animals',
'class' => 'form-control',
),
'options' => array(
'label' => 'Favourite animal',
'fieldset_class' => 'form-group', //<== this
'legend_class' => 'form-legend', //<== and this
'options' => array(
'cat' => 'Cat',
'dog' => 'Dog',
'fish' => 'Fish',
),
),
));
我在 ZF2 中有一个表单,其中添加了以下元素:
$this->add(array(
'name' => 'animals',
'type' => 'radio',
'attributes' => array(
'id' => 'animals',
'class' => 'form-control',
),
'options' => array(
'label' => 'Favourite animal',
'options' => array(
'cat' => 'Cat',
'dog' => 'Dog',
'fish' => 'Fish',
),
),
));
在我的视图脚本中有以下行:
<?php echo $this->formrow($form->get('animals')); ?>
生成以下 html:
<fieldset>
<legend>Favourite Animal</legend>
<label><input type="radio" name="animals" id="animals" class="form-control input-error" value="cat">Cat</label>
<label><input type="radio" name="animals" class="form-control input-error" value="dog">Dog</label>
<label><input type="radio" name="animals" class="form-control input-error" value="fish">Fish</label>
</fieldset>
如何将 class 添加到字段集中?
我尝试将以下内容添加到 options
数组、attributes
数组,并作为主数组的一个选项,但它没有将 class 添加到字段集:
'fieldset_attributes' => array(
'class' => 'form-group',
),
[编辑]
查看代码 (\Zend\Form\View\Helper\FormRow::render
) 我发现了这个:
...
// Multicheckbox elements have to be handled differently as the HTML standard does not allow nested
// labels. The semantic way is to group them inside a fieldset
if ($type === 'multi_checkbox' || $type === 'radio' || $element instanceof MonthSelect ) {
$markup = sprintf('<fieldset><legend>%s</legend>%s</fieldset>', $label, $elementString);
}
...
这意味着将 class 添加到字段集(或图例,如果需要)的唯一方法是扩展视图助手。
我遵循发布的答案 )。
来自答案(根据我的要求进行了修改):
Create the
Application\Form\View\Helper\FormRow.php
helper class like below:<?php /** * Extend zend form view helper formrow to allow class to be added to fieldset / legend */ namespace Application\Form\View\Helper; use Zend\Form\View\Helper\FormRow as ZendFormRow; class FormRow extends ZendFormRow { /** * Utility form helper that renders a label (if it exists), an element and errors * * @param ElementInterface $element * @throws \Zend\Form\Exception\DomainException * @return string */ public function render(\Zend\Form\ElementInterface $element) { //... other code here // Multicheckbox elements have to be handled differently as the HTML standard does not allow nested // labels. The semantic way is to group them inside a fieldset if ($type === 'multi_checkbox' || $type === 'radio' || $element instanceof MonthSelect ) { $fieldset_class = $legend_class = ''; if($class = $element->getOption('fieldset_class')) { $fieldset_class = sprintf(' class="%s"', $class); } if($class = $element->getOption('legend_class')) { $legend_class = sprintf(' class="%s"', $class); } $markup = sprintf( '<fieldset%s><legend%s>%s</legend>%s</fieldset>', $fieldset_class, $legend_class, $label, $elementString); } //... other code here return $markup; } }
And override the factory in the onBootstrap() method of the Module.php file like below:
namespace Application; use Zend\Mvc\MvcEvent; use Zend\View\HelperPluginManager; class Module { /** * On bootstrap for application module. * * @param MvcEvent $event * @return void */ public function onBootstrap(MvcEvent $event) { $services = $event->getApplication()->getServiceManager(); // The magic happens here $services->get('ViewHelperManager')->setFactory('formrow', function (HelperPluginManager $manager) { return new \Application\Form\View\Helper\FormRow(); }); } }
并这样添加 类:
$this->add(array(
'name' => 'animals',
'type' => 'radio',
'attributes' => array(
'id' => 'animals',
'class' => 'form-control',
),
'options' => array(
'label' => 'Favourite animal',
'fieldset_class' => 'form-group', //<== this
'legend_class' => 'form-legend', //<== and this
'options' => array(
'cat' => 'Cat',
'dog' => 'Dog',
'fish' => 'Fish',
),
),
));