Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
8b4ca6e
Allow optional jQuery in Validators, widgets and components.
terabytesoftw Sep 15, 2025
21ee13e
Fix tests lint.
terabytesoftw Sep 15, 2025
5ea9707
Fix tests lint.
terabytesoftw Sep 15, 2025
921b089
Fix phpdoc.
terabytesoftw Sep 15, 2025
e0d40e7
Add jQuery integration flag for console application compatibility.
terabytesoftw Sep 16, 2025
8748789
Add code coverage `100%` for `ActiveFormJqueryClientScript`, `ActiveF…
terabytesoftw Sep 17, 2025
1d6ddb9
Update asset manager hash callback and adjust script paths in `Active…
terabytesoftw Sep 17, 2025
dbd7760
Update `composer.lock`.
terabytesoftw Sep 17, 2025
8a9c70a
Update PHPCS configuration to exclude additional false positives and …
terabytesoftw Sep 17, 2025
bf198b9
Refactor validators to return `null` instead of an empty string and s…
terabytesoftw Sep 17, 2025
514f1b1
Refactor `ActiveFormTest` to remove redundant server request URI and …
terabytesoftw Sep 17, 2025
ddf36bf
Add code coverage `100%` for `BooleanValidator` and `BooleanValidator…
terabytesoftw Sep 17, 2025
a43c983
Enhance error message assertions in `BooleanValidatorJqueryClientScri…
terabytesoftw Sep 17, 2025
16830a1
Replace `assertEquals()` with `assertSame()` in `BooleanValidatorJque…
terabytesoftw Sep 17, 2025
30d0e32
Replace variable assignment with inline instantiation of `View` in `B…
terabytesoftw Sep 17, 2025
5b8aff9
Add code coverage `100%` for `CompareValidator` and `CompareValidator…
terabytesoftw Sep 17, 2025
8335d95
Fix PHPCs issues.
terabytesoftw Sep 17, 2025
693eb43
Add code coverage `100%` for `EmailValidator` and `EmailValidatorJque…
terabytesoftw Sep 18, 2025
342e715
Add coverage `100%` to `CompareValidator`.
terabytesoftw Sep 18, 2025
7e159cf
Refactor validation tests to improve clarity and consistency in error…
terabytesoftw Sep 19, 2025
fabb1e9
Refactor `EmailValidatorJqueryClientScriptTest` to enhance error mess…
terabytesoftw Sep 19, 2025
67ad445
Add code coverage `100%` for `FileValidatorJqueryClientScript`.
terabytesoftw Sep 19, 2025
5ed8f23
Add code coverage `100%` for `FilterValidatorJqueryClientScript`.
terabytesoftw Sep 20, 2025
1b930df
Refactor client script initialization in validators to ensure proper …
terabytesoftw Sep 20, 2025
c4cbbb3
Add code coverage `100%` for `ImageValidatorJqueryClientScript`.
terabytesoftw Sep 20, 2025
b2cda69
Fix minor corrections.
terabytesoftw Sep 20, 2025
0175983
Fix style ci.
terabytesoftw Sep 20, 2025
1657033
Add code coverage `100%` for `IpValidatorJqueryClientScript`.
terabytesoftw Sep 20, 2025
4a366ef
Add code coverage `100%` for `NumberValidatorJqueryClientScript`.
terabytesoftw Sep 20, 2025
538918f
fix style ci.
terabytesoftw Sep 20, 2025
7d9bc31
Add code coverage `100%` for `RangeValidatorJqueryClientScript`.
terabytesoftw Sep 20, 2025
6afa219
Add code coverage `100%` for `RegularValidatorJqueryClientScriptTest`.
terabytesoftw Sep 20, 2025
b160cda
Add code coverage `100%` for `RequireValidatorJqueryClientScript`.
terabytesoftw Sep 20, 2025
a2187a2
Add code coverage `100%` for `StringValidatorJqueryClientScript`.
terabytesoftw Sep 20, 2025
faceade
Add code coverage `100%` for `TrimValidatorJqueryClientScript`.
terabytesoftw Sep 21, 2025
da4625e
Add code coverage `100%` for `UrlValidatorJqueryClientScript`.
terabytesoftw Sep 21, 2025
b42e3ce
Add code coverage `100%` for `GridViewJqueryClientScript`.
terabytesoftw Sep 22, 2025
d8a8761
Fix minor corrections.
terabytesoftw Sep 22, 2025
1ff3c56
Add tests for 'getClientOptions()' method in `GridViewJqueryClientScr…
terabytesoftw Sep 22, 2025
abab38b
Add code coverage `100%` for `CheckboxColumnJqueryClientScript`.
terabytesoftw Sep 24, 2025
ca13ed9
Captcha widget support for vanillaJS should be added in another PR.
terabytesoftw Sep 24, 2025
68e1499
More cleanup.
terabytesoftw Sep 24, 2025
02bfb78
Merge branch '22.0' into allow-jquery-optional.
terabytesoftw Oct 1, 2025
a9dc462
Merge branch '22.0' into allow-jquery-optional.
terabytesoftw Oct 1, 2025
b3764b6
Merge branch '22.0' into allow-jquery-optional.
terabytesoftw Oct 2, 2025
8ce7391
Merge branch '22.0' into allow-jquery-optional.
terabytesoftw Oct 2, 2025
72fabb7
Apply fixed review Copilot.
terabytesoftw Oct 2, 2025
94ac107
Merge branch '22.0' into allow-jquery-optional
terabytesoftw Oct 7, 2025
343f8c6
Refactor ActiveField validation methods to protected and add helper m…
terabytesoftw Oct 7, 2025
184c51b
Apply fixed Copilot review.
terabytesoftw Oct 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion framework/base/Application.php
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
Expand Down Expand Up @@ -182,7 +183,16 @@ abstract class Application extends Module
* @var array list of loaded modules indexed by their class names.
*/
public $loadedModules = [];

/**
* Indicates whether jQuery should be used for client-side validation and widgets.
*
* When enabled, jQuery assets will be registered for validators and widgets that require it.
*
* Set to `false` to disable automatic jQuery inclusion for these features.
*
* @since 2.2.0
*/
public bool $useJquery = true;
Copy link
Member

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?


/**
* Constructor.
Expand Down
11 changes: 10 additions & 1 deletion framework/console/Application.php
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
Expand Down Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The 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}
Expand Down
27 changes: 17 additions & 10 deletions framework/grid/CheckboxColumn.php
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
Expand All @@ -8,9 +9,12 @@
namespace yii\grid;

use Closure;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\jquery\grid\CheckboxColumnJqueryClientScript;
use yii\web\client\ClientScriptInterface;

/**
* CheckboxColumn displays a column of checkboxes in a grid view.
Expand Down Expand Up @@ -74,7 +78,10 @@ class CheckboxColumn extends Column
* @since 2.0.9
*/
public $cssClass;

/**
* Client script class to use for client-side validation.
*/
public array|ClientScriptInterface|null $clientScript = null;

/**
* {@inheritdoc}
Expand All @@ -90,6 +97,11 @@ public function init()
$this->name .= '[]';
}

if (Yii::$app->useJquery && !$this->clientScript instanceof ClientScriptInterface) {
$this->clientScript ??= ['class' => CheckboxColumnJqueryClientScript::class];
$this->clientScript = Yii::createObject($this->clientScript);
}

$this->registerClientScript();
}

Expand Down Expand Up @@ -139,7 +151,7 @@ protected function renderDataCellContent($model, $key, $index)
* @return string header checkbox name
* @since 2.0.8
*/
protected function getHeaderCheckBoxName()
public function getHeaderCheckBoxName()
{
$name = $this->name;
if (substr_compare($name, '[]', -2, 2) === 0) {
Expand All @@ -160,13 +172,8 @@ protected function getHeaderCheckBoxName()
*/
public function registerClientScript()
{
$id = $this->grid->options['id'];
$options = Json::encode([
'name' => $this->name,
'class' => $this->cssClass,
'multiple' => $this->multiple,
'checkAll' => $this->grid->showHeader ? $this->getHeaderCheckBoxName() : null,
]);
$this->grid->getView()->registerJs("jQuery('#$id').yiiGridView('setSelectionColumn', $options);");
if ($this->clientScript instanceof ClientScriptInterface) {
$this->clientScript->register($this, $this->grid->getView());
}
}
}
36 changes: 19 additions & 17 deletions framework/grid/GridView.php
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
Expand All @@ -12,9 +13,9 @@
use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\i18n\Formatter;
use yii\jquery\grid\GridViewJqueryClientScript;
use yii\web\client\ClientScriptInterface;
use yii\widgets\BaseListView;

/**
Expand Down Expand Up @@ -264,7 +265,10 @@ class GridView extends BaseListView
* - `{pager}`: the pager. See [[renderPager()]].
*/
public $layout = "{summary}\n{items}\n{pager}";

/**
* Client script class to use for client-side validation.
*/
public array|ClientScriptInterface|null $clientScript = null;

/**
* Initializes the grid view.
Expand All @@ -285,6 +289,11 @@ public function init()
$this->filterRowOptions['id'] = $this->options['id'] . '-filters';
}

if (Yii::$app->useJquery && !$this->clientScript instanceof ClientScriptInterface) {
$this->clientScript ??= ['class' => GridViewJqueryClientScript::class];
$this->clientScript = Yii::createObject($this->clientScript);
}

$this->initColumns();
}

Expand All @@ -293,11 +302,10 @@ public function init()
*/
public function run()
{
$view = $this->getView();
GridViewAsset::register($view);
$id = $this->options['id'];
$options = Json::htmlEncode(array_merge($this->getClientOptions(), ['filterOnFocusOut' => $this->filterOnFocusOut]));
$view->registerJs("jQuery('#$id').yiiGridView($options);");
if (Yii::$app->useJquery && $this->clientScript instanceof ClientScriptInterface) {
$this->clientScript->register($this, $this->getView());
}

parent::run();
}

Expand Down Expand Up @@ -333,17 +341,11 @@ public function renderSection($name)
*/
protected function getClientOptions()
{
$filterUrl = isset($this->filterUrl) ? $this->filterUrl : Yii::$app->request->url;
$id = $this->filterRowOptions['id'];
$filterSelector = "#$id input, #$id select";
if (isset($this->filterSelector)) {
$filterSelector .= ', ' . $this->filterSelector;
if (Yii::$app->useJquery == false || $this->clientScript === null) {
return [];
}

return [
'filterUrl' => Url::to($filterUrl),
'filterSelector' => $filterSelector,
];
return $this->clientScript->getClientOptions($this);
}

/**
Expand Down
48 changes: 48 additions & 0 deletions framework/jquery/grid/CheckboxColumnJqueryClientScript.php
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

View workflow job for this annotation

GitHub Actions / PHP 8.3-PHPStan

Access to an undefined property yii\base\BaseObject::$grid.

Check failure on line 31 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-PHPStan

Access to an undefined property yii\base\BaseObject::$grid.

Check failure on line 31 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-PHPStan

Access to an undefined property yii\base\BaseObject::$grid.

Check failure on line 31 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.2-PHPStan

Access to an undefined property yii\base\BaseObject::$grid.

$options = Json::encode(
[
'name' => $object->name,

Check failure on line 35 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.3-PHPStan

Access to an undefined property yii\base\BaseObject::$name.

Check failure on line 35 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-PHPStan

Access to an undefined property yii\base\BaseObject::$name.

Check failure on line 35 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-PHPStan

Access to an undefined property yii\base\BaseObject::$name.

Check failure on line 35 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.2-PHPStan

Access to an undefined property yii\base\BaseObject::$name.
'class' => $object->cssClass,

Check failure on line 36 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.3-PHPStan

Access to an undefined property yii\base\BaseObject::$cssClass.

Check failure on line 36 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-PHPStan

Access to an undefined property yii\base\BaseObject::$cssClass.

Check failure on line 36 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-PHPStan

Access to an undefined property yii\base\BaseObject::$cssClass.

Check failure on line 36 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.2-PHPStan

Access to an undefined property yii\base\BaseObject::$cssClass.
'multiple' => $object->multiple,

Check failure on line 37 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.3-PHPStan

Access to an undefined property yii\base\BaseObject::$multiple.

Check failure on line 37 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-PHPStan

Access to an undefined property yii\base\BaseObject::$multiple.

Check failure on line 37 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-PHPStan

Access to an undefined property yii\base\BaseObject::$multiple.

Check failure on line 37 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.2-PHPStan

Access to an undefined property yii\base\BaseObject::$multiple.
'checkAll' => $object->grid->showHeader ? $object->getHeaderCheckBoxName() : null,

Check failure on line 38 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.3-PHPStan

Call to an undefined method yii\base\BaseObject::getHeaderCheckBoxName().

Check failure on line 38 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.3-PHPStan

Access to an undefined property yii\base\BaseObject::$grid.

Check failure on line 38 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-PHPStan

Call to an undefined method yii\base\BaseObject::getHeaderCheckBoxName().

Check failure on line 38 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-PHPStan

Access to an undefined property yii\base\BaseObject::$grid.

Check failure on line 38 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-PHPStan

Call to an undefined method yii\base\BaseObject::getHeaderCheckBoxName().

Check failure on line 38 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-PHPStan

Access to an undefined property yii\base\BaseObject::$grid.

Check failure on line 38 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.2-PHPStan

Call to an undefined method yii\base\BaseObject::getHeaderCheckBoxName().

Check failure on line 38 in framework/jquery/grid/CheckboxColumnJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.2-PHPStan

Access to an undefined property yii\base\BaseObject::$grid.
],
);
$view->registerJs("jQuery('#$id').yiiGridView('setSelectionColumn', $options);");
}

public function getClientOptions(BaseObject $object): array
{
return [];
}
}
72 changes: 72 additions & 0 deletions framework/jquery/grid/GridViewJqueryClientScript.php
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

View workflow job for this annotation

GitHub Actions / PHP 8.3-PHPStan

Call to an undefined method yii\base\BaseObject::getView().

Check failure on line 57 in framework/jquery/grid/GridViewJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-PHPStan

Call to an undefined method yii\base\BaseObject::getView().

Check failure on line 57 in framework/jquery/grid/GridViewJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-PHPStan

Call to an undefined method yii\base\BaseObject::getView().

Check failure on line 57 in framework/jquery/grid/GridViewJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.2-PHPStan

Call to an undefined method yii\base\BaseObject::getView().

GridViewAsset::register($view);

$id = $object->options['id'];

Check failure on line 61 in framework/jquery/grid/GridViewJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.3-PHPStan

Access to an undefined property yii\base\BaseObject::$options.

Check failure on line 61 in framework/jquery/grid/GridViewJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-PHPStan

Access to an undefined property yii\base\BaseObject::$options.

Check failure on line 61 in framework/jquery/grid/GridViewJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-PHPStan

Access to an undefined property yii\base\BaseObject::$options.

Check failure on line 61 in framework/jquery/grid/GridViewJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.2-PHPStan

Access to an undefined property yii\base\BaseObject::$options.

$options = Json::htmlEncode(
array_merge(
$this->getClientOptions($object),
['filterOnFocusOut' => $object->filterOnFocusOut],

Check failure on line 66 in framework/jquery/grid/GridViewJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.3-PHPStan

Access to an undefined property yii\base\BaseObject::$filterOnFocusOut.

Check failure on line 66 in framework/jquery/grid/GridViewJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-PHPStan

Access to an undefined property yii\base\BaseObject::$filterOnFocusOut.

Check failure on line 66 in framework/jquery/grid/GridViewJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-PHPStan

Access to an undefined property yii\base\BaseObject::$filterOnFocusOut.

Check failure on line 66 in framework/jquery/grid/GridViewJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.2-PHPStan

Access to an undefined property yii\base\BaseObject::$filterOnFocusOut.
),
);

$view->registerJs("jQuery('#$id').yiiGridView($options);");
}
}
65 changes: 65 additions & 0 deletions framework/jquery/validators/BooleanValidatorJqueryClientScript.php
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

View workflow job for this annotation

GitHub Actions / PHP 8.3-PHPStan

Access to an undefined property yii\validators\Validator::$trueValue.

Check failure on line 34 in framework/jquery/validators/BooleanValidatorJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-PHPStan

Access to an undefined property yii\validators\Validator::$trueValue.

Check failure on line 34 in framework/jquery/validators/BooleanValidatorJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-PHPStan

Access to an undefined property yii\validators\Validator::$trueValue.

Check failure on line 34 in framework/jquery/validators/BooleanValidatorJqueryClientScript.php

View workflow job for this annotation

GitHub Actions / PHP 8.2-PHPStan

Access to an undefined property yii\validators\Validator::$trueValue.
'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) . ');';
}
}
Loading
Loading