Browse Source

form wip

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
69b2b3722b
  1. 2
      app/protected/models/LoginForm.php
  2. 5
      app/protected/views/layouts/main.php
  3. 9
      app/protected/views/site/login.php
  4. 13
      framework/helpers/base/Html.php
  5. 4
      framework/validators/BooleanValidator.php
  6. 138
      framework/widgets/ActiveField.php
  7. 17
      framework/widgets/ActiveForm.php

2
app/protected/models/LoginForm.php

@ -18,6 +18,7 @@ class LoginForm extends Model
{ {
public $username; public $username;
public $password; public $password;
public $rememberMe = true;
public function rules() public function rules()
{ {
@ -25,6 +26,7 @@ class LoginForm extends Model
array('username', 'required'), array('username', 'required'),
array('password', 'required'), array('password', 'required'),
array('password', 'validatePassword'), array('password', 'validatePassword'),
array('rememberMe', 'boolean'),
); );
} }

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

@ -5,10 +5,11 @@
*/ */
use yii\helpers\Html; use yii\helpers\Html;
?> ?>
<?php $this->beginPage(); ?>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<?php $this->beginPage(); ?>
<head> <head>
<meta charset="utf-8" />
<title><?php echo Html::encode($this->title); ?></title> <title><?php echo Html::encode($this->title); ?></title>
<?php $this->head(); ?> <?php $this->head(); ?>
</head> </head>
@ -18,5 +19,5 @@ use yii\helpers\Html;
<?php echo $content; ?> <?php echo $content; ?>
<?php $this->endBody(); ?> <?php $this->endBody(); ?>
</body> </body>
<?php $this->endPage(); ?>
</html> </html>
<?php $this->endPage(); ?>

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

@ -14,13 +14,6 @@ use yii\helpers\Html;
<?php $form = $this->beginWidget('yii\widgets\ActiveForm'); ?> <?php $form = $this->beginWidget('yii\widgets\ActiveForm'); ?>
<?php echo $form->field($model, 'username')->textInput(); ?> <?php echo $form->field($model, 'username')->textInput(); ?>
<?php echo $form->field($model, 'password')->passwordInput(); ?> <?php echo $form->field($model, 'password')->passwordInput(); ?>
<?php <?php echo $form->field($model, 'rememberMe')->checkbox(); ?>
$field = $form->field($model, 'username');
echo $field->begin() . "\n"
. $field->label() . "\n"
. Html::activeTextInput($model, 'username') . "\n"
. $field->error() . "\n"
. $field->end();
?>
<?php echo Html::submitButton('Login'); ?> <?php echo Html::submitButton('Login'); ?>
<?php $this->endWidget(); ?> <?php $this->endWidget(); ?>

13
framework/helpers/base/Html.php

@ -771,7 +771,7 @@ class Html
* @param array $items the data item used to generate the checkboxes. * @param array $items the data item used to generate the checkboxes.
* The array keys are the labels, while the array values are the corresponding checkbox values. * The array keys are the labels, while the array values are the corresponding checkbox values.
* Note that the labels will NOT be HTML-encoded, while the values will. * Note that the labels will NOT be HTML-encoded, while the values will.
* @param array $options options (name => config) for the checkbox list. The following options are specially handled: * @param array $options options (name => config) for the checkbox list. The following options are supported:
* *
* - unselect: string, the value that should be submitted when none of the checkboxes is selected. * - unselect: string, the value that should be submitted when none of the checkboxes is selected.
* By setting this option, a hidden input will be generated. * By setting this option, a hidden input will be generated.
@ -785,7 +785,7 @@ class Html
* *
* where $index is the zero-based index of the checkbox in the whole list; $label * where $index is the zero-based index of the checkbox in the whole list; $label
* is the label for the checkbox; and $name, $value and $checked represent the name, * is the label for the checkbox; and $name, $value and $checked represent the name,
* value and the checked status of the checkbox input. * value and the checked status of the checkbox input, respectively.
* @return string the generated checkbox list * @return string the generated checkbox list
*/ */
public static function checkboxList($name, $selection = null, $items = array(), $options = array()) public static function checkboxList($name, $selection = null, $items = array(), $options = array())
@ -829,7 +829,7 @@ class Html
* @param array $items the data item used to generate the radio buttons. * @param array $items the data item used to generate the radio buttons.
* The array keys are the labels, while the array values are the corresponding radio button values. * The array keys are the labels, while the array values are the corresponding radio button values.
* Note that the labels will NOT be HTML-encoded, while the values will. * Note that the labels will NOT be HTML-encoded, while the values will.
* @param array $options options (name => config) for the radio button list. The following options are specially handled: * @param array $options options (name => config) for the radio button list. The following options are supported:
* *
* - unselect: string, the value that should be submitted when none of the radio buttons is selected. * - unselect: string, the value that should be submitted when none of the radio buttons is selected.
* By setting this option, a hidden input will be generated. * By setting this option, a hidden input will be generated.
@ -843,7 +843,7 @@ class Html
* *
* where $index is the zero-based index of the radio button in the whole list; $label * where $index is the zero-based index of the radio button in the whole list; $label
* is the label for the radio button; and $name, $value and $checked represent the name, * is the label for the radio button; and $name, $value and $checked represent the name,
* value and the checked status of the radio button input. * value and the checked status of the radio button input, respectively.
* @return string the generated radio button list * @return string the generated radio button list
*/ */
public static function radioList($name, $selection = null, $items = array(), $options = array()) public static function radioList($name, $selection = null, $items = array(), $options = array())
@ -885,8 +885,9 @@ class Html
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* The following options are specially handled: * The following options are specially handled:
* *
* - label: this specifies the label to be displayed. If this is not set, [[Model::getAttributeLabel()]] * - label: this specifies the label to be displayed. Note that this will NOT be [[encoded()]].
* will be called to get the label for display. Note that this will NOT be [[encoded()]]. * If this is not set, [[Model::getAttributeLabel()]] will be called to get the label for display
* (after encoding).
* *
* @return string the generated label tag * @return string the generated label tag
*/ */

4
framework/validators/BooleanValidator.php

@ -70,8 +70,8 @@ class BooleanValidator extends Validator
*/ */
public function validateValue($value) public function validateValue($value)
{ {
return $this->strict && ($value == $this->trueValue || $value == $this->falseValue) return !$this->strict && ($value == $this->trueValue || $value == $this->falseValue)
|| !$this->strict && ($value === $this->trueValue || $value === $this->falseValue); || $this->strict && ($value === $this->trueValue || $value === $this->falseValue);
} }
/** /**

138
framework/widgets/ActiveField.php

@ -8,7 +8,6 @@
namespace yii\widgets; namespace yii\widgets;
use yii\base\Component; use yii\base\Component;
use yii\base\InvalidParamException;
use yii\helpers\Html; use yii\helpers\Html;
/** /**
@ -18,10 +17,6 @@ use yii\helpers\Html;
class ActiveField extends Component class ActiveField extends Component
{ {
/** /**
* @var string the tag name. Defaults to 'div'.
*/
public $tag;
/**
* @var ActiveForm * @var ActiveForm
*/ */
public $form; public $form;
@ -34,13 +29,15 @@ class ActiveField extends Component
*/ */
public $attribute; public $attribute;
/** /**
* @var string the tag name for the field container.
*/
public $tag = 'div';
/**
* @var array * @var array
*/ */
public $options = array( public $options = array(
'tag' => 'div',
'class' => 'yii-field', 'class' => 'yii-field',
); );
public $autoFieldCssClass = true;
/** /**
* @var string the default CSS class that indicates an input is required. * @var string the default CSS class that indicates an input is required.
*/ */
@ -57,7 +54,7 @@ class ActiveField extends Component
* @var string the default CSS class that indicates an input is currently being validated. * @var string the default CSS class that indicates an input is currently being validated.
*/ */
public $validatingCssClass = 'validating'; public $validatingCssClass = 'validating';
public $layout = "{label}\n{input}\n{error}"; public $template = "{label}\n{input}\n{error}";
public $errorOptions = array('tag' => 'span', 'class' => 'yii-error-message'); public $errorOptions = array('tag' => 'span', 'class' => 'yii-error-message');
public $labelOptions = array('class' => 'control-label'); public $labelOptions = array('class' => 'control-label');
@ -65,21 +62,15 @@ class ActiveField extends Component
public function begin() public function begin()
{ {
$options = $this->options; $options = $this->options;
$this->tag = isset($options['tag']) ? $options['tag'] : 'div';
unset($options['tag']);
$class = isset($options['class']) ? array($options['class']) : array(); $class = isset($options['class']) ? array($options['class']) : array();
if ($this->autoFieldCssClass) { $class[] = 'field-' . Html::getInputId($this->model, $this->attribute);
$class[] = 'field-' . Html::getInputId($this->model, $this->attribute);
}
if ($this->model->isAttributeRequired($this->attribute)) { if ($this->model->isAttributeRequired($this->attribute)) {
$class[] = $this->requiredCssClass; $class[] = $this->requiredCssClass;
} }
if ($this->model->hasErrors($this->attribute)) { if ($this->model->hasErrors($this->attribute)) {
$class[] = $this->errorCssClass; $class[] = $this->errorCssClass;
} }
if ($class !== array()) { $options['class'] = implode(' ', $class);
$options['class'] = implode(' ', $class);
}
return Html::beginTag($this->tag, $options); return Html::beginTag($this->tag, $options);
} }
@ -88,6 +79,21 @@ class ActiveField extends Component
return Html::endTag($this->tag); return Html::endTag($this->tag);
} }
/**
* Generates a label tag for [[attribute]].
* The label text is the label associated with the attribute, obtained via [[Model::getAttributeLabel()]].
* @param array $options the tag options in terms of name-value pairs. If this is null, [[labelOptions]] will be used.
* 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.
*
* The following options are specially handled:
*
* - label: this specifies the label to be displayed. Note that this will NOT be [[encoded()]].
* If this is not set, [[Model::getAttributeLabel()]] will be called to get the label for display
* (after encoding).
*
* @return string the generated label tag
*/
public function label($options = null) public function label($options = null)
{ {
if ($options === null) { if ($options === null) {
@ -96,6 +102,19 @@ class ActiveField extends Component
return Html::activeLabel($this->model, $this->attribute, $options); return Html::activeLabel($this->model, $this->attribute, $options);
} }
/**
* Generates a tag that contains the first validation error of [[attribute]].
* If there is no validation, the tag will be returned and styled as hidden.
* @param array $options the tag options in terms of name-value pairs. If this is null, [[errorOptions]] will be used.
* 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.
*
* The following options are specially handled:
*
* - tag: this specifies the tag name. If not set, "span" will be used.
*
* @return string the generated label tag
*/
public function error($options = null) public function error($options = null)
{ {
if ($options === null) { if ($options === null) {
@ -111,13 +130,20 @@ class ActiveField extends Component
return Html::tag($tag, Html::encode($error), $options); return Html::tag($tag, Html::encode($error), $options);
} }
protected function render($input) /**
* Renders the field with the given input HTML.
* This method will generate the label and error tags, and return them together with the given
* input HTML according to [[template]].
* @param string $input the input HTML
* @return string the rendering result
*/
public function render($input)
{ {
return $this->begin() . "\n" . strtr($this->layout, array( return $this->begin() . "\n" . strtr($this->template, array(
'{input}' => $input, '{input}' => $input,
'{label}' => $this->label(), '{label}' => $this->label(),
'{error}' => $this->error(), '{error}' => $this->error(),
)) . $this->end(); )) . "\n" . $this->end();
} }
/** /**
@ -200,7 +226,6 @@ class ActiveField extends Component
* Generates a radio button tag for the given model attribute. * Generates a radio button tag for the given model attribute.
* This method will generate the "name" tag attribute automatically unless it is explicitly specified in `$options`. * This method will generate the "name" tag attribute automatically unless it is explicitly specified in `$options`.
* This method will generate the "checked" tag attribute according to the model attribute value. * This method will generate the "checked" tag attribute according to the model attribute value.
* @param string $value the value tag attribute. If it is null, the value attribute will not be rendered.
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: * @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
* *
* - uncheck: string, the value associated with the uncheck state of the radio button. If not set, * - uncheck: string, the value associated with the uncheck state of the radio button. If not set,
@ -210,28 +235,33 @@ class ActiveField extends Component
* *
* The rest of the options will be rendered as the attributes of the resulting tag. The values will * 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. * 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 * @return string the generated radio button tag
*/ */
public function radio($value = '1', $options = array()) public function radio($options = array(), $enclosedByLabel = true)
{
return $this->render(Html::activeRadio($this->model, $this->attribute, $value, $options));
}
public function radioAlt($value = '1', $options = array())
{ {
$label = Html::encode($this->model->getAttributeLabel($this->attribute)); if ($enclosedByLabel) {
return $this->begin() . "\n" $name = isset($options['name']) ? $options['name'] : Html::getInputName($this->model, $this->attribute);
. Html::label(Html::activeRadio($this->model, $this->attribute, $value, $options) . ' ' . $label) . "\n" $checked = Html::getAttributeValue($this->model, $this->attribute);
. $this->error() . "\n" $radio = Html::radio($name, $checked, $options);
. $this->end(); $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);
}
} }
/** /**
* Generates a checkbox tag for the given model attribute. * Generates a checkbox tag for the given model attribute.
* This method will generate the "name" tag attribute automatically unless it is explicitly specified in `$options`. * This method will generate the "name" tag attribute automatically unless it is explicitly specified in `$options`.
* This method will generate the "checked" tag attribute according to the model attribute value. * This method will generate the "checked" tag attribute according to the model attribute value.
* @param string $value the value tag attribute. If it is null, the value attribute will not be rendered.
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: * @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
* *
* - uncheck: string, the value associated with the uncheck state of the radio button. If not set, * - uncheck: string, the value associated with the uncheck state of the radio button. If not set,
@ -241,21 +271,27 @@ class ActiveField extends Component
* *
* The rest of the options will be rendered as the attributes of the resulting tag. The values will * 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. * 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 * @return string the generated checkbox tag
*/ */
public function checkbox($value = '1', $options = array()) public function checkbox($options = array(), $enclosedByLabel = true)
{ {
return $this->render(Html::activeCheckbox($this->model, $this->attribute, $value, $options)); if ($enclosedByLabel) {
} $name = isset($options['name']) ? $options['name'] : Html::getInputName($this->model, $this->attribute);
$checked = Html::getAttributeValue($this->model, $this->attribute);
public function checkboxAlt($value = '1', $options = array()) $checkbox = Html::checkbox($name, $checked, $options);
{ $uncheck = array_key_exists('unchecked', $options) ? $options['uncheck'] : '0';
$label = Html::encode($this->model->getAttributeLabel($this->attribute)); unset($options['uncheck']);
return $this->begin() . "\n" $hidden = $uncheck !== null ? Html::hiddenInput($name, $uncheck) : '';
. Html::label(Html::activeCheckbox($this->model, $this->attribute, $value, $options) . ' ' . $label) . "\n" $label = Html::encode($this->model->getAttributeLabel($this->attribute));
. $this->error() . "\n" return $this->begin() . "\n"
. $this->end(); . $hidden . Html::label("$checkbox $label", null, $this->labelOptions) . "\n"
. $this->error() . "\n"
. $this->end();
} else {
return Html::activeCheckbox($this->model, $this->attribute, $options);
}
} }
/** /**
@ -362,7 +398,11 @@ class ActiveField extends Component
*/ */
public function checkboxList($items, $options = array()) public function checkboxList($items, $options = array())
{ {
return Html::activeCheckboxList($this->model, $this->attribute, $items, $options); return $this->render(
'<div id="' . Html::getInputId($this->model, $this->attribute) . '">'
. Html::activeCheckboxList($this->model, $this->attribute, $items, $options)
. '</div>'
);
} }
/** /**
@ -391,6 +431,10 @@ class ActiveField extends Component
*/ */
public function radioList($items, $options = array()) public function radioList($items, $options = array())
{ {
return Html::activeRadioList($this->model, $this->attribute, $items, $options); return $this->render(
'<div id="' . Html::getInputId($this->model, $this->attribute) . '">'
. Html::activeRadioList($this->model, $this->attribute, $items, $options)
. '</div>'
);
} }
} }

17
framework/widgets/ActiveForm.php

@ -30,6 +30,11 @@ class ActiveForm extends Widget
* Defaults to 'post'. * Defaults to 'post'.
*/ */
public $method = 'post'; public $method = 'post';
/**
* @param array $options the attributes (name-value pairs) for the form tag.
* The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
*/
public $options = array(); public $options = array();
/** /**
* @var string the default CSS class for the error summary container. * @var string the default CSS class for the error summary container.
@ -37,12 +42,14 @@ class ActiveForm extends Widget
*/ */
public $errorSummaryCssClass = 'yii-error-summary'; public $errorSummaryCssClass = 'yii-error-summary';
/** /**
* @var boolean whether to enable client-side data validation. Defaults to false. * @var boolean whether to enable client-side data validation.
* When this property is set true, client-side validation will be performed by validators * Client-side validation will be performed by validators that support it
* that support it (see {@link CValidator::enableClientValidation} and {@link CValidator::clientValidateAttribute}). * (see [[\yii\validators\Validator::enableClientValidation]] and [[\yii\validators\Validator::clientValidateAttribute()]]).
*/
public $enableClientValidation = true;
/**
* @var array the default configuration used by [[field()]] when creating a new field object.
*/ */
public $enableClientValidation = false;
public $fieldConfig = array( public $fieldConfig = array(
'class' => 'yii\widgets\ActiveField', 'class' => 'yii\widgets\ActiveField',
); );

Loading…
Cancel
Save