Commit 86330471 by Qiang Xue

form wip

parent b4925d78
......@@ -102,12 +102,6 @@ abstract class Validator extends Component
public $skipOnEmpty = true;
/**
* @var boolean whether to enable client-side validation. Defaults to null, meaning
* its actual value inherits from that of [[\yii\web\ActiveForm::enableClientValidation]].
*/
public $enableClientValidation;
/**
* Validates a single attribute.
* Child classes must implement this method to provide the actual validation logic.
* @param \yii\base\Model $object the data object to be validated
......@@ -211,7 +205,6 @@ abstract class Validator extends Component
* @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script. Null if the validator does not support
* client-side validation.
* @see enableClientValidation
* @see \yii\web\ActiveForm::enableClientValidation
*/
public function clientValidateAttribute($object, $attribute)
......
......@@ -10,6 +10,7 @@ namespace yii\widgets;
use yii\base\Component;
use yii\helpers\Html;
use yii\base\Model;
use yii\helpers\JsExpression;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
......@@ -60,15 +61,39 @@ class ActiveField extends Component
public function begin()
{
$inputID = Html::getInputId($this->model, $this->attribute);
$attribute = Html::getAttributeName($this->attribute);
$validators = array();
foreach ($this->model->getValidators($attribute) as $validator) {
/** @var \yii\validators\Validator $validator */
if (($js = $validator->clientValidateAttribute($this->model, $attribute)) != '') {
$validators[] = $js;
}
}
$jsOptions = array(
'name' => $this->attribute,
'container' => ".field-$inputID",
'input' => "#$inputID",
'error' => '.help-inline',
);
if ($validators !== array()) {
$jsOptions['validate'] = new JsExpression("function(attribute, value, messages) {" . implode('', $validators) . '}');
}
$this->form->attributes[$this->attribute] = $jsOptions;
$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[] = "field-$inputID";
if ($this->model->isAttributeRequired($attribute)) {
$class[] = $this->form->requiredCssClass;
}
if ($this->model->hasErrors($this->attribute)) {
if ($this->model->hasErrors($attribute)) {
$class[] = $this->form->errorCssClass;
}
$options['class'] = implode(' ', $class);
return Html::beginTag($this->tag, $options);
}
......
......@@ -11,6 +11,7 @@ use Yii;
use yii\base\Widget;
use yii\base\Model;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* ActiveForm ...
......@@ -41,17 +42,20 @@ class ActiveForm extends Widget
*/
public $errorSummaryCssClass = 'yii-error-summary';
/**
* @var boolean whether to enable client-side data validation.
* Client-side validation will be performed by validators that support it
* (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 $fieldConfig = array(
'class' => 'yii\widgets\ActiveField',
);
/**
* @var boolean whether to enable client-side data validation.
* Client-side validation will be performed by validators that support it
* (see [[\yii\validators\Validator::enableClientValidation]] and [[\yii\validators\Validator::clientValidateAttribute()]]).
*/
public $enableClientValidation = true;
public $enableAjaxValidation = false;
/**
* @var string the CSS class that is added to a field container when the associated attribute is required.
*/
......@@ -69,13 +73,22 @@ class ActiveForm extends Widget
*/
public $validatingCssClass = 'validating';
public $validationUrl;
public $validationDelay;
public $validateOnChange;
public $validateOnType;
public $attributes = array();
/**
* Initializes the widget.
* This renders the form open tag.
*/
public function init()
{
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
echo Html::beginForm($this->action, $this->method, $this->options);
}
......@@ -85,11 +98,18 @@ class ActiveForm extends Widget
*/
public function run()
{
$id = $this->getId();
$options = array();
$options = json_encode($options);
$id = $this->options['id'];
$options = array(
'enableClientValidation' => $this->enableClientValidation,
'enableAjaxValidation' => $this->enableAjaxValidation,
'errorCssClass' => $this->errorCssClass,
'successCssClass' => $this->successCssClass,
'validatingCssClass' => $this->validatingCssClass,
);
$options = Json::encode($options);
$attributes = Json::encode($this->attributes);
$this->view->registerAssetBundle('yii/form');
$this->view->registerJs("jQuery('#$id').yii.form($options);");
$this->view->registerJs("jQuery('#$id').yiiActiveForm($attributes, $options);");
echo Html::endForm();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment