mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 22:32:40 +08:00
Added bootstrap. form WIP
This commit is contained in:
@ -9,6 +9,7 @@ namespace yii\widgets;
|
||||
|
||||
use yii\base\Component;
|
||||
use yii\helpers\Html;
|
||||
use yii\base\Model;
|
||||
|
||||
/**
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
@ -17,15 +18,15 @@ use yii\helpers\Html;
|
||||
class ActiveField extends Component
|
||||
{
|
||||
/**
|
||||
* @var ActiveForm
|
||||
* @var ActiveForm the form that this field is associated with.
|
||||
*/
|
||||
public $form;
|
||||
/**
|
||||
* @var \yii\base\Model
|
||||
* @var Model the data model that this field is associated with
|
||||
*/
|
||||
public $model;
|
||||
/**
|
||||
* @var string
|
||||
* @var string the model attribute that this field is associated with
|
||||
*/
|
||||
public $attribute;
|
||||
/**
|
||||
@ -33,42 +34,40 @@ class ActiveField extends Component
|
||||
*/
|
||||
public $tag = 'div';
|
||||
/**
|
||||
* @var array
|
||||
* @var array the HTML attributes (name-value pairs) for the field container tag.
|
||||
* The values will be HTML-encoded using [[Html::encode()]].
|
||||
* If a value is null, the corresponding attribute will not be rendered.
|
||||
*/
|
||||
public $options = array(
|
||||
'class' => 'yii-field',
|
||||
'class' => 'control-group',
|
||||
);
|
||||
/**
|
||||
* @var string the default CSS class that indicates an input is required.
|
||||
* @var string the template that is used to arrange the label, the input and the error message.
|
||||
* The following tokens will be replaced when [[render()]] is called: `{label}`, `{input}` and `{error}`.
|
||||
*/
|
||||
public $requiredCssClass = 'required';
|
||||
public $template = "{label}\n<div class=\"controls\">\n{input}\n{error}\n</div>";
|
||||
/**
|
||||
* @var string the default CSS class that indicates an input has error.
|
||||
* @var array the default options for the error message. This property is used when calling [[error()]]
|
||||
* without the `$options` parameter.
|
||||
*/
|
||||
public $errorCssClass = 'error';
|
||||
public $errorOptions = array('tag' => 'span', 'class' => 'help-inline');
|
||||
/**
|
||||
* @var string the default CSS class that indicates an input validated successfully.
|
||||
* @var array the default options for the label. This property is used when calling [[label()]]
|
||||
* without the `$options` parameter.
|
||||
*/
|
||||
public $successCssClass = 'success';
|
||||
/**
|
||||
* @var string the default CSS class that indicates an input is currently being validated.
|
||||
*/
|
||||
public $validatingCssClass = 'validating';
|
||||
public $template = "{label}\n{input}\n{error}";
|
||||
|
||||
public $errorOptions = array('tag' => 'span', 'class' => 'yii-error-message');
|
||||
public $labelOptions = array('class' => 'control-label');
|
||||
|
||||
|
||||
public function begin()
|
||||
{
|
||||
$options = $this->options;
|
||||
$class = isset($options['class']) ? array($options['class']) : array();
|
||||
$class[] = 'field-' . Html::getInputId($this->model, $this->attribute);
|
||||
if ($this->model->isAttributeRequired($this->attribute)) {
|
||||
$class[] = $this->requiredCssClass;
|
||||
$class[] = $this->form->requiredCssClass;
|
||||
}
|
||||
if ($this->model->hasErrors($this->attribute)) {
|
||||
$class[] = $this->errorCssClass;
|
||||
$class[] = $this->form->errorCssClass;
|
||||
}
|
||||
$options['class'] = implode(' ', $class);
|
||||
return Html::beginTag($this->tag, $options);
|
||||
@ -235,27 +234,11 @@ class ActiveField extends Component
|
||||
*
|
||||
* The rest of the options will be rendered as the attributes of the resulting tag. The values will
|
||||
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
|
||||
* @param boolean $enclosedByLabel whether to enclose the radio button within the label tag.
|
||||
* If this is true, [[template]] will be ignored.
|
||||
* @return string the generated radio button tag
|
||||
*/
|
||||
public function radio($options = array(), $enclosedByLabel = true)
|
||||
public function radio($options = array())
|
||||
{
|
||||
if ($enclosedByLabel) {
|
||||
$name = isset($options['name']) ? $options['name'] : Html::getInputName($this->model, $this->attribute);
|
||||
$checked = Html::getAttributeValue($this->model, $this->attribute);
|
||||
$radio = Html::radio($name, $checked, $options);
|
||||
$uncheck = array_key_exists('unchecked', $options) ? $options['uncheck'] : '0';
|
||||
unset($options['uncheck']);
|
||||
$hidden = $uncheck !== null ? Html::hiddenInput($name, $uncheck) : '';
|
||||
$label = Html::encode($this->model->getAttributeLabel($this->attribute));
|
||||
return $this->begin() . "\n"
|
||||
. $hidden . Html::label("$radio $label", null, $this->labelOptions) . "\n"
|
||||
. $this->error() . "\n"
|
||||
. $this->end();
|
||||
} else {
|
||||
return Html::activeRadio($this->model, $this->attribute, $options);
|
||||
}
|
||||
return $this->render(Html::activeRadio($this->model, $this->attribute, $options));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -271,27 +254,11 @@ class ActiveField extends Component
|
||||
*
|
||||
* The rest of the options will be rendered as the attributes of the resulting tag. The values will
|
||||
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
|
||||
* @param boolean $enclosedByLabel whether to enclose the checkbox within the label tag.
|
||||
* If this is true, [[template]] will be ignored.
|
||||
* @return string the generated checkbox tag
|
||||
*/
|
||||
public function checkbox($options = array(), $enclosedByLabel = true)
|
||||
public function checkbox($options = array())
|
||||
{
|
||||
if ($enclosedByLabel) {
|
||||
$name = isset($options['name']) ? $options['name'] : Html::getInputName($this->model, $this->attribute);
|
||||
$checked = Html::getAttributeValue($this->model, $this->attribute);
|
||||
$checkbox = Html::checkbox($name, $checked, $options);
|
||||
$uncheck = array_key_exists('unchecked', $options) ? $options['uncheck'] : '0';
|
||||
unset($options['uncheck']);
|
||||
$hidden = $uncheck !== null ? Html::hiddenInput($name, $uncheck) : '';
|
||||
$label = Html::encode($this->model->getAttributeLabel($this->attribute));
|
||||
return $this->begin() . "\n"
|
||||
. $hidden . Html::label("$checkbox $label", null, $this->labelOptions) . "\n"
|
||||
. $this->error() . "\n"
|
||||
. $this->end();
|
||||
} else {
|
||||
return Html::activeCheckbox($this->model, $this->attribute, $options);
|
||||
}
|
||||
return $this->render(Html::activeCheckbox($this->model, $this->attribute, $options));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -11,7 +11,6 @@ use Yii;
|
||||
use yii\base\Widget;
|
||||
use yii\base\Model;
|
||||
use yii\helpers\Html;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
* ActiveForm ...
|
||||
@ -31,8 +30,8 @@ class ActiveForm extends Widget
|
||||
*/
|
||||
public $method = 'post';
|
||||
/**
|
||||
* @param array $options the attributes (name-value pairs) for the form tag.
|
||||
* The values will be HTML-encoded using [[encode()]].
|
||||
* @var array the HTML attributes (name-value pairs) for the form tag.
|
||||
* The values will be HTML-encoded using [[Html::encode()]].
|
||||
* If a value is null, the corresponding attribute will not be rendered.
|
||||
*/
|
||||
public $options = array();
|
||||
@ -53,6 +52,22 @@ class ActiveForm extends Widget
|
||||
public $fieldConfig = array(
|
||||
'class' => 'yii\widgets\ActiveField',
|
||||
);
|
||||
/**
|
||||
* @var string the CSS class that is added to a field container when the associated attribute is required.
|
||||
*/
|
||||
public $requiredCssClass = 'required';
|
||||
/**
|
||||
* @var string the CSS class that is added to a field container when the associated attribute has validation error.
|
||||
*/
|
||||
public $errorCssClass = 'error';
|
||||
/**
|
||||
* @var string the CSS class that is added to a field container when the associated attribute is successfully validated.
|
||||
*/
|
||||
public $successCssClass = 'success';
|
||||
/**
|
||||
* @var string the CSS class that is added to a field container when the associated attribute is being validated.
|
||||
*/
|
||||
public $validatingCssClass = 'validating';
|
||||
|
||||
/**
|
||||
* Initializes the widget.
|
||||
@ -73,9 +88,17 @@ class ActiveForm extends Widget
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Model|Model[] $models
|
||||
* @param array $options
|
||||
* @return string
|
||||
* Generates a summary of the validation errors.
|
||||
* If there is no validation error, an empty error summary markup will still be generated, but it will be hidden.
|
||||
* @param Model|Model[] $models the model(s) associated with this form
|
||||
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
|
||||
*
|
||||
* - header: string, the header HTML for the error summary. If not set, a default prompt string will be used.
|
||||
* - footer: string, the footer HTML for the error summary.
|
||||
*
|
||||
* The rest of the options will be rendered as the attributes of the container tag. The values will
|
||||
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
|
||||
* @return string the generated error summary
|
||||
*/
|
||||
public function errorSummary($models, $options = array())
|
||||
{
|
||||
@ -83,23 +106,17 @@ class ActiveForm extends Widget
|
||||
$models = array($models);
|
||||
}
|
||||
|
||||
$showAll = !empty($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());
|
||||
/** @var $model Model */
|
||||
foreach ($model->getFirstErrors() as $error) {
|
||||
$lines[] = Html::encode($error);
|
||||
}
|
||||
}
|
||||
|
||||
$header = isset($options['header']) ? $options['header'] : '<p>' . Yii::t('yii|Please fix the following errors:') . '</p>';
|
||||
$footer = isset($options['footer']) ? $options['footer'] : '';
|
||||
$tag = isset($options['tag']) ? $options['tag'] : 'div';
|
||||
unset($options['showAll'], $options['header'], $options['footer'], $options['container']);
|
||||
unset($options['header'], $options['footer']);
|
||||
|
||||
if (!isset($options['class'])) {
|
||||
$options['class'] = $this->errorSummaryCssClass;
|
||||
@ -108,22 +125,30 @@ class ActiveForm extends Widget
|
||||
}
|
||||
|
||||
if ($lines !== array()) {
|
||||
$content = "<ul><li>" . implode("</li>\n<li>", ArrayHelper::htmlEncode($lines)) . "</li><ul>";
|
||||
return Html::tag($tag, $header . $content . $footer, $options);
|
||||
$content = "<ul><li>" . implode("</li>\n<li>", $lines) . "</li><ul>";
|
||||
return Html::tag('div', $header . $content . $footer, $options);
|
||||
} else {
|
||||
$content = "<ul></ul>";
|
||||
$options['style'] = isset($options['style']) ? rtrim($options['style'], ';') . '; display:none' : 'display:none';
|
||||
return Html::tag($tag, $header . $content . $footer, $options);
|
||||
return Html::tag('div', $header . $content . $footer, $options);
|
||||
}
|
||||
}
|
||||
|
||||
public function field($model, $attribute, $options = null)
|
||||
/**
|
||||
* Generates a form field.
|
||||
* A form field is associated with a model and an attribute. It contains a label, an input and an error message
|
||||
* and use them to interact with end users to collect their inputs for the attribute.
|
||||
* @param Model $model the data model
|
||||
* @param string $attribute the attribute name or expression. See [[Html::getAttributeName()]] for the format
|
||||
* about attribute expression.
|
||||
* @return ActiveField the created ActiveField object
|
||||
*/
|
||||
public function field($model, $attribute, $options = array())
|
||||
{
|
||||
return Yii::createObject(array_merge($this->fieldConfig, array(
|
||||
return Yii::createObject(array_merge($this->fieldConfig, $options, array(
|
||||
'model' => $model,
|
||||
'attribute' => $attribute,
|
||||
'form' => $this,
|
||||
'options' => $options,
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user