Commit 784bdb32 by Qiang Xue

refactoring activeform.

parent 90cfb6eb
......@@ -8,6 +8,7 @@
namespace yii\widgets;
use Yii;
use yii\base\InvalidCallException;
use yii\base\InvalidParamException;
use yii\base\Widget;
use yii\base\Model;
......@@ -31,25 +32,26 @@ class ActiveForm extends Widget
* Defaults to 'post'.
*/
public $method = 'post';
public $options = array();
public $errorOptions = array('tag' => 'div', 'class' => 'yii-error-message');
public $labelOptions = array('class' => 'yii-input-label');
/**
* @var string the default CSS class for the error summary container.
* @see errorSummary()
*/
public $errorSummaryClass = 'yii-error-summary';
public $errorMessageClass = 'yii-error-message';
public $errorSummaryCssClass = 'yii-error-summary';
/**
* @var string the default CSS class that indicates an input has error.
*/
public $errorClass = 'yii-error';
public $errorCssClass = 'yii-error';
/**
* @var string the default CSS class that indicates an input validated successfully.
*/
public $successClass = 'yii-success';
public $successCssClass = 'yii-success';
/**
* @var string the default CSS class that indicates an input is currently being validated.
*/
public $validatingClass = 'yii-validating';
public $validatingCssClass = '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
......@@ -57,9 +59,7 @@ class ActiveForm extends Widget
*/
public $enableClientValidation = false;
public $options = array();
public $fieldClass = 'yii\widgets\ActiveField';
/**
* Initializes the widget.
* This renders the form open tag.
......@@ -108,9 +108,9 @@ class ActiveForm extends Widget
unset($options['showAll'], $options['header'], $options['footer'], $options['container']);
if (!isset($options['class'])) {
$options['class'] = $this->errorSummaryClass;
$options['class'] = $this->errorSummaryCssClass;
} else {
$options['class'] .= ' ' . $this->errorSummaryClass;
$options['class'] .= ' ' . $this->errorSummaryCssClass;
}
if ($lines !== array()) {
......@@ -124,209 +124,30 @@ class ActiveForm extends Widget
}
/**
* @param Model $model
* @param string $attribute
* @param array $options
* @return string
*/
public function error($model, $attribute, $options = array())
{
$attribute = $this->getAttributeName($attribute);
$tag = isset($options['tag']) ? $options['tag'] : 'div';
unset($options['tag']);
$error = $model->getFirstError($attribute);
return Html::tag($tag, Html::encode($error), $options);
}
/**
* @param Model $model
* @param string $attribute
* @param array $options
* @return string
* @var ActiveField[]
*/
public function label($model, $attribute, $options = array())
{
$attribute = $this->getAttributeName($attribute);
$label = isset($options['label']) ? $options['label'] : Html::encode($model->getAttributeLabel($attribute));
$for = array_key_exists('for', $options) ? $options['for'] : $this->getInputId($model, $attribute);
return Html::label($label, $for, $options);
}
private $_fieldStack = array();
/**
* @param string $type
* @param Model $model
* @param string $attribute
* @param array $options
*
* @return string
*/
public function input($type, $model, $attribute, $options = array())
public function beginField($model, $attribute, $options = array())
{
$value = $this->getAttributeValue($model, $attribute);
$name = $this->getInputName($model, $attribute);
if (!array_key_exists('id', $options)) {
$options['id'] = $this->getInputId($model, $attribute);
}
return Html::input($type, $name, $value, $options);
}
public function textInput($model, $attribute, $options = array())
{
return $this->input('text', $model, $attribute, $options);
}
public function hiddenInput($model, $attribute, $options = array())
{
return $this->input('hidden', $model, $attribute, $options);
}
public function passwordInput($model, $attribute, $options = array())
{
return $this->input('password', $model, $attribute, $options);
}
public function fileInput($model, $attribute, $options = array())
{
return $this->input('file', $model, $attribute, $options);
}
public function textarea($model, $attribute, $options = array())
{
$value = $this->getAttributeValue($model, $attribute);
$name = $this->getInputName($model, $attribute);
if (!array_key_exists('id', $options)) {
$options['id'] = $this->getInputId($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';
}
if (!array_key_exists('id', $options)) {
$options['id'] = $this->getInputId($model, $attribute);
}
return Html::radio($name, $checked, $value, $options);
$field = Yii::createObject(array(
'class' => $this->fieldClass,
'model' => $model,
'attribute' => $attribute,
'form' => $this,
'options' => $options,
));
echo $field->begin();
return $this->_fieldStack[] = $field;
}
public function checkbox($model, $attribute, $value = '1', $options = array())
public function endField()
{
$checked = $this->getAttributeValue($model, $attribute);
$name = $this->getInputName($model, $attribute);
if (!array_key_exists('uncheck', $options)) {
$options['unchecked'] = '0';
}
if (!array_key_exists('id', $options)) {
$options['id'] = $this->getInputId($model, $attribute);
}
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);
if (!array_key_exists('id', $options)) {
$options['id'] = $this->getInputId($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';
}
if (!array_key_exists('id', $options)) {
$options['id'] = $this->getInputId($model, $attribute);
}
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 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;
if ($this->_fieldStack !== array()) {
$field = array_pop($this->_fieldStack);
echo $field->end();
} 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;
throw new InvalidCallException('The "beginField" and "endField" calls are not matching.');
}
}
public function getAttributeName($attribute)
{
if (preg_match('/(^|.*\])(\w+)(\[.*|$)/', $attribute, $matches)) {
return $matches[2];
} else {
throw new InvalidParamException('Attribute name must contain word characters only.');
}
}
/**
* @param Model $model
* @param string $attribute
* @return string
* @throws \yii\base\InvalidParamException
*/
public static function getInputName($model, $attribute)
{
$formName = $model->formName();
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 ($formName === '' && $prefix === '') {
return $attribute . $suffix;
} elseif ($formName !== '') {
return $formName . $prefix . "[$attribute]" . $suffix;
} else {
throw new InvalidParamException(get_class($model) . '::formName() cannot be empty for tabular inputs.');
}
}
public static function getInputId($model, $attribute)
{
$name = static::getInputName($model, $attribute);
return str_replace(array('[]', '][', '[', ']', ' '), array('', '-', '-', '', '-'), $name);
}
}
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