mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
More control over ActiveForm widget
If part of a form is rendered separately (e.g. in ajax request), it's not easy to apply JS validation to it. By moving JS registration to a separate method this problem could be solved much more easier.
This commit is contained in:

committed by
SilverFire - Dmitry Naumenko

parent
c443c25a1e
commit
a480d70875
@ -191,7 +191,7 @@ class ActiveForm extends Widget
|
||||
|
||||
/**
|
||||
* Runs the widget.
|
||||
* This registers the necessary JavaScript code and renders the form close tag.
|
||||
* This registers the necessary JavaScript code and renders the form open and close tags.
|
||||
* @throws InvalidCallException if `beginField()` and `endField()` calls are not matching.
|
||||
*/
|
||||
public function run()
|
||||
@ -205,17 +205,25 @@ class ActiveForm extends Widget
|
||||
echo $content;
|
||||
|
||||
if ($this->enableClientScript) {
|
||||
$id = $this->options['id'];
|
||||
$options = Json::htmlEncode($this->getClientOptions());
|
||||
$attributes = Json::htmlEncode($this->attributes);
|
||||
$view = $this->getView();
|
||||
ActiveFormAsset::register($view);
|
||||
$view->registerJs("jQuery('#$id').yiiActiveForm($attributes, $options);");
|
||||
$this->registerJs();
|
||||
}
|
||||
|
||||
echo Html::endForm();
|
||||
}
|
||||
|
||||
/**
|
||||
* This registers the necessary JavaScript code.
|
||||
*/
|
||||
public function registerJs()
|
||||
{
|
||||
$id = $this->options['id'];
|
||||
$options = Json::htmlEncode($this->getClientOptions());
|
||||
$attributes = Json::htmlEncode($this->attributes);
|
||||
$view = $this->getView();
|
||||
ActiveFormAsset::register($view);
|
||||
$view->registerJs("jQuery('#$id').yiiActiveForm($attributes, $options);");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the options for the form JS widget.
|
||||
* @return array the options.
|
||||
|
Reference in New Issue
Block a user