我想生成一个带有最后一个选项的无线电组,带有输入字段文本的单选按钮,如果需要,此类还必须生成没有此输入字段的无线电组。这是班级:
class Radio extends \PFBC\OptionElement {
protected $_attributes = array("type" => "radio", "class" => "custom");
protected $inline;
protected $_hasOtherTextfield;
public function __construct($label, $name, $properties, $hasOtherTextfield) {
$this->_hasOtherTextfield = $hasOtherTextField;
parent::__construct($label, $name, $properties);
}
public function render() {
$labelClass = $this->_attributes["type"];
if(!empty($this->inline))
$labelClass .= " inline";
$count = 0;
foreach($this->options as $value => $text) {
$value = $this->getOptionValue($value);
echo '<div class="radio"><label class="form-control', $labelClass . '"> <input id="', $this->_attributes["id"], '-', $count, '"', $this->getAttributes(array("id", "value", "checked")), ' value="', $this->filter($value), '"';
if(isset($this->_attributes["value"]) && $this->_attributes["value"] == $value)
echo ' checked="checked"';
echo '/> ', $text, ' </label></div> ';
++$count;
}
if ($this->hasOtherTextField) {
echo '<div class="radio"><label class="form-control', $labelClass . '"> <input id="', $this->_attributes["id"], '-', $count, '"', $this->getAttributes(array("id", "value", "checked")), ' value="', $this->filter($value), '"';
if(isset($this->_attributes["value"]) && $this->_attributes["value"] == $value)
echo ' checked="checked"';
echo '/> ', $text, ' </label></div>';
echo '<input type="text" name=""/>';
}
}
}
这是OptionElement
类:
abstract class OptionElement extends Element {
protected $options;
public function __construct($label, $name, array $options, array $properties = null) {
$this->options = $options;
if(!empty($this->options) && array_values($this->options) === $this->options)
$this->options = array_combine($this->options, $this->options);
parent::__construct($label, $name, $properties);
}
protected function getOptionValue($value) {
$position = strpos($value, ":pfbc");
if($position !== false) {
if($position == 0)
$value = "";
else
$value = substr($value, 0, $position);
}
return $value;
}
}
在这里我创建了一个广播组:
$form->addElement(new Element\Radio("My Selected Radio Buttons", "Radio", array("Option #1", "test"), true //this will be the last option to generate radio with input));
所以在这段代码中我有一个错误:
Notice: Undefined variable: hasOtherTextField in
和
Notice: Undefined property: PFBC\Element\Radio::$hasOtherTextField
答案 0 :(得分:2)
Radio Radio tmethod render中的小错误。
第一个错误(变量名称区分大小写!):
使用
$this->_hasOtherTextfield = $hasOtherTextfield;
而不是
$this->_hasOtherTextfield = $hasOtherTextField;
第二个错误(错误的变量名称): 使用
if ($hasOtherTextField) { ... }
或者
if ($this->_hasOtherTextField) { ... }
而不是
if ($this->hasOtherTextField) {