lithium\template\helper\Form::radio PHP Method

radio() public method

Generates an HTML object.
public radio ( string $name, array $options = [] ) : string
$name string The name of the field
$options array All options to be used when generating the radio `` element: - `'checked'` _boolean_: Whether or not the field should be selected by default. - `'value'` _mixed_: if specified, it will be used as the 'value' html attribute. Defaults to `1` - Any other options specified are rendered as HTML attributes of the element.
return string Returns a `` tag with the given name and attributes
    public function radio($name, array $options = array())
    {
        $defaults = array('value' => '1');
        $options += $defaults;
        $default = $options['value'];
        $key = $name;
        list($name, $options, $template) = $this->_defaults(__FUNCTION__, $name, $options);
        list($scope, $options) = $this->_options($defaults, $options);
        if (!isset($options['checked'])) {
            $options['checked'] = $this->binding($key)->data == $default;
        }
        $options['value'] = $scope['value'];
        return $this->_render(__METHOD__, $template, compact('name', 'options'));
    }

Usage Example

Example #1
0
	public function radio($name, array $options = array(), array $list = array()) {
		if (empty($list)) {
			if (!isset($options['checked']) && isset($options['value'])) {
				$value = false;
				if ($name && $this->_binding ) {
					$value = $this->_binding->data($name);
				}
				if ($options['value'] == $value)
					$options['checked'] = true;
			}
			return parent::radio($name,$options);
		}
		$out = '';
		foreach ($list as $value => $label) {
			$itemOptions = $options;
			$itemOptions['id'] = $name.'-'.$label;
			$itemOptions['value'] = $value;
			$out .= '<div class="radio">';
			$out .= $this->radio($name, $itemOptions, array());
			$out .= $this->label($itemOptions['id'], $label);
			$out .= '</div>';
		}
		return $out;
	}