Skip to content

Conversation

terabytesoftw
Copy link
Member

Q A
Is bugfix?
New feature? ✔️
Breaks BC?

This solution implements a way to make jQuery optional, while trying to keep BC as low as possible:

  • useJquery property is added to yii\console\Application and yii\web\Application this allows you to control whether jQuery is used or not.
  • ActiveForm.
  • ActiveField.
  • Gridview.
  • Validators.
  • Add 100% test coverage.
// Application configuration
return [
    'useJquery' => false, // Disable jQuery globally
];

// Or dynamically
Yii::$app->useJquery = false;

// With jQuery default behavior
$form = ActiveForm::begin([]);

// Without jQuery custom client script
$form = ActiveForm::begin(['clientScript' => ActiveFormClientScript::class]);

Two interfaces have been added to add the clientScript to validators, widgets, components, etc.

<?php

declare(strict_types=1);

namespace yii\validators\client;

use yii\base\Model;
use yii\validators\Validator;
use yii\web\View;

interface ClientValidatorScriptInterface
{
    public function getClientOptions(Validator $validator, Model $model, string $attribute): array;
    public function register(Validator $validator, Model $model, string $attribute, View $view): string;
}
<?php


declare(strict_types=1);

namespace yii\web\client;

use yii\base\BaseObject;
use yii\web\View;

interface ClientScriptInterface
{
    public function getClientOptions(BaseObject $object, array $options = []): array;
    public function register(BaseObject $object, View $view, array $options = []): void;
}

Fixed generation of labels, whether custom or not, when enclosedByLabel === false, for both checkbox() and radio().

To install the assets, i added php-forge/foxy, which i maintain and allows you to install assets easily using the composer installer. i think it would be great to use CDN to manage the assets, so we don't depend on anything external.

Issues related:

@Copilot Copilot AI review requested due to automatic review settings October 7, 2025 14:54
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces the ability to make jQuery optional in Yii2 while maintaining backward compatibility. The implementation adds a useJquery configuration property to applications and refactors widgets and validators to use client script interfaces, allowing for jQuery-free implementations.

Key changes:

  • Added useJquery property to web and console applications
  • Created client script interfaces for modular validation and widget functionality
  • Implemented jQuery-specific client scripts as separate classes
  • Moved jQuery-dependent test code to dedicated test files

Reviewed Changes

Copilot reviewed 71 out of 71 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
framework/web/Application.php Added useJquery property with default true
framework/console/Application.php Added useJquery property with default false
framework/widgets/ActiveForm.php Added client script interface support
framework/validators/Validator.php Added formatted client message method
Multiple validator files Refactored to use client script interfaces
jQuery client script classes New jQuery-specific implementations
Test files Reorganized tests and added jQuery-specific test coverage

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Copilot Copilot AI review requested due to automatic review settings October 7, 2025 15:13
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 71 out of 71 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Copilot Copilot AI review requested due to automatic review settings October 7, 2025 18:43
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 71 out of 71 changed files in this pull request and generated 1 comment.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

*
* @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.

What's the use for it in console application?

Copy link
Member Author

@terabytesoftw terabytesoftw Oct 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because you can use the validators in the console, and JS code will be generated, as in the issue described (#8452), by setting it to false by default, no assets will be registered and no JS code will be generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants