Yii2 framework backup
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.

279 lines
8.3 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\util\Html;
use yii\util\ArrayHelper;
12 years ago
/**
* ActiveForm ...
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ActiveForm extends Widget
{
12 years ago
/**
12 years ago
* @param array|string $action the form action URL. This parameter will be processed by [[\yii\util\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.
* This is
12 years ago
*/
12 years ago
public $errorClass = 'yii-error';
public $successClass = 'yii-success';
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();
12 years ago
/**
* @var array model-class mapped to name prefix
*/
public $modelMap;
12 years ago
/**
* @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);
}
12 years ago
$showAll = isset($options['showAll']) && $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->normalizeAttributeName($attribute);
$this->getInputName($model, $attribute);
$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->normalizeAttributeName($attribute);
$label = $model->getAttributeLabel($attribute);
return Html::label(Html::encode($label), isset($options['for']) ? $options['for'] : null, $options);
12 years ago
}
public function input($type, $model, $attribute, $options = array())
{
12 years ago
$value = $this->getAttributeValue($model, $attribute);
$name = $this->getInputName($model, $attribute);
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);
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';
}
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';
}
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);
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';
}
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 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.');
}
12 years ago
}
}