-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #8452 Jquery Extraction (attempt 2) #14865
Changes from all commits
e4ee202
de48158
530d22c
f2cad0c
18d2e85
dc01884
701ac69
fbaa7fb
dc72df3
8edbcb5
eef2e87
b79f53e
e2aedbd
6548868
5469ce7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
/** | ||
* @link http://www.yiiframework.com/ | ||
* @copyright Copyright (c) 2008 Yii Software LLC | ||
* @license http://www.yiiframework.com/license/ | ||
*/ | ||
|
||
namespace yii\jquery; | ||
|
||
use Yii; | ||
use yii\helpers\Json; | ||
|
||
/** | ||
* ActiveFormClientScript is a behavior for [[\yii\widgets\ActiveForm]], which allows composition | ||
* of the client-side and AJAX form validation via underlying JQuery plugin. | ||
* | ||
* Usage example: | ||
* | ||
* ```php | ||
* <?php $form = \yii\widgets\ActiveForm::begin([ | ||
* 'id' => 'example-form', | ||
* 'as clientScript' => \yii\jquery\ActiveFormClientScript::class, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are pros compared to extending There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. That was 1st approach. Cons are described here: #13839 That's why behaviors. |
||
* // ... | ||
* ]); ?> | ||
* ... | ||
* <?php \yii\widgets\ActiveForm::end(); ?> | ||
* ``` | ||
* | ||
* @see \yii\widgets\ActiveForm | ||
* @see \yii\widgets\ActiveFormClientScriptBehavior | ||
* | ||
* @author Paul Klimov <klimov.paul@gmail.com> | ||
* @since 2.1.0 | ||
*/ | ||
class ActiveFormClientScript extends \yii\widgets\ActiveFormClientScript | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function defaultClientValidatorMap() | ||
{ | ||
return [ | ||
\yii\validators\BooleanValidator::class => \yii\jquery\validators\client\BooleanValidator::class, | ||
\yii\validators\CompareValidator::class => \yii\jquery\validators\client\CompareValidator::class, | ||
\yii\validators\EmailValidator::class => \yii\jquery\validators\client\EmailValidator::class, | ||
\yii\validators\FilterValidator::class => \yii\jquery\validators\client\FilterValidator::class, | ||
\yii\validators\IpValidator::class => \yii\jquery\validators\client\IpValidator::class, | ||
\yii\validators\NumberValidator::class => \yii\jquery\validators\client\NumberValidator::class, | ||
\yii\validators\RangeValidator::class => \yii\jquery\validators\client\RangeValidator::class, | ||
\yii\validators\RegularExpressionValidator::class => \yii\jquery\validators\client\RegularExpressionValidator::class, | ||
\yii\validators\RequiredValidator::class => \yii\jquery\validators\client\RequiredValidator::class, | ||
\yii\validators\StringValidator::class => \yii\jquery\validators\client\StringValidator::class, | ||
\yii\validators\UrlValidator::class => \yii\jquery\validators\client\UrlValidator::class, | ||
\yii\validators\ImageValidator::class => \yii\jquery\validators\client\ImageValidator::class, | ||
\yii\validators\FileValidator::class => \yii\jquery\validators\client\FileValidator::class, | ||
\yii\captcha\CaptchaValidator::class => \yii\jquery\validators\client\CaptchaClientValidator::class, | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function registerClientScript() | ||
{ | ||
$id = $this->owner->options['id']; | ||
$options = Json::htmlEncode($this->getClientOptions()); | ||
$attributes = Json::htmlEncode($this->attributes); | ||
$view = $this->owner->getView(); | ||
ActiveFormAsset::register($view); | ||
$view->registerJs("jQuery('#$id').yiiActiveForm($attributes, $options);"); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getFieldClientOptions($field) | ||
{ | ||
$options = parent::getFieldClientOptions($field); | ||
|
||
// only get the options that are different from the default ones (set in yii.activeForm.js) | ||
return array_diff_assoc($options, [ | ||
'validateOnChange' => true, | ||
'validateOnBlur' => true, | ||
'validateOnType' => false, | ||
'validationDelay' => 500, | ||
'encodeError' => true, | ||
'error' => '.help-block', | ||
'updateAriaInvalid' => true, | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getClientOptions() | ||
{ | ||
$options = parent::getClientOptions(); | ||
|
||
// only get the options that are different from the default ones (set in yii.activeForm.js) | ||
return array_diff_assoc($options, [ | ||
'encodeErrorSummary' => true, | ||
'errorSummary' => '.error-summary', | ||
'validateOnSubmit' => true, | ||
'errorCssClass' => 'has-error', | ||
'successCssClass' => 'has-success', | ||
'validatingCssClass' => 'validating', | ||
'ajaxParam' => 'ajax', | ||
'ajaxDataType' => 'json', | ||
'scrollToError' => true, | ||
'scrollToErrorOffset' => 0, | ||
]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
/** | ||
* @link http://www.yiiframework.com/ | ||
* @copyright Copyright (c) 2008 Yii Software LLC | ||
* @license http://www.yiiframework.com/license/ | ||
*/ | ||
|
||
namespace yii\jquery; | ||
|
||
use Yii; | ||
use yii\base\Behavior; | ||
use yii\base\Widget; | ||
use yii\helpers\Json; | ||
use yii\helpers\Url; | ||
|
||
/** | ||
* GridViewClientScript is a behavior for [[\yii\grid\GridView]] widget, which allows automatic filter submission via jQuery component. | ||
* | ||
* A basic usage looks like the following: | ||
* | ||
* ```php | ||
* <?= yii\grid\GridView::widget([ | ||
* 'dataProvider' => $dataProvider, | ||
* 'as clientScript' => [ | ||
* 'class' => yii\jquery\GridViewClientScript::class | ||
* ], | ||
* 'columns' => [ | ||
* 'id', | ||
* 'name', | ||
* 'created_at:datetime', | ||
* // ... | ||
* ], | ||
* ]) ?> | ||
* ``` | ||
* | ||
* @see \yii\grid\GridView | ||
* @see GridViewAsset | ||
* | ||
* @property \yii\grid\GridView $owner the owner of this behavior. | ||
* | ||
* @author Qiang Xue <qiang.xue@gmail.com> | ||
* @author Paul Klimov <klimov.paul@gmail.com> | ||
* @since 2.1.0 | ||
*/ | ||
class GridViewClientScript extends Behavior | ||
{ | ||
/** | ||
* @var string additional jQuery selector for selecting filter input fields. | ||
*/ | ||
public $filterSelector; | ||
|
||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function events() | ||
{ | ||
return [ | ||
Widget::EVENT_BEFORE_RUN => 'beforeRun' | ||
]; | ||
} | ||
|
||
/** | ||
* Handles [[Widget::EVENT_BEFORE_RUN]] event, registering related client script. | ||
* @param \yii\base\Event $event event instance. | ||
*/ | ||
public function beforeRun($event) | ||
{ | ||
$id = $this->owner->options['id']; | ||
$options = Json::htmlEncode($this->getClientOptions()); | ||
$view = $this->owner->getView(); | ||
GridViewAsset::register($view); | ||
$view->registerJs("jQuery('#$id').yiiGridView($options);"); | ||
} | ||
|
||
/** | ||
* Returns the options for the grid view JS widget. | ||
* @return array the options | ||
*/ | ||
protected function getClientOptions() | ||
{ | ||
$filterUrl = isset($this->owner->filterUrl) ? $this->owner->filterUrl : Yii::$app->request->url; | ||
$id = $this->owner->filterRowOptions['id']; | ||
$filterSelector = "#$id input, #$id select"; | ||
if (isset($this->filterSelector)) { | ||
$filterSelector .= ', ' . $this->filterSelector; | ||
} | ||
|
||
return [ | ||
'filterUrl' => Url::to($filterUrl), | ||
'filterSelector' => $filterSelector, | ||
]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Captcha should be moved to separate repository. Script should be adjusted not to use jQuery.
Could be done in separate pull request after this one is merged.