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

2
framework/web/User.php

@ -32,7 +32,7 @@ class User extends Component
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;
/**

42
framework/widgets/ActiveField.php

@ -36,22 +36,46 @@ class ActiveField extends Component
/**
* @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()
{
$options = $this->options === null ? $this->form->fieldOptions : $this->options;
$options = $this->options;
$this->tag = isset($options['tag']) ? $options['tag'] : 'div';
unset($options['tag']);
$class = isset($options['class']) ? array($options['class']) : array();
if ($this->form->autoFieldCssClass) {
if ($this->autoFieldCssClass) {
$class[] = 'field-' . Html::getInputId($this->model, $this->attribute);
}
if ($this->model->isAttributeRequired($this->attribute)) {
$class[] = $this->form->requiredCssClass;
$class[] = $this->requiredCssClass;
}
if ($this->model->hasErrors($this->attribute)) {
$class[] = $this->form->errorCssClass;
$class[] = $this->errorCssClass;
}
if ($class !== array()) {
$options['class'] = implode(' ', $class);
@ -67,7 +91,7 @@ class ActiveField extends Component
public function label($options = null)
{
if ($options === null) {
$options = $this->form->labelOptions;
$options = $this->labelOptions;
}
return Html::activeLabel($this->model, $this->attribute, $options);
}
@ -75,7 +99,7 @@ class ActiveField extends Component
public function error($options = null)
{
if ($options === null) {
$options = $this->form->errorOptions;
$options = $this->errorOptions;
}
$attribute = Html::getAttributeName($this->attribute);
$error = $this->model->getFirstError($attribute);
@ -89,7 +113,7 @@ class ActiveField extends Component
protected function render($input)
{
return $this->begin() . "\n" . strtr($this->form->fieldTemplate, array(
return $this->begin() . "\n" . strtr($this->layout, array(
'{input}' => $input,
'{label}' => $this->label(),
'{error}' => $this->error(),

31
framework/widgets/ActiveForm.php

@ -31,40 +31,22 @@ class ActiveForm extends Widget
*/
public $method = 'post';
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.
* @see errorSummary()
*/
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.
* 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;
public $fieldClass = 'yii\widgets\ActiveField';
public $fieldConfig = array(
'class' => 'yii\widgets\ActiveField',
);
/**
* Initializes the widget.
* This renders the form open tag.
@ -130,12 +112,11 @@ class ActiveForm extends Widget
public function field($model, $attribute, $options = null)
{
return Yii::createObject(array(
'class' => $this->fieldClass,
return Yii::createObject(array_merge($this->fieldConfig, array(
'model' => $model,
'attribute' => $attribute,
'form' => $this,
'options' => $options,
));
)));
}
}

Loading…
Cancel
Save