'post', 'accept-charset' => 'utf-8'), $attributes); return "
'; } /** * Form closing tag * * @static * @return string */ public static function close() { return '
'; } /** * Creates a label for an input * * @param string $text The label text * @param string $fieldName Name of the input element * @param array $attributes HTML attributes * @return string */ public static function label($text, $fieldName = null, array $attributes = array()) { if (!isset($attributes['for']) && $fieldName !== null) { $attributes['for'] = static::autoId($fieldName); } if (!isset($attributes['id']) && isset($attributes['for'])) { $attributes['id'] = $attributes['for'] . '-label'; } return self::tag('label', $attributes, $text); } /** * Creates a text field * * @param string $name * @param string $value * @param array $attributes HTML attributes * @return string */ public static function text($name, $value = null, array $attributes = array(),$type='text') { $attributes = array_merge(array( 'id' => static::autoId($name), 'name' => $name, 'type' => $type, 'value' => $value, ), $attributes); return self::tag('input', $attributes); } /** * Creates a password input field * * @static * @param string $name * @param string $value * @param array $attributes HTML attributes * @return string */ public static function password($name, $value = null, array $attributes = array()) { $attributes = array_merge(array( 'id' => static::autoId($name), 'name' => $name, 'type' => 'password', 'value' => $value, ), $attributes); return self::tag('input', $attributes); } /** * Creates a hidden input field * * @static * @param string $name * @param string $value * @param array $attributes * @return string */ public static function hidden($name, $value, array $attributes = array()) { $attributes = array_merge(array( 'id' => static::autoId($name), 'name' => $name, 'type' => 'hidden', 'value' => $value, ), $attributes); return self::tag('input', $attributes); } /** * Creates a textarea * * @param string $name * @param string $text * @param array $attributes HTML attributes * @return string */ public static function textArea($name, $text = null, array $attributes = array()) { $attributes = array_merge(array( 'id' => static::autoId($name), 'name' => $name, ), $attributes); return self::tag('textarea', $attributes, (string)$text); } /** * Creates a check box. * By default creates a hidden field with the value of 0, so that the field is present in $_POST even when not checked * * @param string $name * @param bool $checked * @param mixed $value Checked value * @param array $attributes HTML attributes * @param bool|string $withHiddenField Pass false to omit the hidden field or "array" to return both parts as an array * @return string */ public static function checkBox( $name, $checked = false, $value = 1, array $attributes = array(), $withHiddenField = true ) { $auto_id = static::autoId($name); $checkboxAttributes = array_merge(array( 'name' => $name, 'type' => 'checkbox', 'value' => $value, 'id' => $auto_id, 'checked' => (bool)$checked, ), $attributes); $checkbox = self::tag('input', $checkboxAttributes); if ($withHiddenField === false) { return $checkbox; } $hiddenAttributes = array( 'name' => $name, 'type' => 'hidden', 'value' => 0, 'id' => $auto_id . '-hidden', ); $hidden = self::tag('input', $hiddenAttributes); return $withHiddenField === 'array' ? array($hidden, $checkbox) : $hidden . $checkbox; } /** * Creates multiple checkboxes for a has-many association. * * @param string $name * @param array $collection * @param array|\Traversable $checked Collection of checked values * @param array $labelAttributes * @param bool $returnAsArray * @throws \InvalidArgumentException * @return string */ public static function collectionCheckBoxes($name, array $collection, $checked, array $labelAttributes = array(), $returnAsArray = false) { // TODO: Does this check cover all options? if (!(is_array($checked) || $checked instanceof \Traversable)) { throw new \InvalidArgumentException("$name must be an array or Traversable!"); } $checkBoxes = array(); foreach ($collection as $value => $label) { $checkBoxes[] = self::tag( 'label', $labelAttributes, FormHelper::checkBox("{$name}[]", in_array($value, $checked, true), $value, array(), false) . self::escape($label), false ); } return $returnAsArray ? $checkBoxes : implode('', $checkBoxes); } /** * Creates a radio button * * @static * @param string $name * @param string $value * @param bool $checked * @param array $attributes * @return string */ public static function radio($name, $value, $checked = false, array $attributes = array()) { $attributes = array_merge(array( 'type' => 'radio', 'name' => $name, 'value' => $value, 'checked' => (bool)$checked, ), $attributes); return self::tag('input', $attributes); } /** * Creates multiple radio buttons with labels * * @static * @param string $name * @param array $collection * @param mixed $checked Checked value * @param array $labelAttributes * @param bool $returnAsArray * @return array|string */ public static function collectionRadios($name, array $collection, $checked, array $labelAttributes = array(), $returnAsArray = false) { $radioButtons = array(); foreach ($collection as $value => $label) { $radioButtons[] = self::tag( 'label', $labelAttributes, FormHelper::radio($name, $value, $value === $checked) . self::escape($label), false ); } return $returnAsArray ? $radioButtons : implode('', $radioButtons); } /** * Creates a select tag * * // Simple select * select('coffee_id', array('b' => 'black', 'w' => 'white')); * * With option groups * select('beverage', array( * 'Coffee' => array('bc' => 'black', 'wc' => 'white'), * 'Tea' => array('gt' => 'Green', 'bt' => 'Black'), * )); * * * @param string $name Name of the attribute * @param array $collection An associative array used for the option values * @param mixed $selected Selected option Can be array or scalar * @param array $attributes HTML attributes * @return string */ public static function select($name, array $collection, $selected = null, array $attributes = array()) { $attributes = array_merge(array( 'name' => $name, 'id' => static::autoId($name), 'multiple' => false, ), $attributes); if (is_string($selected) || is_numeric($selected)) { $selected = array($selected => 1); } else if (is_array($selected)) { $selected = array_flip($selected); } else { $selected = array(); } $content = self::option('', '', $selected); foreach ($collection as $value => $element) { // Element is an optgroup // $content .= self::option(, $element['label'], $selected); $content .= self::option($element['value'], $element['label'], $selected); } return self::tag('select', $attributes, $content, false); } /** * Creates an option tag * * @param string $value * @param string $label * @param array $selected * @return string */ private static function option($value, $label, $selected) { // Special handling of option tag contents to enable indentation with   //$label = str_replace('&nbsp;', ' ', self::escape($label)); return self::tag( 'option', array( 'value' => $value, 'selected' => isset($selected[$value]), ), $label, false ); } /** * Creates a file input field * * @static * @param string $name * @param array $attributes HTML attributes * @return string */ public static function file($name, array $attributes = array()) { $attributes = array_merge(array( 'type' => 'file', 'name' => $name, 'id' => static::autoId($name), ), $attributes); return self::tag('input', $attributes); } public static function button($name, $text, array $attributes = array()) { $attributes = array_merge(array( 'id' => static::autoId($name), 'name' => $name, ), $attributes); return self::tag('button', $attributes, $text); } /** * Generate an ID given the name of an input * * @static * @param string $name * @return string|null */ public static function autoId($name) { // Don't set an id on collection inputs if (strpos($name, '[]') !== false) { return null; } // Hyphenate array keys, for example model[field][other_field] => model-field-other_field $name = preg_replace('/\[([^]]+)\]/u', '-\\1', $name); return $name; } /** * Generates an HTML tag * * @param string $tagName Name of the tag * @param array $attributes HTML attributes * @param string $content Content of the tag. Omit to create a self-closing tag * @param bool $escape_content * * @see attributes() * * @return string */ public static function tag($tagName, array $attributes = array(), $content = null, $escape_content = true) { $result = '<' . $tagName . static::attributes($attributes) . '>'; if ($content !== null) { $result .= ($escape_content ? static::escape($content) : $content) . ''; } return $result; } /** * Converts an array of HTML attributes to a string * * If an attribute is false or null, it will not be set. * * If an attribute is true or is passed without a key, it will * be set without an explicit value (useful for checked, disabled, ..) * * If an array is passed as a value, it will be joined using spaces * * Note: Starts with a space * * Html::attributes(array('id' => 'some-id', 'selected' => false, 'disabled' => true, 'class' => array('a', 'b'))); * //=> ' id="some-id" disabled class="a b"' * * * @param array $attributes Associative array of attributes * * @return string */ public static function attributes(array $attributes) { $result = ''; foreach ($attributes as $attribute => $value) { if ($value === false || $value === null) continue; if ($value === true) { $result .= ' ' . $attribute; } else if (is_numeric($attribute)) { $result .= ' ' . $value; } else { if (is_array($value)) { // support cases like 'class' => array('one', 'two') $value = implode(' ', $value); } $result .= ' ' . $attribute . '=\'' . static::escape($value) . '\''; } } return $result; } /** * Escapes a string for output in HTML * * @static * @param string $string * @return string */ public static function escape($string) { return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); } /** * 输出表单标题 * * @param string $title * @return void */ public static function formTitle(string $title) { return '
' . $title . '
'; } /** * 输出行标题 * * @param string $title * @return void */ public static function formRowTitle(string $title) { return '
' . $title . '
'; } /** * 输出行内容 * * @return void */ public static function formRowContent($fieldName, string $fieldValue, string $key) { return '

' . $fieldName . '

'; } /** * 生成Row * * @return void */ public function formRow() { $_div = "
"; $_div .= "
"; } }