Create a StringValidator instance without having Yii::$app set.
String validation should work fine without an app.
Exception trying to get property of non-object.
The cause is:
if ($this->encoding === null) {
$this->encoding = Yii::$app->charset;
}
I think it would be nice if there was a check to see if Yii::$app is set.
| Q | A
| ---------------- | ---
| Yii version | latest
| PHP version | N/A
| Operating system | N/A
This actually applies to the base Validator:
$model->addError($attribute, Yii::$app->getI18n()->format($message, $params, Yii::$app->language));
This is questionable issue.
There are many places around validators and helpers, which depend on application instance.
For example:
StringHelper: https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseStringHelper.php#L108 , https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseStringHelper.php#L111 , https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseStringHelper.php#L155 , https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseStringHelper.php#L212, https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseStringHelper.php#L237The most obvious one is yii\helpers\Url: https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseUrl.php#L136, https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseUrl.php#L142, https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseUrl.php#L219 and so on.
Yii architecture has a relatively high coupling between different classes - it is a price for the simpicity. I can see no way we can make each component trully independent without changing the main paradigm.
For me it is expected that I need Yii application instantiated before using most of the functionality.
@klimov-paul I agree, it is definitely not a "high priority" issue.
But less coupling is something to strive for in my opinion.
That said:
\Yii::$app.@klimov-paul In most of the places where the dependecy to Yii::$app is only for the charset we have a check already. There are only a few places like this one that are not covered. We can not and do not want to achive full independence but most components (if specially configured) can work without \Yii::$app without problem. E.g. a DbCache reads its db from Yii::$app->db but if the DB is explicitly configured, the component can be used independent. For the charset which is hardly ever different from UTF-8 a fallback is useful imo.
In most of the places where the dependecy to Yii::$app is only for the charset we have a check already.
Here is a Application::$language dependency:
https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseFileHelper.php#L96
For the charset which is hardly ever different from UTF-8 a fallback is useful imo.
For me the issue does not worth the effots, but I will not object if you wish to provide fallbacks for Application::$charset and Application::$language. However in this case fallback should cover every usage not just some of them.
@klimov-paul With every usage I hope you mean every usage inside a single class not inside all framework classes?
I have implemented a solution for all validators:
Yii::$app->getI18n()->format($message, $params, Yii::$app->language)Validator::format($message, $params), which forwards the call if Yii::$app exists and otherwise uses a simplified implementation (same as Yii::t currently does).This reduces the link to Yii::$app only 1 place in the base validator instead of repeated in each call to format. It also makes the dependency optional.
Most helpful comment
Here is a
Application::$languagedependency:https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseFileHelper.php#L96
For me the issue does not worth the effots, but I will not object if you wish to provide fallbacks for
Application::$charsetandApplication::$language. However in this case fallback should cover every usage not just some of them.