vojtech-dobes / nette-forms-inputlist
为 Nette 框架提供的替代 RadioList & CheckboxList
1.0.4
2013-04-04 00:01 UTC
Requires
- php: >=5.3.2
- nette/nette: 2.0.*
This package is not auto-updated.
Last update: 2024-09-14 12:50:06 UTC
README
为 Nette 框架提供的替代 RadioList & CheckboxList
许可证
New BSD
依赖
Nette 2.0.0
安装
- 从 Github 或通过 Composer (
vojtech-dobes/nette-forms-inputlist
) 获取源代码。 - 将
VojtechDobes\NetteForms\InputListExtension
注册为$configurator
的扩展。
$configurator->onCompile[] = function ($configurator, $compiler) { $compiler->addExtension('inputlist', new VojtechDobes\NetteForms\InputListExtension); };
使用方法
RadioList
$form->addMultiRadio('sex', 'Sex:', array( 'male' => 'Male', 'female' => 'Female', ));
方法名称选择是为了不干扰原生的
addRadiolist
。
CheckboxList
$checkboxlist = $form->addMultiCheckbox('topics', 'I like:', array( 'l' => 'lifestyle', 'm' => 'military', 'c' => 'computers', 'f' => 'flowers', ));
默认值
$checkboxlist->setDefaultValue(array('l', 'm')); // lifestyle, military
返回值
$checkboxlist->getValue() === array(0 => 'l', 1 => 'm')
渲染
自动
RadioList 和 CheckboxList 都提供标准机制,如 getControlPrototype
等。您也可以强制省略最后一个分隔符
$radiolist->omitLastSeparator();
手动
Latte 模板中有一个特殊的新宏:{inputlist}
。它的行为与 {foreach}
完全相同,但它专门设计用于与 *List
表单元素一起使用。
{form formName} {inputlist sex as $key => $label} {input} {label /} {sep}<br>{/sep} {/inputlist} {/form}
宏 {input}
和 {label}
的行为与以往相同,但如果没有使用元素标识符,它们将为特定迭代项渲染适当的元素。如果您使用标识符,它将渲染表单中的相应元素。
{form formName} {inputlist sex as $key => $label} {input} {label /}<br> {input send} {* standard button, no problem *} {/inputlist} {/form}
您可以向它们添加通常的 HTML 属性。
{form formName} {inputlist sex as $key => $label} {input class => 'input-radio'} {label}{$label}{/label}<br> {/inputlist} {/form}
{inputlist}
的属性版本也是可能的
{form formName} <ul n:inner-inputlist="sex as $key => $label"> <li>{input} {label /}</li> </ul> {/form}
验证
这里列出了支持的规则
CheckboxList
RadioList
所有规则也都在客户端支持。