diff --git a/framework/widgets/ActiveForm.php b/framework/widgets/ActiveForm.php
index b8c8f2c..2d47253 100644
--- a/framework/widgets/ActiveForm.php
+++ b/framework/widgets/ActiveForm.php
@@ -8,6 +8,7 @@
namespace yii\widgets;
use Yii;
+use yii\base\InvalidParamException;
use yii\base\Widget;
use yii\base\Model;
use yii\util\Html;
@@ -35,6 +36,7 @@ class ActiveForm extends Widget
* @see errorSummary()
*/
public $errorSummaryClass = 'yii-error-summary';
+ public $errorMessageClass = 'yii-error-message';
/**
* @var string the default CSS class that indicates an input has error.
* This is
@@ -50,7 +52,10 @@ class ActiveForm extends Widget
public $enableClientValidation = false;
public $options = array();
-
+ /**
+ * @var array model-class mapped to name prefix
+ */
+ public $modelMap;
/**
* @param Model|Model[] $models
@@ -78,20 +83,22 @@ class ActiveForm extends Widget
$header = isset($options['header']) ? $options['header'] : '
' . Yii::t('yii|Please fix the following errors:') . '
';
$footer = isset($options['footer']) ? $options['footer'] : '';
- $container = isset($options['container']) ? $options['container'] : 'div';
+ $tag = isset($options['tag']) ? $options['tag'] : 'div';
unset($options['showAll'], $options['header'], $options['footer'], $options['container']);
if (!isset($options['class'])) {
$options['class'] = $this->errorSummaryClass;
+ } else {
+ $options['class'] .= ' ' . $this->errorSummaryClass;
}
if ($lines !== array()) {
$content = "- " . implode("
\n- ", ArrayHelper::htmlEncode($lines)) . "
";
- return Html::tag($container, $header . $content . $footer, $options);
+ return Html::tag($tag, $header . $content . $footer, $options);
} else {
$content = "";
$options['style'] = isset($options['style']) ? rtrim($options['style'], ';') . '; display:none' : 'display:none';
- return Html::tag($container, $header . $content . $footer, $options);
+ return Html::tag($tag, $header . $content . $footer, $options);
}
}
@@ -103,25 +110,32 @@ class ActiveForm extends Widget
*/
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']);
+ $attribute = $this->normalizeAttributeName($attribute);
+ $this->getInputName($model, $attribute);
+ $tag = isset($options['tag']) ? $options['tag'] : 'div';
+ unset($options['tag']);
$error = $model->getFirstError($attribute);
- return Html::tag($container, Html::encode($error), $options);
- }
-
- public function resolveAttributeName($name)
- {
-
+ return Html::tag($tag, Html::encode($error), $options);
}
+ /**
+ * @param Model $model
+ * @param string $attribute
+ * @param array $options
+ * @return string
+ */
public function label($model, $attribute, $options = array())
{
+ $attribute = $this->normalizeAttributeName($attribute);
+ $label = $model->getAttributeLabel($attribute);
+ return Html::label(Html::encode($label), isset($options['for']) ? $options['for'] : null, $options);
}
public function input($type, $model, $attribute, $options = array())
{
- return '';
+ $value = $this->getAttributeValue($model, $attribute);
+ $name = $this->getInputName($model, $attribute);
+ return Html::input($type, $name, $value, $options);
}
public function textInput($model, $attribute, $options = array())
@@ -146,29 +160,119 @@ class ActiveForm extends Widget
public function textarea($model, $attribute, $options = array())
{
+ $value = $this->getAttributeValue($model, $attribute);
+ $name = $this->getInputName($model, $attribute);
+ return Html::textarea($name, $value, $options);
}
public function radio($model, $attribute, $value = '1', $options = array())
{
+ $checked = $this->getAttributeValue($model, $attribute);
+ $name = $this->getInputName($model, $attribute);
+ if (!array_key_exists('uncheck', $options)) {
+ $options['unchecked'] = '0';
+ }
+ return Html::radio($name, $checked, $value, $options);
}
public function checkbox($model, $attribute, $value = '1', $options = array())
{
+ $checked = $this->getAttributeValue($model, $attribute);
+ $name = $this->getInputName($model, $attribute);
+ if (!array_key_exists('uncheck', $options)) {
+ $options['unchecked'] = '0';
+ }
+ return Html::checkbox($name, $checked, $value, $options);
}
public function dropDownList($model, $attribute, $items, $options = array())
{
+ $checked = $this->getAttributeValue($model, $attribute);
+ $name = $this->getInputName($model, $attribute);
+ return Html::dropDownList($name, $checked, $items, $options);
}
public function listBox($model, $attribute, $items, $options = array())
{
+ $checked = $this->getAttributeValue($model, $attribute);
+ $name = $this->getInputName($model, $attribute);
+ if (!array_key_exists('unselect', $options)) {
+ $options['unselect'] = '0';
+ }
+ return Html::listBox($name, $checked, $items, $options);
}
public function checkboxList($model, $attribute, $items, $options = array())
{
+ $checked = $this->getAttributeValue($model, $attribute);
+ $name = $this->getInputName($model, $attribute);
+ if (!array_key_exists('unselect', $options)) {
+ $options['unselect'] = '0';
+ }
+ return Html::checkboxList($name, $checked, $items, $options);
}
public function radioList($model, $attribute, $items, $options = array())
{
+ $checked = $this->getAttributeValue($model, $attribute);
+ $name = $this->getInputName($model, $attribute);
+ if (!array_key_exists('unselect', $options)) {
+ $options['unselect'] = '0';
+ }
+ return Html::radioList($name, $checked, $items, $options);
+ }
+
+ public function getInputName($model, $attribute)
+ {
+ $class = get_class($model);
+ if (isset($this->modelMap[$class])) {
+ $class = $this->modelMap[$class];
+ } elseif (($pos = strrpos($class, '\\')) !== false) {
+ $class = substr($class, $pos);
+ }
+ if (!preg_match('/(^|.*\])(\w+)(\[.*|$)/', $attribute, $matches)) {
+ throw new InvalidParamException('Attribute name must contain word characters only.');
+ }
+ $prefix = $matches[1];
+ $attribute = $matches[2];
+ $suffix = $matches[3];
+ if ($class === '' && $prefix === '') {
+ return $attribute . $suffix;
+ } elseif ($class !== '') {
+ return $class . $prefix . "[$attribute]" . $suffix;
+ } else {
+ throw new InvalidParamException('Model name cannot be mapped to empty for tabular inputs.');
+ }
+ }
+
+ public function getAttributeValue($model, $attribute)
+ {
+ if (!preg_match('/(^|.*\])(\w+)(\[.*|$)/', $attribute, $matches)) {
+ throw new InvalidParamException('Attribute name must contain word characters only.');
+ }
+ $attribute = $matches[2];
+ $index = $matches[3];
+ if ($index === '') {
+ return $model->$attribute;
+ } else {
+ $value = $model->$attribute;
+ foreach (explode('][', trim($index, '[]')) as $id) {
+ if ((is_array($value) || $value instanceof \ArrayAccess) && isset($value[$id])) {
+ $value = $value[$id];
+ } else {
+ return null;
+ }
+ }
+ return $value;
+ }
+ }
+
+ public function normalizeAttributeName($attribute)
+ {
+ if (preg_match('/(^|.*\])(\w+)(\[.*|$)/', $attribute, $matches)) {
+ return $matches[2];
+ } else {
+ throw new InvalidParamException('Attribute name must contain word characters only.');
+ }
}
}