Browse Source

ActiveForm WIP

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
b71e83016f
  1. 40
      framework/base/Model.php
  2. 46
      framework/util/Html.php
  3. 100
      framework/widgets/ActiveForm.php

40
framework/base/Model.php

@ -420,12 +420,31 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
} }
/** /**
* Returns the first error of every attribute in the model.
* @return array the first errors. An empty array will be returned if there is no error.
*/
public function getFirstErrors()
{
if (empty($this->_errors)) {
return array();
} else {
$errors = array();
foreach ($this->_errors as $errors) {
if (isset($errors[0])) {
$errors[] = $errors[0];
}
}
}
return $errors;
}
/**
* Returns the first error of the specified attribute. * Returns the first error of the specified attribute.
* @param string $attribute attribute name. * @param string $attribute attribute name.
* @return string the error message. Null is returned if no error. * @return string the error message. Null is returned if no error.
* @see getErrors * @see getErrors
*/ */
public function getError($attribute) public function getFirstError($attribute)
{ {
return isset($this->_errors[$attribute]) ? reset($this->_errors[$attribute]) : null; return isset($this->_errors[$attribute]) ? reset($this->_errors[$attribute]) : null;
} }
@ -441,25 +460,6 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
} }
/** /**
* Adds a list of errors.
* @param array $errors a list of errors. The array keys must be attribute names.
* The array values should be error messages. If an attribute has multiple errors,
* these errors must be given in terms of an array.
*/
public function addErrors($errors)
{
foreach ($errors as $attribute => $error) {
if (is_array($error)) {
foreach ($error as $e) {
$this->_errors[$attribute][] = $e;
}
} else {
$this->_errors[$attribute][] = $error;
}
}
}
/**
* Removes errors for all attributes or a single attribute. * Removes errors for all attributes or a single attribute.
* @param string $attribute attribute name. Use null to remove errors for all attribute. * @param string $attribute attribute name. Use null to remove errors for all attribute.
*/ */

46
framework/util/Html.php

@ -152,7 +152,7 @@ class Html
* @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. * If this is coming from end users, you should consider [[encode()]] it to prevent XSS attacks.
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated HTML tag * @return string the generated HTML tag
@ -172,7 +172,7 @@ class Html
/** /**
* Generates a start tag. * Generates a start tag.
* @param string $name the tag name * @param string $name the tag name
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated start tag * @return string the generated start tag
@ -209,7 +209,7 @@ class Html
/** /**
* Generates a style tag. * Generates a style tag.
* @param string $content the style content * @param string $content the style content
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* If the options does not contain "type", a "type" attribute with value "text/css" will be used. * If the options does not contain "type", a "type" attribute with value "text/css" will be used.
@ -226,7 +226,7 @@ class Html
/** /**
* Generates a script tag. * Generates a script tag.
* @param string $content the script content * @param string $content the script content
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* If the options does not contain "type", a "type" attribute with value "text/javascript" will be rendered. * If the options does not contain "type", a "type" attribute with value "text/javascript" will be rendered.
@ -243,7 +243,7 @@ class Html
/** /**
* Generates a link tag that refers to an external CSS file. * Generates a link tag that refers to an external CSS file.
* @param array|string $url the URL of the external CSS file. This parameter will be processed by [[url()]]. * @param array|string $url the URL of the external CSS file. This parameter will be processed by [[url()]].
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated link tag * @return string the generated link tag
@ -260,7 +260,7 @@ class Html
/** /**
* Generates a script tag that refers to an external JavaScript file. * Generates a script tag that refers to an external JavaScript file.
* @param string $url the URL of the external JavaScript file. This parameter will be processed by [[url()]]. * @param string $url the URL of the external JavaScript file. This parameter will be processed by [[url()]].
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated script tag * @return string the generated script tag
@ -276,8 +276,8 @@ class Html
/** /**
* Generates a form start tag. * Generates a form start tag.
* @param array|string $action the form action URL. This parameter will be processed by [[url()]]. * @param array|string $action the form action URL. This parameter will be processed by [[url()]].
* @param string $method form method, either "post" or "get" (case-insensitive) * @param string $method the form submission method, either "post" or "get" (case-insensitive)
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated form start tag. * @return string the generated form start tag.
@ -329,7 +329,7 @@ class Html
* @param array|string|null $url the URL for the hyperlink tag. This parameter will be processed by [[url()]] * @param array|string|null $url the URL for the hyperlink tag. This parameter will be processed by [[url()]]
* and will be used for the "href" attribute of the tag. If this parameter is null, the "href" attribute * and will be used for the "href" attribute of the tag. If this parameter is null, the "href" attribute
* will not be generated. * will not be generated.
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated hyperlink * @return string the generated hyperlink
@ -350,7 +350,7 @@ class Html
* it to prevent XSS attacks. * it to prevent XSS attacks.
* @param string $email email address. If this is null, the first parameter (link body) will be treated * @param string $email email address. If this is null, the first parameter (link body) will be treated
* as the email address and used. * as the email address and used.
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated mailto link * @return string the generated mailto link
@ -363,7 +363,7 @@ class Html
/** /**
* Generates an image tag. * Generates an image tag.
* @param string $src the image URL. This parameter will be processed by [[url()]]. * @param string $src the image URL. This parameter will be processed by [[url()]].
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated image tag * @return string the generated image tag
@ -384,7 +384,7 @@ class Html
* it to prevent XSS attacks. * it to prevent XSS attacks.
* @param string $for the ID of the HTML element that this label is associated with. * @param string $for the ID of the HTML element that this label is associated with.
* If this is null, the "for" attribute will not be generated. * If this is null, the "for" attribute will not be generated.
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated label tag * @return string the generated label tag
@ -402,7 +402,7 @@ class Html
* @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded. * @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, * 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. * you should consider [[encode()]] it to prevent XSS attacks.
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* If the options does not contain "type", a "type" attribute with value "button" will be rendered. * If the options does not contain "type", a "type" attribute with value "button" will be rendered.
@ -425,7 +425,7 @@ class Html
* @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded. * @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, * 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. * you should consider [[encode()]] it to prevent XSS attacks.
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated submit button tag * @return string the generated submit button tag
@ -443,7 +443,7 @@ class Html
* @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded. * @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, * 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. * you should consider [[encode()]] it to prevent XSS attacks.
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated reset button tag * @return string the generated reset button tag
@ -459,7 +459,7 @@ class Html
* @param string $type the type attribute. * @param string $type the type attribute.
* @param string $name the name attribute. If it is null, the name attribute will not be generated. * @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 $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated input tag * @return string the generated input tag
@ -476,7 +476,7 @@ class Html
* Generates an input button. * Generates an input button.
* @param string $name the name attribute. * @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated. * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated button tag * @return string the generated button tag
@ -490,7 +490,7 @@ class Html
* Generates a submit input button. * Generates a submit input button.
* @param string $name the name attribute. If it is null, the name attribute will not be generated. * @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 $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated button tag * @return string the generated button tag
@ -517,7 +517,7 @@ class Html
* Generates a text input field. * Generates a text input field.
* @param string $name the name attribute. * @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated. * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated button tag * @return string the generated button tag
@ -531,7 +531,7 @@ class Html
* Generates a hidden input field. * Generates a hidden input field.
* @param string $name the name attribute. * @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated. * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated button tag * @return string the generated button tag
@ -545,7 +545,7 @@ class Html
* Generates a password input field. * Generates a password input field.
* @param string $name the name attribute. * @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated. * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated button tag * @return string the generated button tag
@ -562,7 +562,7 @@ class Html
* can be obtained via $_FILES[$name] (see PHP documentation). * can be obtained via $_FILES[$name] (see PHP documentation).
* @param string $name the name attribute. * @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated. * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated button tag * @return string the generated button tag
@ -576,7 +576,7 @@ class Html
* 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. Note that it will be encoded using [[encode()]]. * @param string $value the input value. Note that it will be encoded using [[encode()]].
* @param array $options the tag options in terms of name-value pairs. These be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated text area tag * @return string the generated text area tag

100
framework/widgets/ActiveForm.php

@ -7,7 +7,11 @@
namespace yii\widgets; namespace yii\widgets;
use Yii;
use yii\base\Widget; use yii\base\Widget;
use yii\base\Model;
use yii\util\Html;
use yii\util\ArrayHelper;
/** /**
* ActiveForm ... * ActiveForm ...
@ -18,8 +22,7 @@ use yii\base\Widget;
class ActiveForm extends Widget class ActiveForm extends Widget
{ {
/** /**
* @var mixed the form action URL (see {@link CHtml::normalizeUrl} for details about this parameter). * @param array|string $action the form action URL. This parameter will be processed by [[\yii\util\Html::url()]].
* If not set, the current page URL is used.
*/ */
public $action = ''; public $action = '';
/** /**
@ -28,49 +31,88 @@ class ActiveForm extends Widget
*/ */
public $method = 'post'; public $method = 'post';
/** /**
* @var string the CSS class name for error messages. Defaults to 'errorMessage'. * @var string the default CSS class for the error summary container.
* Individual {@link error} call may override this value by specifying the 'class' HTML option. * @see errorSummary()
*/ */
public $errorMessageCssClass = 'errorMessage'; public $errorSummaryClass = 'yii-error-summary';
/** /**
* @var array additional HTML attributes that should be rendered for the form tag. * @var string the default CSS class that indicates an input has error.
* This is
*/ */
public $htmlOptions = array(); public $errorClass = 'yii-error';
/** public $successClass = 'yii-success';
* @var boolean whether to enable data validation via AJAX. Defaults to false. public $validatingClass = 'yii-validating';
* When this property is set true, you should respond to the AJAX validation request on the server side as shown below:
* <pre>
* public function actionCreate()
* {
* $model=new User;
* if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
* {
* echo CActiveForm::validate($model);
* Yii::app()->end();
* }
* ......
* }
* </pre>
*/
public $enableAjaxValidation = false;
/** /**
* @var boolean whether to enable client-side data validation. Defaults to false. * @var boolean whether to enable client-side data validation. Defaults to false.
*
* When this property is set true, client-side validation will be performed by validators * When this property is set true, client-side validation will be performed by validators
* that support it (see {@link CValidator::enableClientValidation} and {@link CValidator::clientValidateAttribute}). * that support it (see {@link CValidator::enableClientValidation} and {@link CValidator::clientValidateAttribute}).
*
* @see error
* @since 1.1.7
*/ */
public $enableClientValidation = false; public $enableClientValidation = false;
public $options = array();
public function errorSummary($model, $options = array()) /**
* @param Model|Model[] $models
* @param array $options
* @return string
*/
public function errorSummary($models, $options = array())
{ {
if (!is_array($models)) {
$models = array($models);
}
$showAll = isset($options['showAll']) && $options['showAll']);
$lines = array();
/** @var $model Model */
foreach ($models as $model) {
if ($showAll) {
foreach ($model->getErrors() as $errors) {
$lines = array_merge($lines, $errors);
} }
} else {
$lines = array_merge($lines, $model->getFirstErrors());
}
}
$header = isset($options['header']) ? $options['header'] : '<p>' . Yii::t('yii|Please fix the following errors:') . '</p>';
$footer = isset($options['footer']) ? $options['footer'] : '';
$container = isset($options['container']) ? $options['container'] : 'div';
unset($options['showAll'], $options['header'], $options['footer'], $options['container']);
if (!isset($options['class'])) {
$options['class'] = $this->errorSummaryClass;
}
if ($lines !== array()) {
$content = "<ul><li>" . implode("</li>\n<li>", ArrayHelper::htmlEncode($lines)) . "</li><ul>";
return Html::tag($container, $header . $content . $footer, $options);
} else {
$content = "<ul></ul>";
$options['style'] = isset($options['style']) ? rtrim($options['style'], ';') . '; display:none' : 'display:none';
return Html::tag($container, $header . $content . $footer, $options);
}
}
/**
* @param Model $model
* @param string $attribute
* @param array $options
* @return string
*/
public function error($model, $attribute, $options = array()) public function error($model, $attribute, $options = array())
{ {
self::resolveName($model, $attribute); // turn [a][b]attr into attr
$container = isset($options['container']) ? $options['container'] : 'div';
unset($options['container']);
$error = $model->getFirstError($attribute);
return Html::tag($container, Html::encode($error), $options);
}
public function resolveAttributeName($name)
{
} }
public function label($model, $attribute, $options = array()) public function label($model, $attribute, $options = array())

Loading…
Cancel
Save