Commit 30d70be0 by Qiang Xue

Finished Html helper.

parent d2fcc69b
...@@ -382,6 +382,15 @@ class Application extends Module ...@@ -382,6 +382,15 @@ class Application extends Module
} }
/** /**
* Returns the URL manager for this application.
* @return \yii\web\UrlManager the URL manager for this application.
*/
public function getUrlManager()
{
return $this->getComponent('urlManager');
}
/**
* Returns the internationalization (i18n) component * Returns the internationalization (i18n) component
* @return \yii\i18n\I18N the internationalization component * @return \yii\i18n\I18N the internationalization component
*/ */
...@@ -411,8 +420,8 @@ class Application extends Module ...@@ -411,8 +420,8 @@ class Application extends Module
'i18n' => array( 'i18n' => array(
'class' => 'yii\i18n\I18N', 'class' => 'yii\i18n\I18N',
), ),
'securityManager' => array( 'urlManager' => array(
'class' => 'yii\base\SecurityManager', 'class' => 'yii\web\UrlManager',
), ),
)); ));
} }
......
...@@ -55,16 +55,16 @@ class ActiveRelation extends ActiveQuery ...@@ -55,16 +55,16 @@ class ActiveRelation extends ActiveQuery
/** /**
* Specifies the relation associated with the pivot table. * Specifies the relation associated with the pivot table.
* @param string $relationName the relation name. This refers to a relation declared in [[primaryModel]]. * @param string $relationName the relation name. This refers to a relation declared in [[primaryModel]].
* @param callback $callback a PHP callback for customizing the relation associated with the pivot table. * @param callable $callable a PHP callback for customizing the relation associated with the pivot table.
* Its signature should be `function($query)`, where `$query` is the query to be customized. * Its signature should be `function($query)`, where `$query` is the query to be customized.
* @return ActiveRelation the relation object itself. * @return ActiveRelation the relation object itself.
*/ */
public function via($relationName, $callback = null) public function via($relationName, $callable = null)
{ {
$relation = $this->primaryModel->getRelation($relationName); $relation = $this->primaryModel->getRelation($relationName);
$this->via = array($relationName, $relation); $this->via = array($relationName, $relation);
if ($callback !== null) { if ($callable !== null) {
call_user_func($callback, $relation); call_user_func($callable, $relation);
} }
return $this; return $this;
} }
...@@ -75,11 +75,11 @@ class ActiveRelation extends ActiveQuery ...@@ -75,11 +75,11 @@ class ActiveRelation extends ActiveQuery
* @param array $link the link between the pivot table and the table associated with [[primaryModel]]. * @param array $link the link between the pivot table and the table associated with [[primaryModel]].
* The keys of the array represent the columns in the pivot table, and the values represent the columns * The keys of the array represent the columns in the pivot table, and the values represent the columns
* in the [[primaryModel]] table. * in the [[primaryModel]] table.
* @param callback $callback a PHP callback for customizing the relation associated with the pivot table. * @param callable $callable a PHP callback for customizing the relation associated with the pivot table.
* Its signature should be `function($query)`, where `$query` is the query to be customized. * Its signature should be `function($query)`, where `$query` is the query to be customized.
* @return ActiveRelation * @return ActiveRelation
*/ */
public function viaTable($tableName, $link, $callback = null) public function viaTable($tableName, $link, $callable = null)
{ {
$relation = new ActiveRelation(array( $relation = new ActiveRelation(array(
'modelClass' => get_class($this->primaryModel), 'modelClass' => get_class($this->primaryModel),
...@@ -89,8 +89,8 @@ class ActiveRelation extends ActiveQuery ...@@ -89,8 +89,8 @@ class ActiveRelation extends ActiveQuery
'asArray' => true, 'asArray' => true,
)); ));
$this->via = $relation; $this->via = $relation;
if ($callback !== null) { if ($callable !== null) {
call_user_func($callback, $relation); call_user_func($callable, $relation);
} }
return $this; return $this;
} }
......
...@@ -281,33 +281,59 @@ class ArrayHelper ...@@ -281,33 +281,59 @@ class ArrayHelper
call_user_func_array('array_multisort', $args); call_user_func_array('array_multisort', $args);
} }
/** /**
* Encodes special characters in an array of strings into HTML entities. * Encodes special characters in an array of strings into HTML entities.
* Both the array keys and values will be encoded if needed. * Both the array keys and values will be encoded.
* If a value is an array, this method will also encode it recursively. * If a value is an array, this method will also encode it recursively.
* @param array $data data to be encoded * @param array $data data to be encoded
* @param boolean $valuesOnly whether to encode array values only. If false,
* both the array keys and array values will be encoded.
* @param string $charset the charset that the data is using. If not set, * @param string $charset the charset that the data is using. If not set,
* [[\yii\base\Application::charset]] will be used. * [[\yii\base\Application::charset]] will be used.
* @return array the encoded data * @return array the encoded data
* @see http://www.php.net/manual/en/function.htmlspecialchars.php * @see http://www.php.net/manual/en/function.htmlspecialchars.php
*/ */
public static function htmlEncode($data, $charset = null) public static function htmlEncode($data, $valuesOnly = false, $charset = null)
{ {
if ($charset === null) { if ($charset === null) {
$charset = Yii::$app->charset; $charset = Yii::$app->charset;
} }
$d = array(); $d = array();
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
if (is_string($key)) { if (!$valuesOnly && is_string($key)) {
$key = htmlspecialchars($key, ENT_QUOTES, $charset); $key = htmlspecialchars($key, ENT_QUOTES, $charset);
} }
if (is_string($value)) { if (is_string($value)) {
$value = htmlspecialchars($value, ENT_QUOTES, $charset); $d[$key] = htmlspecialchars($value, ENT_QUOTES, $charset);
} elseif (is_array($value)) {
$d[$key] = static::htmlEncode($value, $charset);
}
}
return $d;
}
/**
* Decodes HTML entities into the corresponding characters in an array of strings.
* Both the array keys and values will be decoded.
* If a value is an array, this method will also decode it recursively.
* @param array $data data to be decoded
* @param boolean $valuesOnly whether to decode array values only. If false,
* both the array keys and array values will be decoded.
* @return array the decoded data
* @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php
*/
public static function htmlDecode($data, $valuesOnly = false)
{
$d = array();
foreach ($data as $key => $value) {
if (!$valuesOnly && is_string($key)) {
$key = htmlspecialchars_decode($key, ENT_QUOTES);
}
if (is_string($value)) {
$d[$key] = htmlspecialchars_decode($value, ENT_QUOTES);
} elseif (is_array($value)) { } elseif (is_array($value)) {
$value = static::htmlEncode($value); $d[$key] = static::htmlDecode($value);
} }
$d[$key] = $value;
} }
return $d; return $d;
} }
......
...@@ -8,8 +8,11 @@ ...@@ -8,8 +8,11 @@
namespace yii\util; namespace yii\util;
use Yii; use Yii;
use yii\base\InvalidParamException;
/** /**
* Html provides a set of static methods for generating commonly used HTML tags.
*
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
...@@ -22,6 +25,7 @@ class Html ...@@ -22,6 +25,7 @@ class Html
public static $closeVoidElements = true; public static $closeVoidElements = true;
/** /**
* @var array list of void elements (element name => 1) * @var array list of void elements (element name => 1)
* @see closeVoidElements
* @see http://www.w3.org/TR/html-markup/syntax.html#void-element * @see http://www.w3.org/TR/html-markup/syntax.html#void-element
*/ */
public static $voidElements = array( public static $voidElements = array(
...@@ -43,9 +47,78 @@ class Html ...@@ -43,9 +47,78 @@ class Html
'wbr' => 1, 'wbr' => 1,
); );
/** /**
* @var boolean whether to render special attributes value. Defaults to true. Can be set to false for HTML5. * @var boolean whether to show the values of boolean attributes in element tags.
* If false, only the attribute names will be generated.
* @see booleanAttributes
*/
public static $showBooleanAttributeValues = true;
/**
* @var array list of boolean attributes. The presence of a boolean attribute on
* an element represents the true value, and the absence of the attribute represents the false value.
* @see showBooleanAttributeValues
* @see http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes
*/ */
public static $renderSpecialAttributesValue = true; public static $booleanAttributes = array(
'async' => 1,
'autofocus' => 1,
'autoplay' => 1,
'checked' => 1,
'controls' => 1,
'declare' => 1,
'default' => 1,
'defer' => 1,
'disabled' => 1,
'formnovalidate' => 1,
'hidden' => 1,
'ismap' => 1,
'loop' => 1,
'multiple' => 1,
'muted' => 1,
'nohref' => 1,
'noresize' => 1,
'novalidate' => 1,
'open' => 1,
'readonly' => 1,
'required' => 1,
'reversed' => 1,
'scoped' => 1,
'seamless' => 1,
'selected' => 1,
'typemustmatch' => 1,
);
/**
* @var array the preferred order of attributes in a tag. This mainly affects the order of the attributes
* that are rendered by [[renderAttributes()]].
*/
public static $attributeOrder = array(
'type',
'id',
'class',
'name',
'value',
'href',
'src',
'action',
'method',
'selected',
'checked',
'readonly',
'disabled',
'size',
'maxlength',
'width',
'height',
'rows',
'cols',
'alt',
'title',
'rel',
'media',
);
/** /**
...@@ -78,6 +151,7 @@ class Html ...@@ -78,6 +151,7 @@ class Html
* Generates a complete HTML tag. * Generates a complete HTML tag.
* @param string $name the tag name * @param string $name the tag name
* @param string $content the content to be enclosed between the start and end tags. It will not be HTML-encoded. * @param string $content the content to be enclosed between the start and end tags. It will not be HTML-encoded.
* If this is coming from end users, you should consider [[encode()]] it to prevent XSS attacks.
* @param array $attributes the element attributes. The values will be HTML-encoded using [[encode()]]. * @param array $attributes the element attributes. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated HTML tag * @return string the generated HTML tag
...@@ -117,7 +191,7 @@ class Html ...@@ -117,7 +191,7 @@ class Html
*/ */
public static function endTag($name) public static function endTag($name)
{ {
return !static::$closeVoidElements && isset(static::$voidElements[strtolower($name)]) ? '' : "</$name>"; return "</$name>";
} }
/** /**
...@@ -143,9 +217,7 @@ class Html ...@@ -143,9 +217,7 @@ class Html
if (!isset($attributes['type'])) { if (!isset($attributes['type'])) {
$attributes['type'] = 'text/css'; $attributes['type'] = 'text/css';
} }
return static::beginTag('style', $attributes) return static::tag('style', "\n/*<![CDATA[*/\n{$content}\n/*]]>*/\n", $attributes);
. "\n/*<![CDATA[*/\n{$content}\n/*]]>*/\n"
. static::endTag('style');
} }
/** /**
...@@ -161,110 +233,77 @@ class Html ...@@ -161,110 +233,77 @@ class Html
if (!isset($attributes['type'])) { if (!isset($attributes['type'])) {
$attributes['type'] = 'text/javascript'; $attributes['type'] = 'text/javascript';
} }
return static::beginTag('script', $attributes) return static::tag('script', "\n/*<![CDATA[*/\n{$content}\n/*]]>*/\n", $attributes);
. "\n/*<![CDATA[*/\n{$content}\n/*]]>*/\n"
. static::endTag('script');
}
/**
* Registers a 'refresh' meta tag.
* This method can be invoked anywhere in a view. It will register a 'refresh'
* meta tag with {@link CClientScript} so that the page can be refreshed in
* the specified seconds.
* @param integer $seconds the number of seconds to wait before refreshing the page
* @param string $url the URL to which the page should be redirected to. If empty, it means the current page.
* @since 1.1.1
*/
public static function refresh($seconds, $url = '')
{
$content = "$seconds";
if ($url !== '') {
$content .= ';' . static::normalizeUrl($url);
}
Yii::app()->clientScript->registerMetaTag($content, null, 'refresh');
} }
/** /**
* Links to the specified CSS file. * Generates a link tag that refers to an external CSS file.
* @param string $url the CSS URL * @param array|string $url the URL of the external CSS file. This parameter will be processed by [[url()]].
* @param string $media the media that this CSS should apply to. * @param array $attributes the attributes of the link tag. The values will be HTML-encoded using [[encode()]].
* @return string the CSS link. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated link tag
* @see url
*/ */
public static function cssFile($url, $media = '') public static function cssFile($url, $attributes = array())
{ {
return CHtml::linkTag('stylesheet', 'text/css', $url, $media !== '' ? $media : null); $attributes['rel'] = 'stylesheet';
$attributes['type'] = 'text/css';
$attributes['href'] = static::url($url);
return static::tag('link', '', $attributes);
} }
/** /**
* Encloses the given JavaScript within a script tag. * Generates a script tag that refers to an external JavaScript file.
* @param string $text the JavaScript to be enclosed * @param string $url the URL of the external JavaScript file. This parameter will be processed by [[url()]].
* @return string the enclosed JavaScript * @param array $attributes the attributes of the script tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated script tag
* @see url
*/ */
public static function script($text) public static function jsFile($url, $attributes = array())
{ {
return "<script type=\"text/javascript\">\n/*<![CDATA[*/\n{$text}\n/*]]>*/\n</script>"; $attributes['type'] = 'text/javascript';
$attributes['src'] = static::url($url);
return static::tag('script', '', $attributes);
} }
/** /**
* Includes a JavaScript file. * Generates a form start tag.
* @param string $url URL for the JavaScript file * @param array|string $action the form action URL. This parameter will be processed by [[url()]].
* @return string the JavaScript file tag * @param string $method form method, either "post" or "get" (case-insensitive)
* @param array $attributes the attributes of the form tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated form start tag.
* @see endForm
*/ */
public static function scriptFile($url) public static function beginForm($action = '', $method = 'post', $attributes = array())
{ {
return '<script type="text/javascript" src="' . static::encode($url) . '"></script>'; $attributes['action'] = $url = static::url($action);
} $attributes['method'] = $method;
/** $form = static::beginTag('form', $attributes);
* Generates an opening form tag.
* This is a shortcut to {@link beginForm}.
* @param mixed $action the form action URL (see {@link normalizeUrl} for details about this parameter.)
* @param string $method form method (e.g. post, get)
* @param array $htmlOptions additional HTML attributes (see {@link tag}).
* @return string the generated form tag.
*/
public static function form($action = '', $method = 'post', $htmlOptions = array())
{
return static::beginForm($action, $method, $htmlOptions);
}
/** // query parameters in the action are ignored for GET method
* Generates an opening form tag. // we use hidden fields to add them back
* Note, only the open tag is generated. A close tag should be placed manually
* at the end of the form.
* @param mixed $action the form action URL (see {@link normalizeUrl} for details about this parameter.)
* @param string $method form method (e.g. post, get)
* @param array $htmlOptions additional HTML attributes (see {@link tag}).
* @return string the generated form tag.
* @see endForm
*/
public static function beginForm($action = '', $method = 'post', $htmlOptions = array())
{
$htmlOptions['action'] = $url = static::normalizeUrl($action);
$htmlOptions['method'] = $method;
$form = static::tag('form', $htmlOptions, false, false);
$hiddens = array(); $hiddens = array();
if (!strcasecmp($method, 'get') && ($pos = strpos($url, '?')) !== false) { if (!strcasecmp($method, 'get') && ($pos = strpos($url, '?')) !== false) {
foreach (explode('&', substr($url, $pos + 1)) as $pair) { foreach (explode('&', substr($url, $pos + 1)) as $pair) {
if (($pos = strpos($pair, '=')) !== false) { if (($pos = strpos($pair, '=')) !== false) {
$hiddens[] = static::hiddenField(urldecode(substr($pair, 0, $pos)), urldecode(substr($pair, $pos + 1)), array('id' => false)); $hiddens[] = static::hiddenInput(urldecode(substr($pair, 0, $pos)), urldecode(substr($pair, $pos + 1)));
} else { } else {
$hiddens[] = static::hiddenField(urldecode($pair), '', array('id' => false)); $hiddens[] = static::hiddenInput(urldecode($pair), '');
}
} }
} }
$request = Yii::app()->request;
if ($request->enableCsrfValidation && !strcasecmp($method, 'post')) {
$hiddens[] = static::hiddenField($request->csrfTokenName, $request->getCsrfToken(), array('id' => false));
} }
if ($hiddens !== array()) { if ($hiddens !== array()) {
$form .= "\n" . static::tag('div', array('style' => 'display:none'), implode("\n", $hiddens)); $form .= "\n" . implode("\n", $hiddens);
} }
return $form; return $form;
} }
/** /**
* Generates a closing form tag. * Generates a form end tag.
* @return string the generated tag * @return string the generated tag
* @see beginForm * @see beginForm
*/ */
...@@ -275,854 +314,599 @@ class Html ...@@ -275,854 +314,599 @@ class Html
/** /**
* Generates a hyperlink tag. * Generates a hyperlink tag.
* @param string $text link body. It will NOT be HTML-encoded. Therefore you can pass in HTML code such as an image tag. * @param string $text link body. It will NOT be HTML-encoded. Therefore you can pass in HTML code
* @param mixed $url a URL or an action route that can be used to create a URL. * such as an image tag. If this is is coming from end users, you should consider [[encode()]]
* See {@link normalizeUrl} for more details about how to specify this parameter. * it to prevent XSS attacks.
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special * @param array|string|null $url the URL for the hyperlink tag. This parameter will be processed by [[url()]]
* attributes are also recognized (see {@link clientChange} and {@link tag} for more details.) * and will be used for the "href" attribute of the tag. If this parameter is null, the "href" attribute
* will not be generated.
* @param array $attributes the attributes of the hyperlink tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated hyperlink * @return string the generated hyperlink
* @see normalizeUrl * @see url
* @see clientChange
*/ */
public static function link($text, $url = '#', $htmlOptions = array()) public static function a($text, $url = null, $attributes = array())
{ {
if ($url !== '') { if ($url !== null) {
$htmlOptions['href'] = static::normalizeUrl($url); $attributes['href'] = static::url($url);
} }
static::clientChange('click', $htmlOptions); return static::tag('a', $text, $attributes);
return static::tag('a', $htmlOptions, $text);
} }
/** /**
* Generates a mailto link. * Generates a mailto hyperlink.
* @param string $text link body. It will NOT be HTML-encoded. Therefore you can pass in HTML code such as an image tag. * @param string $text link body. It will NOT be HTML-encoded. Therefore you can pass in HTML code
* @param string $email email address. If this is empty, the first parameter (link body) will be treated as the email address. * such as an image tag. If this is is coming from end users, you should consider [[encode()]]
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special * it to prevent XSS attacks.
* attributes are also recognized (see {@link clientChange} and {@link tag} for more details.) * @param string $email email address. If this is null, the first parameter (link body) will be treated
* as the email address and used.
* @param array $attributes the attributes of the hyperlink tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated mailto link * @return string the generated mailto link
* @see clientChange
*/ */
public static function mailto($text, $email = '', $htmlOptions = array()) public static function mailto($text, $email = null, $attributes = array())
{ {
if ($email === '') { return static::a($text, 'mailto:' . ($email === null ? $text : $email), $attributes);
$email = $text;
}
return static::link($text, 'mailto:' . $email, $htmlOptions);
} }
/** /**
* Generates an image tag. * Generates an image tag.
* @param string $src the image URL * @param string $src the image URL. This parameter will be processed by [[url()]].
* @param string $alt the alternative text display * @param array $attributes the attributes of the image tag. The values will be HTML-encoded using [[encode()]].
* @param array $htmlOptions additional HTML attributes (see {@link tag}). * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated image tag * @return string the generated image tag
*/ */
public static function image($src, $alt = '', $htmlOptions = array()) public static function img($src, $attributes = array())
{ {
$htmlOptions['src'] = $src; $attributes['src'] = static::url($src);
$htmlOptions['alt'] = $alt; if (!isset($attributes['alt'])) {
return static::tag('img', $htmlOptions); $attributes['alt'] = '';
}
return static::tag('img', null, $attributes);
} }
/** /**
* Generates a button. * Generates a label tag.
* @param string $label the button label * @param string $content label text. It will NOT be HTML-encoded. Therefore you can pass in HTML code
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special * such as an image tag. If this is is coming from end users, you should consider [[encode()]]
* attributes are also recognized (see {@link clientChange} and {@link tag} for more details.) * it to prevent XSS attacks.
* @return string the generated button tag * @param string $for the ID of the HTML element that this label is associated with.
* @see clientChange * If this is null, the "for" attribute will not be generated.
* @param array $attributes the attributes of the label tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated label tag
*/ */
public static function button($name, $label = 'button', $htmlOptions = array()) public static function label($content, $for = null, $attributes = array())
{ {
if (!isset($htmlOptions['name'])) { $attributes['for'] = $for;
if (!array_key_exists('name', $htmlOptions)) { return static::tag('label', $content, $attributes);
$htmlOptions['name'] = static::ID_PREFIX . static::$count++;
}
}
if (!isset($htmlOptions['type'])) {
$htmlOptions['type'] = 'button';
}
if (!isset($htmlOptions['value'])) {
$htmlOptions['value'] = $label;
}
static::clientChange('click', $htmlOptions);
return static::tag('input', $htmlOptions);
} }
/** /**
* Generates a button using HTML button tag. * Generates a button tag.
* This method is similar to {@link button} except that it generates a 'button' * @param string $name the name attribute. If it is null, the name attribute will not be generated.
* tag instead of 'input' tag. * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param string $label the button label. Note that this value will be directly inserted in the button element * @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded.
* without being HTML-encoded. * Therefore you can pass in HTML code such as an image tag. If this is is coming from end users,
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special * you should consider [[encode()]] it to prevent XSS attacks.
* attributes are also recognized (see {@link clientChange} and {@link tag} for more details.) * @param array $attributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned.
* If the attributes does not contain "type", a default one with value "button" will be used.
* @return string the generated button tag * @return string the generated button tag
* @see clientChange
*/ */
public static function htmlButton($label = 'button', $htmlOptions = array()) public static function button($name = null, $value = null, $content = 'Button', $attributes = array())
{ {
if (!isset($htmlOptions['name'])) { $attributes['name'] = $name;
$htmlOptions['name'] = static::ID_PREFIX . static::$count++; $attributes['value'] = $value;
if (!isset($attributes['type'])) {
$attributes['type'] = 'button';
} }
if (!isset($htmlOptions['type'])) { return static::tag('button', $content, $attributes);
$htmlOptions['type'] = 'button';
} }
static::clientChange('click', $htmlOptions);
return static::tag('button', $htmlOptions, $label); /**
* Generates a submit button tag.
* @param string $name the name attribute. If it is null, the name attribute will not be generated.
* @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded.
* Therefore you can pass in HTML code such as an image tag. If this is is coming from end users,
* you should consider [[encode()]] it to prevent XSS attacks.
* @param array $attributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated submit button tag
*/
public static function submitButton($name = null, $value = null, $content = 'Submit', $attributes = array())
{
$attributes['type'] = 'submit';
return static::button($name, $value, $content, $attributes);
} }
/** /**
* Generates a submit button. * Generates a reset button tag.
* @param string $label the button label * @param string $name the name attribute. If it is null, the name attribute will not be generated.
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* attributes are also recognized (see {@link clientChange} and {@link tag} for more details.) * @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded.
* @return string the generated button tag * Therefore you can pass in HTML code such as an image tag. If this is is coming from end users,
* @see clientChange * you should consider [[encode()]] it to prevent XSS attacks.
* @param array $attributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated reset button tag
*/ */
public static function submitButton($label = 'submit', $htmlOptions = array()) public static function resetButton($name = null, $value = null, $content = 'Reset', $attributes = array())
{ {
$htmlOptions['type'] = 'submit'; $attributes['type'] = 'reset';
return static::button($label, $htmlOptions); return static::button($name, $value, $content, $attributes);
} }
/** /**
* Generates a reset button. * Generates an input type of the given type.
* @param string $label the button label * @param string $type the type attribute.
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special * @param string $name the name attribute. If it is null, the name attribute will not be generated.
* attributes are also recognized (see {@link clientChange} and {@link tag} for more details.) * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @return string the generated button tag * @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* @see clientChange * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated input tag
*/ */
public static function resetButton($label = 'reset', $htmlOptions = array()) public static function input($type, $name = null, $value = null, $attributes = array())
{ {
$htmlOptions['type'] = 'reset'; $attributes['type'] = $type;
return static::button($label, $htmlOptions); $attributes['name'] = $name;
$attributes['value'] = $value;
return static::tag('input', null, $attributes);
} }
/** /**
* Generates an image submit button. * Generates an input button.
* @param string $src the image URL * @param string $name the name attribute.
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* attributes are also recognized (see {@link clientChange} and {@link tag} for more details.) * @param array $attributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated button tag * @return string the generated button tag
* @see clientChange
*/ */
public static function imageButton($src, $htmlOptions = array()) public static function buttonInput($name, $value = 'Button', $attributes = array())
{ {
$htmlOptions['src'] = $src; return static::input('button', $name, $value, $attributes);
$htmlOptions['type'] = 'image';
return static::button('submit', $htmlOptions);
} }
/** /**
* Generates a link submit button. * Generates a submit input button.
* @param string $label the button label * @param string $name the name attribute. If it is null, the name attribute will not be generated.
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* attributes are also recognized (see {@link clientChange} and {@link tag} for more details.) * @param array $attributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated button tag * @return string the generated button tag
* @see clientChange
*/ */
public static function linkButton($label = 'submit', $htmlOptions = array()) public static function submitInput($name = null, $value = 'Submit', $attributes = array())
{ {
if (!isset($htmlOptions['submit'])) { return static::input('submit', $name, $value, $attributes);
$htmlOptions['submit'] = isset($htmlOptions['href']) ? $htmlOptions['href'] : '';
}
return static::link($label, '#', $htmlOptions);
} }
/** /**
* Generates a label tag. * Generates a reset input button.
* @param string $label label text. Note, you should HTML-encode the text if needed. * @param string $name the name attribute. If it is null, the name attribute will not be generated.
* @param string $for the ID of the HTML element that this label is associated with. * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* If this is false, the 'for' attribute for the label tag will not be rendered. * @param array $attributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* @param array $htmlOptions additional HTML attributes. * Attributes whose value is null will be ignored and not put in the tag returned.
* The following HTML option is recognized: * @return string the generated button tag
* <ul>
* <li>required: if this is set and is true, the label will be styled
* with CSS class 'required' (customizable with CHtml::$requiredCss),
* and be decorated with {@link CHtml::beforeRequiredLabel} and
* {@link CHtml::afterRequiredLabel}.</li>
* </ul>
* @return string the generated label tag
*/ */
public static function label($label, $for, $htmlOptions = array()) public static function resetInput($name = null, $value = 'Reset', $attributes = array())
{ {
if ($for === false) { return static::input('reset', $name, $value, $attributes);
unset($htmlOptions['for']);
} else {
$htmlOptions['for'] = $for;
}
if (isset($htmlOptions['required'])) {
if ($htmlOptions['required']) {
if (isset($htmlOptions['class'])) {
$htmlOptions['class'] .= ' ' . static::$requiredCss;
} else {
$htmlOptions['class'] = static::$requiredCss;
}
$label = static::$beforeRequiredLabel . $label . static::$afterRequiredLabel;
}
unset($htmlOptions['required']);
}
return static::tag('label', $htmlOptions, $label);
} }
/** /**
* Generates a text field input. * Generates a text input field.
* @param string $name the input name * @param string $name the name attribute.
* @param string $value the input value * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special * @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* attributes are also recognized (see {@link clientChange} and {@link tag} for more details.) * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated input field * @return string the generated button tag
* @see clientChange
* @see inputField
*/ */
public static function textField($name, $value = '', $htmlOptions = array()) public static function textInput($name, $value = null, $attributes = array())
{ {
static::clientChange('change', $htmlOptions); return static::input('text', $name, $value, $attributes);
return static::inputField('text', $name, $value, $htmlOptions);
} }
/** /**
* Generates a hidden input. * Generates a hidden input field.
* @param string $name the input name * @param string $name the name attribute.
* @param string $value the input value * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $htmlOptions additional HTML attributes (see {@link tag}). * @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* @return string the generated input field * Attributes whose value is null will be ignored and not put in the tag returned.
* @see inputField * @return string the generated button tag
*/ */
public static function hiddenField($name, $value = '', $htmlOptions = array()) public static function hiddenInput($name, $value = null, $attributes = array())
{ {
return static::inputField('hidden', $name, $value, $htmlOptions); return static::input('hidden', $name, $value, $attributes);
} }
/** /**
* Generates a password field input. * Generates a password input field.
* @param string $name the input name * @param string $name the name attribute.
* @param string $value the input value * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special * @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* attributes are also recognized (see {@link clientChange} and {@link tag} for more details.) * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated input field * @return string the generated button tag
* @see clientChange
* @see inputField
*/ */
public static function passwordField($name, $value = '', $htmlOptions = array()) public static function passwordInput($name, $value = null, $attributes = array())
{ {
static::clientChange('change', $htmlOptions); return static::input('password', $name, $value, $attributes);
return static::inputField('password', $name, $value, $htmlOptions);
} }
/** /**
* Generates a file input. * Generates a file input field.
* Note, you have to set the enclosing form's 'enctype' attribute to be 'multipart/form-data'. * To use a file input field, you should set the enclosing form's "enctype" attribute to
* After the form is submitted, the uploaded file information can be obtained via $_FILES[$name] (see * be "multipart/form-data". After the form is submitted, the uploaded file information
* PHP documentation). * can be obtained via $_FILES[$name] (see PHP documentation).
* @param string $name the input name * @param string $name the name attribute.
* @param string $value the input value * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $htmlOptions additional HTML attributes (see {@link tag}). * @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* @return string the generated input field * Attributes whose value is null will be ignored and not put in the tag returned.
* @see inputField * @return string the generated button tag
*/ */
public static function fileField($name, $value = '', $htmlOptions = array()) public static function fileInput($name, $value = null, $attributes = array())
{ {
return static::inputField('file', $name, $value, $htmlOptions); return static::input('file', $name, $value, $attributes);
} }
/** /**
* Generates a text area input. * Generates a text area input.
* @param string $name the input name * @param string $name the input name
* @param string $value the input value * @param string $value the input value. Note that it will be encoded using [[encode()]].
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special * @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* attributes are also recognized (see {@link clientChange} and {@link tag} for more details.) * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated text area * @return string the generated text area tag
* @see clientChange
* @see inputField
*/ */
public static function textArea($name, $value = '', $htmlOptions = array()) public static function textarea($name, $value = '', $attributes = array())
{ {
$htmlOptions['name'] = $name; $attributes['name'] = $name;
if (!isset($htmlOptions['id'])) { return static::tag('textarea', static::encode($value), $attributes);
$htmlOptions['id'] = static::getIdByName($name);
} elseif ($htmlOptions['id'] === false) {
unset($htmlOptions['id']);
}
static::clientChange('change', $htmlOptions);
return static::tag('textarea', $htmlOptions, isset($htmlOptions['encode']) && !$htmlOptions['encode'] ? $value : static::encode($value));
} }
/** /**
* Generates a radio button. * Generates a radio button input.
* @param string $name the input name * @param string $name the name attribute.
* @param boolean $checked whether the radio button is checked * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special * @param boolean $checked whether the radio button should be checked.
* attributes are also recognized (see {@link clientChange} and {@link tag} for more details.) * @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* Since version 1.1.2, a special option named 'uncheckValue' is available that can be used to specify * Attributes whose value is null will be ignored and not put in the tag returned. The following attribute
* the value returned when the radio button is not checked. When set, a hidden field is rendered so that * will be specially handled and not put in the resulting tag:
* when the radio button is not checked, we can still obtain the posted uncheck value. *
* If 'uncheckValue' is not set or set to NULL, the hidden field will not be rendered. * - uncheck: string, the value associated with the uncheck state of the radio button. When this attribute
* @return string the generated radio button * is present, a hidden input will be generated so that if the radio button is not checked and is submitted,
* @see clientChange * the value of this attribute will still be submitted to the server via the hidden input.
* @see inputField *
* @return string the generated radio button tag
*/ */
public static function radioButton($name, $checked = false, $htmlOptions = array()) public static function radio($name, $value = '1', $checked = false, $attributes = array())
{ {
if ($checked) { $attributes['checked'] = $checked;
$htmlOptions['checked'] = 'checked'; if (isset($attributes['uncheck'])) {
} else {
unset($htmlOptions['checked']);
}
$value = isset($htmlOptions['value']) ? $htmlOptions['value'] : 1;
static::clientChange('click', $htmlOptions);
if (array_key_exists('uncheckValue', $htmlOptions)) {
$uncheck = $htmlOptions['uncheckValue'];
unset($htmlOptions['uncheckValue']);
} else {
$uncheck = null;
}
if ($uncheck !== null) {
// add a hidden field so that if the radio button is not selected, it still submits a value // add a hidden field so that if the radio button is not selected, it still submits a value
if (isset($htmlOptions['id']) && $htmlOptions['id'] !== false) { $hidden = static::hiddenInput($name, $attributes['uncheck']);
$uncheckOptions = array('id' => static::ID_PREFIX . $htmlOptions['id']); unset($attributes['uncheck']);
} else {
$uncheckOptions = array('id' => false);
}
$hidden = static::hiddenField($name, $uncheck, $uncheckOptions);
} else { } else {
$hidden = ''; $hidden = '';
} }
return $hidden . static::input('radio', $name, $value, $attributes);
// add a hidden field so that if the radio button is not selected, it still submits a value
return $hidden . static::inputField('radio', $name, $value, $htmlOptions);
} }
/** /**
* Generates a check box. * Generates a checkbox input.
* @param string $name the input name * @param string $name the name attribute.
* @param boolean $checked whether the check box is checked * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special * @param boolean $checked whether the checkbox should be checked.
* attributes are also recognized (see {@link clientChange} and {@link tag} for more details.) * @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* Since version 1.1.2, a special option named 'uncheckValue' is available that can be used to specify * Attributes whose value is null will be ignored and not put in the tag returned. The following attribute
* the value returned when the checkbox is not checked. When set, a hidden field is rendered so that * will be specially handled and not put in the resulting tag:
* when the checkbox is not checked, we can still obtain the posted uncheck value. *
* If 'uncheckValue' is not set or set to NULL, the hidden field will not be rendered. * - uncheck: string, the value associated with the uncheck state of the checkbox. When this attribute
* @return string the generated check box * is present, a hidden input will be generated so that if the checkbox is not checked and is submitted,
* @see clientChange * the value of this attribute will still be submitted to the server via the hidden input.
* @see inputField *
* @return string the generated checkbox tag
*/ */
public static function checkBox($name, $checked = false, $htmlOptions = array()) public static function checkbox($name, $value = '1', $checked = false, $attributes = array())
{ {
if ($checked) { $attributes['checked'] = $checked;
$htmlOptions['checked'] = 'checked'; if (isset($attributes['uncheck'])) {
} else { // add a hidden field so that if the checkbox is not selected, it still submits a value
unset($htmlOptions['checked']); $hidden = static::hiddenInput($name, $attributes['uncheck']);
} unset($attributes['uncheck']);
$value = isset($htmlOptions['value']) ? $htmlOptions['value'] : 1;
static::clientChange('click', $htmlOptions);
if (array_key_exists('uncheckValue', $htmlOptions)) {
$uncheck = $htmlOptions['uncheckValue'];
unset($htmlOptions['uncheckValue']);
} else {
$uncheck = null;
}
if ($uncheck !== null) {
// add a hidden field so that if the check box is not checked, it still submits a value
if (isset($htmlOptions['id']) && $htmlOptions['id'] !== false) {
$uncheckOptions = array('id' => static::ID_PREFIX . $htmlOptions['id']);
} else {
$uncheckOptions = array('id' => false);
}
$hidden = static::hiddenField($name, $uncheck, $uncheckOptions);
} else { } else {
$hidden = ''; $hidden = '';
} }
return $hidden . static::input('checkbox', $name, $value, $attributes);
// add a hidden field so that if the check box is not checked, it still submits a value
return $hidden . static::inputField('checkbox', $name, $value, $htmlOptions);
} }
/** /**
* Generates a drop down list. * Generates a drop-down list.
* @param string $name the input name * @param string $name the input name
* @param string $select the selected value * @param array $items the option data items. The array keys are option values, and the array values
* @param array $data data for generating the list options (value=>display). * are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
* You may use {@link listData} to generate this data. * For each sub-array, an option group will be generated whose label is the key associated with the sub-array.
* Please refer to {@link listOptions} on how this data is used to generate the list options. * If you have a list of data models, you may convert them into the format described above using
* Note, the values and labels will be automatically HTML-encoded by this method. * [[\yii\util\ArrayHelper::map()]].
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special *
* attributes are recognized. See {@link clientChange} and {@link tag} for more details. * Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in
* In addition, the following options are also supported specifically for dropdown list: * the labels will also be HTML-encoded.
* <ul> * @param string $selection the selected value
* <li>encode: boolean, specifies whether to encode the values. Defaults to true.</li> * @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* <li>prompt: string, specifies the prompt text shown as the first list option. Its value is empty. Note, the prompt text will NOT be HTML-encoded.</li> * Attributes whose value is null will be ignored and not put in the tag returned. The following attribute
* <li>empty: string, specifies the text corresponding to empty selection. Its value is empty. * will be specially handled and not put in the resulting tag:
* The 'empty' option can also be an array of value-label pairs. *
* Each pair will be used to render a list option at the beginning. Note, the text label will NOT be HTML-encoded.</li> * - prompt: string, a prompt text to be displayed as the first option;
* <li>options: array, specifies additional attributes for each OPTION tag. * - options: array, the attributes for the option tags. The array keys must be valid option values,
* The array keys must be the option values, and the array values are the extra * and the array values are the extra attributes for the corresponding option tags. For example,
* OPTION tag attributes in the name-value pairs. For example, *
* <pre> * ~~~
* array( * array(
* 'value1'=>array('disabled'=>true, 'label'=>'value 1'), * 'value1' => array('disabled' => true),
* 'value2'=>array('label'=>'value 2'), * 'value2' => array('label' => 'value 2'),
* ); * );
* </pre> * ~~~
* </li> *
* </ul> * - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options',
* Since 1.1.13, a special option named 'unselectValue' is available. It can be used to set the value * except that the array keys represent the optgroup labels specified in $items.
* that will be returned when no option is selected in multiple mode. When set, a hidden field is * @return string the generated drop-down list tag
* rendered so that if no option is selected in multiple mode, we can still obtain the posted
* unselect value. If 'unselectValue' is not set or set to NULL, the hidden field will not be rendered.
* @return string the generated drop down list
* @see clientChange
* @see inputField
* @see listData
*/ */
public static function dropDownList($name, $select, $data, $htmlOptions = array()) public static function dropDownList($name, $items, $selection = null, $attributes = array())
{ {
$htmlOptions['name'] = $name; $attributes['name'] = $name;
$options = static::renderOptions($items, $selection, $attributes);
if (!isset($htmlOptions['id'])) { return static::tag('select', "\n" . $options . "\n", $attributes);
$htmlOptions['id'] = static::getIdByName($name);
} elseif ($htmlOptions['id'] === false) {
unset($htmlOptions['id']);
}
static::clientChange('change', $htmlOptions);
$options = "\n" . static::listOptions($select, $data, $htmlOptions);
$hidden = '';
if (isset($htmlOptions['multiple'])) {
if (substr($htmlOptions['name'], -2) !== '[]') {
$htmlOptions['name'] .= '[]';
}
if (isset($htmlOptions['unselectValue'])) {
$hiddenOptions = isset($htmlOptions['id']) ? array('id' => static::ID_PREFIX . $htmlOptions['id']) : array('id' => false);
$hidden = static::hiddenField(substr($htmlOptions['name'], 0, -2), $htmlOptions['unselectValue'], $hiddenOptions);
unset($htmlOptions['unselectValue']);
}
}
// add a hidden field so that if the option is not selected, it still submits a value
return $hidden . static::tag('select', $htmlOptions, $options);
} }
/** /**
* Generates a list box. * Generates a list box.
* @param string $name the input name * @param string $name the input name
* @param mixed $select the selected value(s). This can be either a string for single selection or an array for multiple selections. * @param array $items the option data items. The array keys are option values, and the array values
* @param array $data data for generating the list options (value=>display) * are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
* You may use {@link listData} to generate this data. * For each sub-array, an option group will be generated whose label is the key associated with the sub-array.
* Please refer to {@link listOptions} on how this data is used to generate the list options. * If you have a list of data models, you may convert them into the format described above using
* Note, the values and labels will be automatically HTML-encoded by this method. * [[\yii\util\ArrayHelper::map()]].
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special *
* attributes are also recognized. See {@link clientChange} and {@link tag} for more details. * Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in
* In addition, the following options are also supported specifically for list box: * the labels will also be HTML-encoded.
* <ul> * @param string|array $selection the selected value(s)
* <li>encode: boolean, specifies whether to encode the values. Defaults to true.</li> * @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* <li>prompt: string, specifies the prompt text shown as the first list option. Its value is empty. Note, the prompt text will NOT be HTML-encoded.</li> * Attributes whose value is null will be ignored and not put in the tag returned. The following attribute
* <li>empty: string, specifies the text corresponding to empty selection. Its value is empty. * will be specially handled and not put in the resulting tag:
* The 'empty' option can also be an array of value-label pairs. *
* Each pair will be used to render a list option at the beginning. Note, the text label will NOT be HTML-encoded.</li> * - prompt: string, a prompt text to be displayed as the first option;
* <li>options: array, specifies additional attributes for each OPTION tag. * - options: array, the attributes for the option tags. The array keys must be valid option values,
* The array keys must be the option values, and the array values are the extra * and the array values are the extra attributes for the corresponding option tags. For example,
* OPTION tag attributes in the name-value pairs. For example, *
* <pre> * ~~~
* array( * array(
* 'value1'=>array('disabled'=>true, 'label'=>'value 1'), * 'value1' => array('disabled' => true),
* 'value2'=>array('label'=>'value 2'), * 'value2' => array('label' => 'value 2'),
* ); * );
* </pre> * ~~~
* </li> *
* </ul> * - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options',
* @return string the generated list box * except that the array keys represent the optgroup labels specified in $items.
* @see clientChange * - unselect: string, the value that will be submitted when no option is selected.
* @see inputField * When this attribute is set, a hidden field will be generated so that if no option is selected in multiple
* @see listData * mode, we can still obtain the posted unselect value.
*/ * @return string the generated list box tag
public static function listBox($name, $select, $data, $htmlOptions = array()) */
{ public static function listBox($name, $items, $selection = null, $attributes = array())
if (!isset($htmlOptions['size'])) { {
$htmlOptions['size'] = 4; if (!isset($attributes['size'])) {
$attributes['size'] = 4;
}
if (isset($attributes['unselect'])) {
// add a hidden field so that if the list box has no option being selected, it still submits a value
$hidden = static::hiddenInput($name, $attributes['unselect']);
unset($attributes['unselect']);
} else {
$hidden = '';
} }
if (isset($htmlOptions['multiple'])) { if (isset($attributes['multiple']) && $attributes['multiple'] && substr($name, -2) !== '[]') {
if (substr($name, -2) !== '[]') {
$name .= '[]'; $name .= '[]';
} }
} $attributes['name'] = $name;
return static::dropDownList($name, $select, $data, $htmlOptions); $options = static::renderOptions($items, $selection, $attributes);
return $hidden . static::tag('select', "\n" . $options . "\n", $attributes);
} }
/** /**
* Generates a check box list. * Generates a list of checkboxes.
* A check box list allows multiple selection, like {@link listBox}. * A checkbox list allows multiple selection, like [[listBox()]].
* As a result, the corresponding POST value is an array. * As a result, the corresponding submitted value is an array.
* @param string $name name of the check box list. You can use this name to retrieve * @param string $name the name attribute of each checkbox.
* the selected value(s) once the form is submitted. * @param array $items the data item used to generate the checkboxes.
* @param mixed $select selection of the check boxes. This can be either a string * The array keys are the labels, while the array values are the corresponding checkbox values.
* for single selection or an array for multiple selections. * Note that the labels will NOT be HTML-encoded, while the values will.
* @param array $data value-label pairs used to generate the check box list. * @param string|array $selection the selected value(s).
* Note, the values will be automatically HTML-encoded, while the labels will not. * @param callable $formatter a callback that can be used to customize the generation of the HTML code
* @param array $htmlOptions additional HTML options. The options will be applied to * corresponding to a single checkbox. The signature of this callback must be:
* each checkbox input. The following special options are recognized: *
* <ul> * ~~~
* <li>template: string, specifies how each checkbox is rendered. Defaults * function ($index, $label, $name, $value, $checked)
* to "{input} {label}", where "{input}" will be replaced by the generated * ~~~
* check box input tag while "{label}" be replaced by the corresponding check box label.</li> *
* <li>separator: string, specifies the string that separates the generated check boxes.</li> * where $index is the zero-based index of the checkbox in the whole list; $label
* <li>checkAll: string, specifies the label for the "check all" checkbox. * is the label for the checkbox; and $name, $value and $checked represent the name,
* If this option is specified, a 'check all' checkbox will be displayed. Clicking on * value and the checked status of the checkbox input.
* this checkbox will cause all checkboxes checked or unchecked.</li> * @return string the generated checkbox list
* <li>checkAllLast: boolean, specifies whether the 'check all' checkbox should be
* displayed at the end of the checkbox list. If this option is not set (default)
* or is false, the 'check all' checkbox will be displayed at the beginning of
* the checkbox list.</li>
* <li>labelOptions: array, specifies the additional HTML attributes to be rendered
* for every label tag in the list.</li>
* <li>container: string, specifies the checkboxes enclosing tag. Defaults to 'span'.
* If the value is an empty string, no enclosing tag will be generated</li>
* <li>baseID: string, specifies the base ID prefix to be used for checkboxes in the list.
* This option is available since version 1.1.13.</li>
* </ul>
* @return string the generated check box list
*/ */
public static function checkBoxList($name, $select, $data, $htmlOptions = array()) public static function checkboxList($name, $items, $selection = null, $formatter = null)
{ {
$template = isset($htmlOptions['template']) ? $htmlOptions['template'] : '{input} {label}';
$separator = isset($htmlOptions['separator']) ? $htmlOptions['separator'] : "<br/>\n";
$container = isset($htmlOptions['container']) ? $htmlOptions['container'] : 'span';
unset($htmlOptions['template'], $htmlOptions['separator'], $htmlOptions['container']);
if (substr($name, -2) !== '[]') { if (substr($name, -2) !== '[]') {
$name .= '[]'; $name .= '[]';
} }
if (isset($htmlOptions['checkAll'])) { $lines = array();
$checkAllLabel = $htmlOptions['checkAll']; $index = 0;
$checkAllLast = isset($htmlOptions['checkAllLast']) && $htmlOptions['checkAllLast']; foreach ($items as $value => $label) {
} $checked = $selection !== null &&
unset($htmlOptions['checkAll'], $htmlOptions['checkAllLast']); (!is_array($selection) && !strcmp($value, $selection)
|| is_array($selection) && in_array($value, $selection));
$labelOptions = isset($htmlOptions['labelOptions']) ? $htmlOptions['labelOptions'] : array(); if ($formatter !== null) {
unset($htmlOptions['labelOptions']); $lines[] = call_user_func($formatter, $index, $label, $name, $value, $checked);
$items = array();
$baseID = isset($htmlOptions['baseID']) ? $htmlOptions['baseID'] : static::getIdByName($name);
unset($htmlOptions['baseID']);
$id = 0;
$checkAll = true;
foreach ($data as $value => $label) {
$checked = !is_array($select) && !strcmp($value, $select) || is_array($select) && in_array($value, $select);
$checkAll = $checkAll && $checked;
$htmlOptions['value'] = $value;
$htmlOptions['id'] = $baseID . '_' . $id++;
$option = static::checkBox($name, $checked, $htmlOptions);
$label = static::label($label, $htmlOptions['id'], $labelOptions);
$items[] = strtr($template, array('{input}' => $option, '{label}' => $label));
}
if (isset($checkAllLabel)) {
$htmlOptions['value'] = 1;
$htmlOptions['id'] = $id = $baseID . '_all';
$option = static::checkBox($id, $checkAll, $htmlOptions);
$label = static::label($checkAllLabel, $id, $labelOptions);
$item = strtr($template, array('{input}' => $option, '{label}' => $label));
if ($checkAllLast) {
$items[] = $item;
} else { } else {
array_unshift($items, $item); $lines[] = static::label(static::checkbox($name, $value, $checked) . ' ' . $label);
}
$name = strtr($name, array('[' => '\\[', ']' => '\\]'));
$js = <<<EOD
jQuery('#$id').click(function() {
jQuery("input[name='$name']").prop('checked', this.checked);
});
jQuery("input[name='$name']").click(function() {
jQuery('#$id').prop('checked', !jQuery("input[name='$name']:not(:checked)").length);
});
jQuery('#$id').prop('checked', !jQuery("input[name='$name']:not(:checked)").length);
EOD;
$cs = Yii::app()->getClientScript();
$cs->registerCoreScript('jquery');
$cs->registerScript($id, $js);
}
if (empty($container)) {
return implode($separator, $items);
} else {
return static::tag($container, array('id' => $baseID), implode($separator, $items));
} }
$index++;
} }
/** return implode("\n", $lines);
* Generates a radio button list.
* A radio button list is like a {@link checkBoxList check box list}, except that
* it only allows single selection.
* @param string $name name of the radio button list. You can use this name to retrieve
* the selected value(s) once the form is submitted.
* @param string $select selection of the radio buttons.
* @param array $data value-label pairs used to generate the radio button list.
* Note, the values will be automatically HTML-encoded, while the labels will not.
* @param array $htmlOptions additional HTML options. The options will be applied to
* each radio button input. The following special options are recognized:
* <ul>
* <li>template: string, specifies how each radio button is rendered. Defaults
* to "{input} {label}", where "{input}" will be replaced by the generated
* radio button input tag while "{label}" will be replaced by the corresponding radio button label.</li>
* <li>separator: string, specifies the string that separates the generated radio buttons. Defaults to new line (<br/>).</li>
* <li>labelOptions: array, specifies the additional HTML attributes to be rendered
* for every label tag in the list.</li>
* <li>container: string, specifies the radio buttons enclosing tag. Defaults to 'span'.
* If the value is an empty string, no enclosing tag will be generated</li>
* <li>baseID: string, specifies the base ID prefix to be used for radio buttons in the list.
* This option is available since version 1.1.13.</li>
* </ul>
* @return string the generated radio button list
*/
public static function radioButtonList($name, $select, $data, $htmlOptions = array())
{
$template = isset($htmlOptions['template']) ? $htmlOptions['template'] : '{input} {label}';
$separator = isset($htmlOptions['separator']) ? $htmlOptions['separator'] : "<br/>\n";
$container = isset($htmlOptions['container']) ? $htmlOptions['container'] : 'span';
unset($htmlOptions['template'], $htmlOptions['separator'], $htmlOptions['container']);
$labelOptions = isset($htmlOptions['labelOptions']) ? $htmlOptions['labelOptions'] : array();
unset($htmlOptions['labelOptions']);
$items = array();
$baseID = isset($htmlOptions['baseID']) ? $htmlOptions['baseID'] : static::getIdByName($name);
unset($htmlOptions['baseID']);
$id = 0;
foreach ($data as $value => $label) {
$checked = !strcmp($value, $select);
$htmlOptions['value'] = $value;
$htmlOptions['id'] = $baseID . '_' . $id++;
$option = static::radioButton($name, $checked, $htmlOptions);
$label = static::label($label, $htmlOptions['id'], $labelOptions);
$items[] = strtr($template, array('{input}' => $option, '{label}' => $label));
}
if (empty($container)) {
return implode($separator, $items);
} else {
return static::tag($container, array('id' => $baseID), implode($separator, $items));
}
} }
/** /**
* Normalizes the input parameter to be a valid URL. * Generates a list of radio buttons.
* * A radio button list is like a checkbox list, except that it only allows single selection.
* If the input parameter is an empty string, the currently requested URL will be returned. * @param string $name the name attribute of each radio button.
* * @param array $items the data item used to generate the radio buttons.
* If the input parameter is a non-empty string, it is treated as a valid URL and will * The array keys are the labels, while the array values are the corresponding radio button values.
* be returned without any change. * Note that the labels will NOT be HTML-encoded, while the values will.
* @param string|array $selection the selected value(s).
* @param callable $formatter a callback that can be used to customize the generation of the HTML code
* corresponding to a single radio button. The signature of this callback must be:
* *
* If the input parameter is an array, it is treated as a controller route and a list of * ~~~
* GET parameters, and the {@link CController::createUrl} method will be invoked to * function ($index, $label, $name, $value, $checked)
* create a URL. In this case, the first array element refers to the controller route, * ~~~
* and the rest key-value pairs refer to the additional GET parameters for the URL.
* For example, <code>array('post/list', 'page'=>3)</code> may be used to generate the URL
* <code>/index.php?r=post/list&page=3</code>.
* *
* @param mixed $url the parameter to be used to generate a valid URL * where $index is the zero-based index of the radio button in the whole list; $label
* @return string the normalized URL * is the label for the radio button; and $name, $value and $checked represent the name,
* value and the checked status of the radio button input.
* @return string the generated radio button list
*/ */
public static function normalizeUrl($url) public static function radioList($name, $items, $selection = null, $formatter = null)
{ {
if (is_array($url)) { $lines = array();
if (isset($url[0])) { $index = 0;
if (($c = Yii::app()->getController()) !== null) { foreach ($items as $value => $label) {
$url = $c->createUrl($url[0], array_splice($url, 1)); $checked = $selection !== null &&
(!is_array($selection) && !strcmp($value, $selection)
|| is_array($selection) && in_array($value, $selection));
if ($formatter !== null) {
$lines[] = call_user_func($formatter, $index, $label, $name, $value, $checked);
} else { } else {
$url = Yii::app()->createUrl($url[0], array_splice($url, 1)); $lines[] = static::label(static::radio($name, $value, $checked) . ' ' . $label);
}
} else {
$url = '';
} }
} $index++;
return $url === '' ? Yii::app()->getRequest()->getUrl() : $url;
} }
/** return implode("\n", $lines);
* Generates an input HTML tag.
* This method generates an input HTML tag based on the given input name and value.
* @param string $type the input type (e.g. 'text', 'radio')
* @param string $name the input name
* @param string $value the input value
* @param array $htmlOptions additional HTML attributes for the HTML tag (see {@link tag}).
* @return string the generated input tag
*/
protected static function inputField($type, $name, $value, $htmlOptions)
{
$htmlOptions['type'] = $type;
$htmlOptions['value'] = $value;
$htmlOptions['name'] = $name;
if (!isset($htmlOptions['id'])) {
$htmlOptions['id'] = static::getIdByName($name);
} elseif ($htmlOptions['id'] === false) {
unset($htmlOptions['id']);
}
return static::tag('input', $htmlOptions);
} }
/** /**
* Generates the list options. * Renders the option tags that can be used by [[dropDownList()]] and [[listBox()]].
* @param mixed $selection the selected value(s). This can be either a string for single selection or an array for multiple selections. * @param array $items the option data items. The array keys are option values, and the array values
* @param array $listData the option data (see {@link listData}) * are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
* @param array $htmlOptions additional HTML attributes. The following two special attributes are recognized: * For each sub-array, an option group will be generated whose label is the key associated with the sub-array.
* <ul> * If you have a list of data models, you may convert them into the format described above using
* <li>encode: boolean, specifies whether to encode the values. Defaults to true.</li> * [[\yii\util\ArrayHelper::map()]].
* <li>prompt: string, specifies the prompt text shown as the first list option. Its value is empty. Note, the prompt text will NOT be HTML-encoded.</li> *
* <li>empty: string, specifies the text corresponding to empty selection. Its value is empty. * Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in
* The 'empty' option can also be an array of value-label pairs. * the labels will also be HTML-encoded.
* Each pair will be used to render a list option at the beginning. Note, the text label will NOT be HTML-encoded.</li> * @param string|array $selection the selected value(s). This can be either a string for single selection
* <li>options: array, specifies additional attributes for each OPTION tag. * or an array for multiple selections.
* The array keys must be the option values, and the array values are the extra * @param array $attributes the attributes parameter that is passed to the [[dropDownList()]] or [[listBox()]] call.
* OPTION tag attributes in the name-value pairs. For example, * This method will take out these elements, if any: "prompt", "options" and "groups". See more details
* <pre> * in [[dropDownList()]] for the explanation of these elements.
* array( *
* 'value1'=>array('disabled'=>true, 'label'=>'value 1'),
* 'value2'=>array('label'=>'value 2'),
* );
* </pre>
* </li>
* <li>key: string, specifies the name of key attribute of the selection object(s).
* This is used when the selection is represented in terms of objects. In this case,
* the property named by the key option of the objects will be treated as the actual selection value.
* This option defaults to 'primaryKey', meaning using the 'primaryKey' property value of the objects in the selection.
* This option has been available since version 1.1.3.</li>
* </ul>
* @return string the generated list options * @return string the generated list options
*/ */
public static function listOptions($selection, $listData, &$htmlOptions) public static function renderOptions($items, $selection = null, &$attributes = array())
{ {
$raw = isset($htmlOptions['encode']) && !$htmlOptions['encode']; $lines = array();
$content = ''; if (isset($attributes['prompt'])) {
if (isset($htmlOptions['prompt'])) { $prompt = strtr(static::encode($attributes['prompt']), ' ', '&nbsp;');
$content .= '<option value="">' . strtr($htmlOptions['prompt'], array('<' => '&lt;', '>' => '&gt;')) . "</option>\n"; $lines[] = static::tag('option', $prompt, array('value' => ''));
unset($htmlOptions['prompt']);
}
if (isset($htmlOptions['empty'])) {
if (!is_array($htmlOptions['empty'])) {
$htmlOptions['empty'] = array('' => $htmlOptions['empty']);
}
foreach ($htmlOptions['empty'] as $value => $label) {
$content .= '<option value="' . static::encode($value) . '">' . strtr($label, array('<' => '&lt;', '>' => '&gt;')) . "</option>\n";
}
unset($htmlOptions['empty']);
} }
if (isset($htmlOptions['options'])) { $options = isset($attributes['options']) ? $attributes['options'] : array();
$options = $htmlOptions['options']; $groups = isset($attributes['groups']) ? $attributes['groups'] : array();
unset($htmlOptions['options']); unset($attributes['prompt'], $attributes['options'], $attributes['groups']);
} else {
$options = array();
}
$key = isset($htmlOptions['key']) ? $htmlOptions['key'] : 'primaryKey'; foreach ($items as $key => $value) {
if (is_array($selection)) {
foreach ($selection as $i => $item) {
if (is_object($item)) {
$selection[$i] = $item->$key;
}
}
} elseif (is_object($selection)) {
$selection = $selection->$key;
}
foreach ($listData as $key => $value) {
if (is_array($value)) { if (is_array($value)) {
$content .= '<optgroup label="' . ($raw ? $key : static::encode($key)) . "\">\n"; $groupAttrs = isset($groups[$key]) ? $groups[$key] : array();
$dummy = array('options' => $options); $groupAttrs['label'] = $key;
if (isset($htmlOptions['encode'])) { $attrs = array('options' => $options, 'groups' => $groups);
$dummy['encode'] = $htmlOptions['encode']; $content = static::renderOptions($selection, $value, $attrs);
} $lines[] = static::tag('optgroup', "\n" . $content . "\n", $groupAttrs);
$content .= static::listOptions($selection, $value, $dummy);
$content .= '</optgroup>' . "\n";
} else { } else {
$attributes = array('value' => (string)$key, 'encode' => !$raw); $attrs = isset($options[$key]) ? $options[$key] : array();
if (!is_array($selection) && !strcmp($key, $selection) || is_array($selection) && in_array($key, $selection)) { $attrs['value'] = $key;
$attributes['selected'] = 'selected'; $attrs['selected'] = $selection !== null &&
} (!is_array($selection) && !strcmp($key, $selection)
if (isset($options[$key])) { || is_array($selection) && in_array($key, $selection));
$attributes = array_merge($attributes, $options[$key]); $lines[] = static::tag('option', strtr(static::encode($value), ' ', '&nbsp;'), $attrs);
}
$content .= static::tag('option', $attributes, $raw ? (string)$value : static::encode((string)$value)) . "\n";
} }
} }
unset($htmlOptions['key']); return implode("\n", $lines);
return $content;
} }
/** /**
* Renders the HTML tag attributes. * Renders the HTML tag attributes.
* Since version 1.1.5, attributes whose value is null will not be rendered. * Boolean attributes such as s 'checked', 'disabled', 'readonly', will be handled specially
* Special attributes, such as 'checked', 'disabled', 'readonly', will be rendered * according to [[booleanAttributes]] and [[showBooleanAttributeValues]].
* properly based on their corresponding boolean value. * @param array $attributes attributes to be rendered. The attribute values will be HTML-encoded using [[encode()]].
* @param array $attributes attributes to be rendered * Attributes whose value is null will be ignored and not put in the rendering result.
* @return string the rendering result * @return string the rendering result. If the attributes are not empty, they will be rendered
* into a string with a leading white space (such that it can be directly appended to the tag name
* in a tag. If there is no attribute, an empty string will be returned.
*/ */
public static function renderAttributes($attributes) public static function renderAttributes($attributes)
{ {
static $specialAttributes = array( if (count($attributes) > 1) {
'async' => 1, $sorted = array();
'autofocus' => 1, foreach (static::$attributeOrder as $name) {
'autoplay' => 1, if (isset($attributes[$name])) {
'checked' => 1, $sorted[$name] = $attributes[$name];
'controls' => 1,
'declare' => 1,
'default' => 1,
'defer' => 1,
'disabled' => 1,
'formnovalidate' => 1,
'hidden' => 1,
'ismap' => 1,
'loop' => 1,
'multiple' => 1,
'muted' => 1,
'nohref' => 1,
'noresize' => 1,
'novalidate' => 1,
'open' => 1,
'readonly' => 1,
'required' => 1,
'reversed' => 1,
'scoped' => 1,
'seamless' => 1,
'selected' => 1,
'typemustmatch' => 1,
);
if ($attributes === array()) {
return '';
} }
}
$html = ''; $attributes = array_merge($sorted, $attributes);
if (isset($attributes['encode'])) {
$raw = !$attributes['encode'];
unset($attributes['encode']);
} else {
$raw = false;
} }
$html = '';
foreach ($attributes as $name => $value) { foreach ($attributes as $name => $value) {
if (isset($specialAttributes[$name])) { if (isset(static::$booleanAttributes[strtolower($name)])) {
if ($value) { if ($value || strcasecmp($name, $value) === 0) {
$html .= ' ' . $name; $html .= static::$showBooleanAttributeValues ? " $name=\"$name\"" : " $name";
if (static::$renderSpecialAttributesValue) {
$html .= '="' . $name . '"';
}
} }
} elseif ($value !== null) { } elseif ($value !== null) {
$html .= ' ' . $name . '="' . ($raw ? $value : static::encode($value)) . '"'; $html .= " $name=\"" . static::encode($value) . '"';
} }
} }
return $html; return $html;
} }
/**
* Normalizes the input parameter to be a valid URL.
*
* If the input parameter
*
* - is an empty string: the currently requested URL will be returned;
* - is a non-empty string: it will be processed by [[Yii::getAlias()]] which, if the string is an alias,
* will be resolved into a URL;
* - is an array: the first array element is considered a route, while the rest of the name-value
* pairs are considered as the parameters to be used for URL creation using [[\yii\base\Application::createUrl()]].
* Here are some examples: `array('post/index', 'page' => 2)`, `array('index')`.
*
* @param array|string $url the parameter to be used to generate a valid URL
* @return string the normalized URL
* @throws InvalidParamException if the parameter is invalid.
*/
public static function url($url)
{
if (is_array($url)) {
if (isset($url[0])) {
return Yii::$app->createUrl($url[0], array_splice($url, 1));
} else {
throw new InvalidParamException('The array specifying a URL must contain at least one element.');
}
} elseif ($url === '') {
return Yii::$app->getRequest()->getUrl();
} else {
return Yii::getAlias($url);
}
}
} }
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
namespace yii\web; namespace yii\web;
use yii\base\InvalidParamException;
/** /**
* Application is the base class for all application classes. * Application is the base class for all application classes.
...@@ -62,11 +63,48 @@ class Application extends \yii\base\Application ...@@ -62,11 +63,48 @@ class Application extends \yii\base\Application
} }
/** /**
* @return UrlManager * Creates a URL using the given route and parameters.
*
* This method first normalizes the given route by converting a relative route into an absolute one.
* A relative route is a route without slash. If the route is an empty string, it stands for
* the route of the currently active [[controller]]. If the route is not empty, it stands for
* an action ID of the [[controller]].
*
* After normalizing the route, this method calls [[\yii\web\UrlManager::createUrl()]]
* to create a relative URL.
*
* @param string $route the route. This can be either an absolute or a relative route.
* @param array $params the parameters (name-value pairs) to be included in the generated URL
* @return string the created URL
* @throws InvalidParamException if a relative route is given and there is no active controller.
* @see createAbsoluteUrl
*/ */
public function getUrlManager() public function createUrl($route, $params = array())
{ {
return $this->getComponent('urlManager'); if (strpos($route, '/') === false) {
// a relative route
if ($this->controller !== null) {
$route = $route === '' ? $this->controller->route : $this->controller->uniqueId . '/' . $route;
} else {
throw new InvalidParamException('No active controller exists for resolving a relative route.');
}
}
return $this->getUrlManager()->createUrl($route, $params);
}
/**
* Creates an absolute URL using the given route and parameters.
* This method first calls [[createUrl()]] to create a relative URL.
* It then prepends [[\yii\web\UrlManager::hostInfo]] to the URL to form an absolute one.
* @param string $route the route. This can be either an absolute or a relative route.
* See [[createUrl()]] for more details.
* @param array $params the parameters (name-value pairs)
* @return string the created URL
* @see createUrl
*/
public function createAbsoluteUrl($route, $params = array())
{
return $this->getUrlManager()->getHostInfo() . $this->createUrl($route, $params);
} }
/** /**
...@@ -86,9 +124,6 @@ class Application extends \yii\base\Application ...@@ -86,9 +124,6 @@ class Application extends \yii\base\Application
'session' => array( 'session' => array(
'class' => 'yii\web\Session', 'class' => 'yii\web\Session',
), ),
'urlManager' => array(
'class' => 'yii\web\UrlManager',
),
)); ));
} }
} }
...@@ -16,4 +16,8 @@ namespace yii\web; ...@@ -16,4 +16,8 @@ namespace yii\web;
*/ */
class Controller extends \yii\base\Controller class Controller extends \yii\base\Controller
{ {
public function createUrl($route, $params = array())
{
}
} }
\ No newline at end of file
...@@ -368,7 +368,7 @@ class Request extends \yii\base\Request ...@@ -368,7 +368,7 @@ class Request extends \yii\base\Request
*/ */
protected function resolvePathInfo() protected function resolvePathInfo()
{ {
$pathInfo = $this->getRequestUri(); $pathInfo = $this->getUrl();
if (($pos = strpos($pathInfo, '?')) !== false) { if (($pos = strpos($pathInfo, '?')) !== false) {
$pathInfo = substr($pathInfo, 0, $pos); $pathInfo = substr($pathInfo, 0, $pos);
...@@ -407,42 +407,41 @@ class Request extends \yii\base\Request ...@@ -407,42 +407,41 @@ class Request extends \yii\base\Request
} }
/** /**
* Returns the currently requested URL. * Returns the currently requested absolute URL.
* This is a shortcut to the concatenation of [[hostInfo]] and [[requestUri]]. * This is a shortcut to the concatenation of [[hostInfo]] and [[url]].
* @return string the currently requested URL. * @return string the currently requested absolute URL.
*/ */
public function getUrl() public function getAbsoluteUrl()
{ {
return $this->getHostInfo() . $this->getRequestUri(); return $this->getHostInfo() . $this->getUrl();
} }
private $_requestUri; private $_url;
/** /**
* Returns the portion after [[hostInfo]] for the currently requested URL. * Returns the currently requested relative URL.
* This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any. * This refers to the portion of the URL that is after the [[hostInfo]] part.
* The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. * It includes the [[queryString]] part if any.
* @return string the request URI portion for the currently requested URL. * @return string the currently requested relative URL. Note that the URI returned is URL-encoded.
* Note that the URI returned is URL-encoded. * @throws InvalidConfigException if the URL cannot be determined due to unusual server configuration
* @throws InvalidConfigException if the request URI cannot be determined due to unusual server configuration
*/ */
public function getRequestUri() public function getUrl()
{ {
if ($this->_requestUri === null) { if ($this->_url === null) {
$this->_requestUri = $this->resolveRequestUri(); $this->_url = $this->resolveRequestUri();
} }
return $this->_requestUri; return $this->_url;
} }
/** /**
* Sets the currently requested URI. * Sets the currently requested relative URL.
* The URI must refer to the portion that is after [[hostInfo]]. * The URI must refer to the portion that is after [[hostInfo]].
* Note that the URI should be URL-encoded. * Note that the URI should be URL-encoded.
* @param string $value the request URI to be set * @param string $value the request URI to be set
*/ */
public function setRequestUri($value) public function setUrl($value)
{ {
$this->_requestUri = $value; $this->_url = $value;
} }
/** /**
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
* backend-specific unit tests * backend-specific unit tests
* dependency unit tests * dependency unit tests
- validators - validators
* Refactor validators to add validateValue() for every validator, if possible. Check if value is an array.
* FileValidator: depends on CUploadedFile * FileValidator: depends on CUploadedFile
* CaptchaValidator: depends on CaptchaAction * CaptchaValidator: depends on CaptchaAction
* DateValidator: should we use CDateTimeParser, or simply use strtotime()? * DateValidator: should we use CDateTimeParser, or simply use strtotime()?
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment