Browse Source

form wip

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
74246a23f6
  1. 2
      app/protected/views/site/login.php
  2. 2
      framework/web/User.php
  3. 40
      framework/widgets/ActiveField.php
  4. 31
      framework/widgets/ActiveForm.php

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

@ -13,7 +13,7 @@ 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')->checkboxAlt(); ?> <?php echo $form->field($model, 'password')->passwordInput(); ?>
<?php <?php
$field = $form->field($model, 'username'); $field = $form->field($model, 'username');
echo $field->begin() . "\n" echo $field->begin() . "\n"

2
framework/web/User.php

@ -32,7 +32,7 @@ class User extends Component
const EVENT_AFTER_LOGOUT = 'afterLogout'; const EVENT_AFTER_LOGOUT = 'afterLogout';
/** /**
* @var string the class name or alias of the [[identity]] object. * @var string the class name of the [[identity]] object.
*/ */
public $identityClass; public $identityClass;
/** /**

40
framework/widgets/ActiveField.php

@ -36,22 +36,46 @@ class ActiveField extends Component
/** /**
* @var array * @var array
*/ */
public $options; public $options = array(
'tag' => 'div',
'class' => 'yii-field',
);
public $autoFieldCssClass = true;
/**
* @var string the default CSS class that indicates an input is required.
*/
public $requiredCssClass = 'required';
/**
* @var string the default CSS class that indicates an input has error.
*/
public $errorCssClass = 'error';
/**
* @var string the default CSS class that indicates an input validated successfully.
*/
public $successCssClass = 'success';
/**
* @var string the default CSS class that indicates an input is currently being validated.
*/
public $validatingCssClass = 'validating';
public $layout = "{label}\n{input}\n{error}";
public $errorOptions = array('tag' => 'span', 'class' => 'yii-error-message');
public $labelOptions = array('class' => 'control-label');
public function begin() public function begin()
{ {
$options = $this->options === null ? $this->form->fieldOptions : $this->options; $options = $this->options;
$this->tag = isset($options['tag']) ? $options['tag'] : 'div'; $this->tag = isset($options['tag']) ? $options['tag'] : 'div';
unset($options['tag']); unset($options['tag']);
$class = isset($options['class']) ? array($options['class']) : array(); $class = isset($options['class']) ? array($options['class']) : array();
if ($this->form->autoFieldCssClass) { 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->form->requiredCssClass; $class[] = $this->requiredCssClass;
} }
if ($this->model->hasErrors($this->attribute)) { if ($this->model->hasErrors($this->attribute)) {
$class[] = $this->form->errorCssClass; $class[] = $this->errorCssClass;
} }
if ($class !== array()) { if ($class !== array()) {
$options['class'] = implode(' ', $class); $options['class'] = implode(' ', $class);
@ -67,7 +91,7 @@ class ActiveField extends Component
public function label($options = null) public function label($options = null)
{ {
if ($options === null) { if ($options === null) {
$options = $this->form->labelOptions; $options = $this->labelOptions;
} }
return Html::activeLabel($this->model, $this->attribute, $options); return Html::activeLabel($this->model, $this->attribute, $options);
} }
@ -75,7 +99,7 @@ class ActiveField extends Component
public function error($options = null) public function error($options = null)
{ {
if ($options === null) { if ($options === null) {
$options = $this->form->errorOptions; $options = $this->errorOptions;
} }
$attribute = Html::getAttributeName($this->attribute); $attribute = Html::getAttributeName($this->attribute);
$error = $this->model->getFirstError($attribute); $error = $this->model->getFirstError($attribute);
@ -89,7 +113,7 @@ class ActiveField extends Component
protected function render($input) protected function render($input)
{ {
return $this->begin() . "\n" . strtr($this->form->fieldTemplate, array( return $this->begin() . "\n" . strtr($this->layout, array(
'{input}' => $input, '{input}' => $input,
'{label}' => $this->label(), '{label}' => $this->label(),
'{error}' => $this->error(), '{error}' => $this->error(),

31
framework/widgets/ActiveForm.php

@ -31,40 +31,22 @@ class ActiveForm extends Widget
*/ */
public $method = 'post'; public $method = 'post';
public $options = array(); public $options = array();
public $fieldOptions = array('tag' => 'div', 'class' => 'yii-field');
public $fieldTemplate = "{label}\n{input}\n{error}";
public $autoFieldCssClass = true;
public $errorOptions = array('tag' => 'span', 'class' => 'yii-error-message');
public $labelOptions = array('class' => 'control-label');
/** /**
* @var string the default CSS class for the error summary container. * @var string the default CSS class for the error summary container.
* @see errorSummary() * @see errorSummary()
*/ */
public $errorSummaryCssClass = 'yii-error-summary'; public $errorSummaryCssClass = 'yii-error-summary';
/** /**
* @var string the default CSS class that indicates an input is required.
*/
public $requiredCssClass = 'required';
/**
* @var string the default CSS class that indicates an input has error.
*/
public $errorCssClass = 'error';
/**
* @var string the default CSS class that indicates an input validated successfully.
*/
public $successCssClass = 'success';
/**
* @var string the default CSS class that indicates an input is currently being validated.
*/
public $validatingCssClass = 'validating';
/**
* @var boolean whether to enable client-side data validation. Defaults to false. * @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 * 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}). * that support it (see {@link CValidator::enableClientValidation} and {@link CValidator::clientValidateAttribute}).
*/ */
public $enableClientValidation = false; public $enableClientValidation = false;
public $fieldClass = 'yii\widgets\ActiveField'; public $fieldConfig = array(
'class' => 'yii\widgets\ActiveField',
);
/** /**
* Initializes the widget. * Initializes the widget.
* This renders the form open tag. * This renders the form open tag.
@ -130,12 +112,11 @@ class ActiveForm extends Widget
public function field($model, $attribute, $options = null) public function field($model, $attribute, $options = null)
{ {
return Yii::createObject(array( return Yii::createObject(array_merge($this->fieldConfig, array(
'class' => $this->fieldClass,
'model' => $model, 'model' => $model,
'attribute' => $attribute, 'attribute' => $attribute,
'form' => $this, 'form' => $this,
'options' => $options, 'options' => $options,
)); )));
} }
} }

Loading…
Cancel
Save