Yii2: StringValidator crashes if \Yii::$app is not set.

Created on 20 Dec 2016  路  7Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

Create a StringValidator instance without having Yii::$app set.

What is the expected result?

String validation should work fine without an app.

What do you get instead?

Exception trying to get property of non-object.

Additional info

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

bug

Most helpful comment

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.

All 7 comments

This actually applies to the base Validator:
$model->addError($attribute, Yii::$app->getI18n()->format($message, $params, Yii::$app->language));

@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:

  • I fully understand if the team doesn't want to spend time on this.
  • I am still wondering if you would accept pull requests that change validators and / or helpers to do a "check before use" on \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:

  • All validators used to call Yii::$app->getI18n()->format($message, $params, Yii::$app->language)
  • I introduced 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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

indicalabs picture indicalabs  路  3Comments

newscloud picture newscloud  路  3Comments

kminooie picture kminooie  路  3Comments

SamMousa picture SamMousa  路  3Comments

nokimaro picture nokimaro  路  3Comments