-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Allow optional jQuery in Validators and Widgets. #20528
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
Changes from all commits
8b4ca6e
21ee13e
5ea9707
921b089
e0d40e7
8748789
1d6ddb9
dbd7760
8a9c70a
bf198b9
514f1b1
ddf36bf
a43c983
16830a1
30d0e32
5b8aff9
8335d95
693eb43
342e715
7e159cf
fabb1e9
67ad445
5ed8f23
1b930df
c4cbbb3
b2cda69
0175983
1657033
4a366ef
538918f
7d9bc31
6afa219
b160cda
a2187a2
faceade
da4625e
b42e3ce
d8a8761
1ff3c56
abab38b
ca13ed9
68e1499
02bfb78
a9dc462
b3764b6
8ce7391
72fabb7
94ac107
343f8c6
184c51b
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @link https://www.yiiframework.com/ | ||
| * @copyright Copyright (c) 2008 Yii Software LLC | ||
|
|
@@ -78,7 +79,15 @@ class Application extends \yii\base\Application | |
| * @var Controller the currently active controller instance | ||
| */ | ||
| public $controller; | ||
|
|
||
| /** | ||
| * Whether to enable jQuery integration for the console application. | ||
| * | ||
| * This property is reserved for future compatibility. Console applications do not utilize jQuery, but this flag is | ||
| * provided for configuration consistency with web applications. | ||
| * | ||
| * @since 2.2.0 | ||
| */ | ||
| public bool $useJquery = false; | ||
|
Member
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. I don't think it is needed. Anything breaks if it's removed? |
||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @link https://www.yiiframework.com/ | ||
| * @copyright Copyright (c) 2008 Yii Software LLC | ||
| * @license https://www.yiiframework.com/license/ | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace yii\jquery\grid; | ||
|
|
||
| use yii\base\BaseObject; | ||
| use yii\helpers\Json; | ||
| use yii\web\client\ClientScriptInterface; | ||
| use yii\web\View; | ||
|
|
||
| /** | ||
| * CheckboxColumnJqueryClientScript provides client-side script registration for gridview checkbox columns. | ||
| * | ||
| * This class implements {@see ClientScriptInterface} to supply client-side options and register the corresponding | ||
| * JavaScript code for checkbox selection columns in Yii2 gridviews using jQuery. | ||
| * | ||
| * @author Wilmer Arambula <[email protected]> | ||
| * @since 2.2.0 | ||
| */ | ||
| class CheckboxColumnJqueryClientScript implements ClientScriptInterface | ||
| { | ||
| public function register(BaseObject $object, View $view): void | ||
| { | ||
| $id = $object->grid->options['id']; | ||
|
Check failure on line 31 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php
|
||
|
|
||
| $options = Json::encode( | ||
| [ | ||
| 'name' => $object->name, | ||
|
Check failure on line 35 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php
|
||
| 'class' => $object->cssClass, | ||
|
Check failure on line 36 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php
|
||
| 'multiple' => $object->multiple, | ||
|
Check failure on line 37 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php
|
||
| 'checkAll' => $object->grid->showHeader ? $object->getHeaderCheckBoxName() : null, | ||
|
Check failure on line 38 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php
|
||
| ], | ||
| ); | ||
| $view->registerJs("jQuery('#$id').yiiGridView('setSelectionColumn', $options);"); | ||
| } | ||
|
|
||
| public function getClientOptions(BaseObject $object): array | ||
| { | ||
| return []; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @link https://www.yiiframework.com/ | ||
| * @copyright Copyright (c) 2008 Yii Software LLC | ||
| * @license https://www.yiiframework.com/license/ | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace yii\jquery\grid; | ||
|
|
||
| use Yii; | ||
| use yii\base\BaseObject; | ||
| use yii\grid\GridView; | ||
| use yii\grid\GridViewAsset; | ||
| use yii\helpers\Json; | ||
| use yii\helpers\Url; | ||
| use yii\web\client\ClientScriptInterface; | ||
| use yii\web\View; | ||
|
|
||
| use function array_merge; | ||
|
|
||
| /** | ||
| * GridViewJqueryClientScript provides client-side script registration for GridView widgets using jQuery. | ||
| * | ||
| * This class implements {@see ClientScriptInterface} to supply client-side options and register the corresponding | ||
| * JavaScript code for GridView widgets in Yii2 applications using jQuery. | ||
| * | ||
| * @author Wilmer Arambula <[email protected]> | ||
| * @since 2.2.0 | ||
| */ | ||
| class GridViewJqueryClientScript implements ClientScriptInterface | ||
| { | ||
| public function getClientOptions(BaseObject $object): array | ||
| { | ||
| if (!$object instanceof GridView) { | ||
| return []; | ||
| } | ||
|
|
||
| $filterUrl = $object->filterUrl ?? Yii::$app->request->url; | ||
| $id = $object->filterRowOptions['id']; | ||
| $filterSelector = "#$id input, #$id select"; | ||
|
|
||
| if (isset($object->filterSelector)) { | ||
| $filterSelector .= ', ' . $object->filterSelector; | ||
| } | ||
|
|
||
| return [ | ||
| 'filterUrl' => Url::to($filterUrl), | ||
| 'filterSelector' => $filterSelector, | ||
| ]; | ||
| } | ||
|
|
||
| public function register(BaseObject $object, View $view): void | ||
| { | ||
| $view = $object->getView(); | ||
|
Check failure on line 57 in framework/jquery/grid/GridViewJqueryClientScript.php
|
||
|
|
||
| GridViewAsset::register($view); | ||
|
|
||
| $id = $object->options['id']; | ||
|
Check failure on line 61 in framework/jquery/grid/GridViewJqueryClientScript.php
|
||
|
|
||
| $options = Json::htmlEncode( | ||
| array_merge( | ||
| $this->getClientOptions($object), | ||
| ['filterOnFocusOut' => $object->filterOnFocusOut], | ||
|
Check failure on line 66 in framework/jquery/grid/GridViewJqueryClientScript.php
|
||
| ), | ||
| ); | ||
|
|
||
| $view->registerJs("jQuery('#$id').yiiGridView($options);"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @link https://www.yiiframework.com/ | ||
| * @copyright Copyright (c) 2008 Yii Software LLC | ||
| * @license https://www.yiiframework.com/license/ | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace yii\jquery\validators; | ||
|
|
||
| use yii\base\Model; | ||
| use yii\helpers\Json; | ||
| use yii\validators\client\ClientValidatorScriptInterface; | ||
| use yii\validators\ValidationAsset; | ||
| use yii\validators\Validator; | ||
| use yii\web\View; | ||
|
|
||
| /** | ||
| * BooleanValidatorJqueryClientScript provides client-side validation script generation for boolean attributes. | ||
| * | ||
| * This class implements {@see ClientValidatorScriptInterface} to supply client-side validation options and register | ||
| * the corresponding JavaScript code for boolean validation in Yii2 forms using jQuery. | ||
| * | ||
| * @author Wilmer Arambula <[email protected]> | ||
| * @since 2.2.0 | ||
| */ | ||
| class BooleanValidatorJqueryClientScript implements ClientValidatorScriptInterface | ||
| { | ||
| public function getClientOptions(Validator $validator, Model $model, string $attribute): array | ||
| { | ||
| $options = [ | ||
| 'trueValue' => $validator->trueValue, | ||
|
Check failure on line 34 in framework/jquery/validators/BooleanValidatorJqueryClientScript.php
|
||
| 'falseValue' => $validator->falseValue, | ||
| 'message' => $validator->formatMessage( | ||
| $validator->message, | ||
| [ | ||
| 'attribute' => $model->getAttributeLabel($attribute), | ||
| 'true' => $validator->trueValue === true ? 'true' : $validator->trueValue, | ||
| 'false' => $validator->falseValue === false ? 'false' : $validator->falseValue, | ||
| ], | ||
| ), | ||
| ]; | ||
|
|
||
| if ($validator->skipOnEmpty) { | ||
| $options['skipOnEmpty'] = 1; | ||
| } | ||
|
|
||
| if ($validator->strict) { | ||
| $options['strict'] = 1; | ||
| } | ||
|
|
||
| return $options; | ||
| } | ||
|
|
||
| public function register(Validator $validator, Model $model, string $attribute, View $view): string | ||
| { | ||
| ValidationAsset::register($view); | ||
|
|
||
| $options = $this->getClientOptions($validator, $model, $attribute); | ||
|
|
||
| return 'yii.validation.boolean(value, messages, ' . Json::htmlEncode($options) . ');'; | ||
| } | ||
| } | ||
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.
Should it be for web application only?