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,16 +205,24 @@ class ActiveForm extends Widget
echo $content; echo $content;
if ($this->enableClientScript) { if ($this->enableClientScript) {
$id = $this->options['id']; $this->registerJs();
$options = Json::htmlEncode($this->getClientOptions());
$attributes = Json::htmlEncode($this->attributes);
$view = $this->getView();
ActiveFormAsset::register($view);
$view->registerJs("jQuery('#$id').yiiActiveForm($attributes, $options);");
} }
echo Html::endForm(); 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. * Returns the options for the form JS widget.