Browse Source

Added bootstrap. form WIP

tags/2.0.0-alpha
Qiang Xue 12 years ago
parent
commit
f5f3d2e48e
  1. 1109
      app/css/bootstrap-responsive.css
  2. 9
      app/css/bootstrap-responsive.min.css
  3. 6158
      app/css/bootstrap.css
  4. 9
      app/css/bootstrap.min.css
  5. BIN
      app/img/glyphicons-halflings-white.png
  6. BIN
      app/img/glyphicons-halflings.png
  7. 2276
      app/js/bootstrap.js
  8. 6
      app/js/bootstrap.min.js
  9. 5
      app/protected/views/layouts/main.php
  10. 9
      app/protected/views/site/login.php
  11. 11
      framework/helpers/base/Html.php
  12. 79
      framework/widgets/ActiveField.php
  13. 69
      framework/widgets/ActiveForm.php

1109
app/css/bootstrap-responsive.css vendored

File diff suppressed because it is too large Load Diff

9
app/css/bootstrap-responsive.min.css vendored

File diff suppressed because one or more lines are too long

6158
app/css/bootstrap.css vendored

File diff suppressed because it is too large Load Diff

9
app/css/bootstrap.min.css vendored

File diff suppressed because one or more lines are too long

BIN
app/img/glyphicons-halflings-white.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

BIN
app/img/glyphicons-halflings.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

2276
app/js/bootstrap.js vendored

File diff suppressed because it is too large Load Diff

6
app/js/bootstrap.min.js vendored

File diff suppressed because one or more lines are too long

5
app/protected/views/layouts/main.php

@ -7,17 +7,20 @@ use yii\helpers\Html;
?>
<?php $this->beginPage(); ?>
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title><?php echo Html::encode($this->title); ?></title>
<?php echo Html::cssFile("css/bootstrap.min.css", array('media' => 'screen')); ?>
<?php $this->head(); ?>
</head>
<body>
<div class="container">
<h1>Welcome</h1>
<?php $this->beginBody(); ?>
<?php echo $content; ?>
<?php $this->endBody(); ?>
</div>
</body>
</html>
<?php $this->endPage(); ?>

9
app/protected/views/site/login.php

@ -1,6 +1,5 @@
<?php
use yii\helpers\Html;
/**
* @var yii\base\View $this
* @var yii\widgets\ActiveForm $form
@ -11,9 +10,13 @@ use yii\helpers\Html;
<p>Please fill out the following fields to login:</p>
<?php $form = $this->beginWidget('yii\widgets\ActiveForm'); ?>
<?php $form = $this->beginWidget('yii\widgets\ActiveForm', array('options' => array('class' => 'form-horizontal'))); ?>
<?php echo $form->field($model, 'username')->textInput(); ?>
<?php echo $form->field($model, 'password')->passwordInput(); ?>
<?php echo $form->field($model, 'rememberMe')->checkbox(); ?>
<?php echo Html::submitButton('Login'); ?>
<div class="control-group">
<div class="controls">
<?php echo Html::submitButton('Login', null, null, array('class' => 'btn btn-primary')); ?>
</div>
</div>
<?php $this->endWidget(); ?>

11
framework/helpers/base/Html.php

@ -1331,7 +1331,9 @@ class Html
* If the input parameter
*
* - is an empty string: the currently requested URL will be returned;
* - is a non-empty string: it will be processed by [[Yii::getAlias()]] and returned;
* - is a non-empty string: it will first be processed by [[Yii::getAlias()]]. If the result
* is an absolute URL, it will be returned with any change further; Otherwise, the result
* will be prefixed with [[\yii\web\Request::baseUrl]] and returned.
* - is an array: the first array element is considered a route, while the rest of the name-value
* pairs are treated as the parameters to be used for URL creation using [[\yii\web\Controller::createUrl()]].
* For example: `array('post/index', 'page' => 2)`, `array('index')`.
@ -1357,7 +1359,12 @@ class Html
} elseif ($url === '') {
return Yii::$app->getRequest()->getUrl();
} else {
return Yii::getAlias($url);
$url = Yii::getAlias($url);
if ($url[0] === '/' || strpos($url, '://')) {
return $url;
} else {
return Yii::$app->getRequest()->getBaseUrl() . '/' . $url;
}
}
}

79
framework/widgets/ActiveField.php

@ -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));
}
/**

69
framework/widgets/ActiveForm.php

@ -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,
)));
}
}

Loading…
Cancel
Save