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:
Oleg Poludnenko
2016-12-18 18:26:31 +02:00
committed by SilverFire - Dmitry Naumenko
parent c443c25a1e
commit a480d70875

View File

@ -191,7 +191,7 @@ class ActiveForm extends Widget
/** /**
* Runs the 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. * @throws InvalidCallException if `beginField()` and `endField()` calls are not matching.
*/ */
public function run() public function run()
@ -205,6 +205,17 @@ class ActiveForm extends Widget
echo $content; echo $content;
if ($this->enableClientScript) { if ($this->enableClientScript) {
$this->registerJs();
}
echo Html::endForm();
}
/**
* This registers the necessary JavaScript code.
*/
public function registerJs()
{
$id = $this->options['id']; $id = $this->options['id'];
$options = Json::htmlEncode($this->getClientOptions()); $options = Json::htmlEncode($this->getClientOptions());
$attributes = Json::htmlEncode($this->attributes); $attributes = Json::htmlEncode($this->attributes);
@ -213,9 +224,6 @@ class ActiveForm extends Widget
$view->registerJs("jQuery('#$id').yiiActiveForm($attributes, $options);"); $view->registerJs("jQuery('#$id').yiiActiveForm($attributes, $options);");
} }
echo Html::endForm();
}
/** /**
* Returns the options for the form JS widget. * Returns the options for the form JS widget.
* @return array the options. * @return array the options.