Commit b71e8301 by Qiang Xue

ActiveForm WIP

parent c69e6180
......@@ -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.
* @param string $attribute attribute name.
* @return string the error message. Null is returned if no error.
* @see getErrors
*/
public function getError($attribute)
public function getFirstError($attribute)
{
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.
* @param string $attribute attribute name. Use null to remove errors for all attribute.
*/
......
......@@ -7,7 +7,11 @@
namespace yii\widgets;
use Yii;
use yii\base\Widget;
use yii\base\Model;
use yii\util\Html;
use yii\util\ArrayHelper;
/**
* ActiveForm ...
......@@ -18,8 +22,7 @@ use yii\base\Widget;
class ActiveForm extends Widget
{
/**
* @var mixed the form action URL (see {@link CHtml::normalizeUrl} for details about this parameter).
* If not set, the current page URL is used.
* @param array|string $action the form action URL. This parameter will be processed by [[\yii\util\Html::url()]].
*/
public $action = '';
/**
......@@ -28,49 +31,88 @@ class ActiveForm extends Widget
*/
public $method = 'post';
/**
* @var string the CSS class name for error messages. Defaults to 'errorMessage'.
* Individual {@link error} call may override this value by specifying the 'class' HTML option.
* @var string the default CSS class for the error summary container.
* @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();
/**
* @var boolean whether to enable data validation via AJAX. Defaults to false.
* 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;
public $errorClass = 'yii-error';
public $successClass = 'yii-success';
public $validatingClass = 'yii-validating';
/**
* @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
* that support it (see {@link CValidator::enableClientValidation} and {@link CValidator::clientValidateAttribute}).
*
* @see error
* @since 1.1.7
*/
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())
{
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())
......
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