You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
9.4 KiB

<?php
12 years ago
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
12 years ago
namespace yii\widgets;
12 years ago
use Yii;
12 years ago
use yii\base\InvalidParamException;
12 years ago
use yii\base\Widget;
12 years ago
use yii\base\Model;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
12 years ago
/**
* ActiveForm ...
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ActiveForm extends Widget
{
12 years ago
/**
* @param array|string $action the form action URL. This parameter will be processed by [[\yii\helpers\Html::url()]].
12 years ago
*/
public $action = '';
/**
* @var string the form submission method. This should be either 'post' or 'get'.
* Defaults to 'post'.
*/
public $method = 'post';
/**
12 years ago
* @var string the default CSS class for the error summary container.
* @see errorSummary()
12 years ago
*/
12 years ago
public $errorSummaryClass = 'yii-error-summary';
12 years ago
public $errorMessageClass = 'yii-error-message';
12 years ago
/**
12 years ago
* @var string the default CSS class that indicates an input has error.
12 years ago
*/
12 years ago
public $errorClass = 'yii-error';
12 years ago
/**
* @var string the default CSS class that indicates an input validated successfully.
*/
12 years ago
public $successClass = 'yii-success';
12 years ago
/**
* @var string the default CSS class that indicates an input is currently being validated.
*/
12 years ago
public $validatingClass = 'yii-validating';
12 years ago
/**
* @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}).
*/
public $enableClientValidation = false;
12 years ago
public $options = array();
/**
* @param Model|Model[] $models
* @param array $options
* @return string
*/
public function errorSummary($models, $options = array())
12 years ago
{
12 years ago
if (!is_array($models)) {
$models = array($models);
}
$showAll = !empty($options['showAll']);
12 years ago
$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'] : '';
12 years ago
$tag = isset($options['tag']) ? $options['tag'] : 'div';
12 years ago
unset($options['showAll'], $options['header'], $options['footer'], $options['container']);
if (!isset($options['class'])) {
$options['class'] = $this->errorSummaryClass;
12 years ago
} else {
$options['class'] .= ' ' . $this->errorSummaryClass;
12 years ago
}
if ($lines !== array()) {
$content = "<ul><li>" . implode("</li>\n<li>", ArrayHelper::htmlEncode($lines)) . "</li><ul>";
12 years ago
return Html::tag($tag, $header . $content . $footer, $options);
12 years ago
} else {
$content = "<ul></ul>";
$options['style'] = isset($options['style']) ? rtrim($options['style'], ';') . '; display:none' : 'display:none';
12 years ago
return Html::tag($tag, $header . $content . $footer, $options);
12 years ago
}
12 years ago
}
12 years ago
/**
* @param Model $model
* @param string $attribute
* @param array $options
* @return string
*/
12 years ago
public function error($model, $attribute, $options = array())
{
12 years ago
$attribute = $this->getAttributeName($attribute);
12 years ago
$tag = isset($options['tag']) ? $options['tag'] : 'div';
unset($options['tag']);
12 years ago
$error = $model->getFirstError($attribute);
12 years ago
return Html::tag($tag, Html::encode($error), $options);
12 years ago
}
12 years ago
/**
* @param Model $model
* @param string $attribute
* @param array $options
* @return string
*/
12 years ago
public function label($model, $attribute, $options = array())
{
12 years ago
$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);
12 years ago
}
12 years ago
/**
* @param string $type
* @param Model $model
* @param string $attribute
* @param array $options
*
* @return string
*/
12 years ago
public function input($type, $model, $attribute, $options = array())
{
12 years ago
$value = $this->getAttributeValue($model, $attribute);
$name = $this->getInputName($model, $attribute);
12 years ago
if (!array_key_exists('id', $options)) {
$options['id'] = $this->getInputId($model, $attribute);
}
12 years ago
return Html::input($type, $name, $value, $options);
12 years ago
}
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())
{
12 years ago
$value = $this->getAttributeValue($model, $attribute);
$name = $this->getInputName($model, $attribute);
12 years ago
if (!array_key_exists('id', $options)) {
$options['id'] = $this->getInputId($model, $attribute);
}
12 years ago
return Html::textarea($name, $value, $options);
12 years ago
}
public function radio($model, $attribute, $value = '1', $options = array())
{
12 years ago
$checked = $this->getAttributeValue($model, $attribute);
$name = $this->getInputName($model, $attribute);
if (!array_key_exists('uncheck', $options)) {
$options['unchecked'] = '0';
}
12 years ago
if (!array_key_exists('id', $options)) {
$options['id'] = $this->getInputId($model, $attribute);
}
12 years ago
return Html::radio($name, $checked, $value, $options);
12 years ago
}
public function checkbox($model, $attribute, $value = '1', $options = array())
{
12 years ago
$checked = $this->getAttributeValue($model, $attribute);
$name = $this->getInputName($model, $attribute);
if (!array_key_exists('uncheck', $options)) {
$options['unchecked'] = '0';
}
12 years ago
if (!array_key_exists('id', $options)) {
$options['id'] = $this->getInputId($model, $attribute);
}
12 years ago
return Html::checkbox($name, $checked, $value, $options);
12 years ago
}
public function dropDownList($model, $attribute, $items, $options = array())
{
12 years ago
$checked = $this->getAttributeValue($model, $attribute);
$name = $this->getInputName($model, $attribute);
12 years ago
if (!array_key_exists('id', $options)) {
$options['id'] = $this->getInputId($model, $attribute);
}
12 years ago
return Html::dropDownList($name, $checked, $items, $options);
12 years ago
}
public function listBox($model, $attribute, $items, $options = array())
{
12 years ago
$checked = $this->getAttributeValue($model, $attribute);
$name = $this->getInputName($model, $attribute);
if (!array_key_exists('unselect', $options)) {
$options['unselect'] = '0';
}
12 years ago
if (!array_key_exists('id', $options)) {
$options['id'] = $this->getInputId($model, $attribute);
}
12 years ago
return Html::listBox($name, $checked, $items, $options);
12 years ago
}
public function checkboxList($model, $attribute, $items, $options = array())
{
12 years ago
$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);
12 years ago
}
12 years ago
public function radioList($model, $attribute, $items, $options = array())
{
12 years ago
$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;
} 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;
}
}
12 years ago
public function getAttributeName($attribute)
12 years ago
{
if (preg_match('/(^|.*\])(\w+)(\[.*|$)/', $attribute, $matches)) {
return $matches[2];
} else {
throw new InvalidParamException('Attribute name must contain word characters only.');
}
12 years ago
}
/**
* @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);
}
}