yii2support icon indicating copy to clipboard operation
yii2support copied to clipboard

View require variable bug

Open sokyrko opened this issue 8 years ago • 13 comments

Hi! In my view used a static variable from class instance, e.g: $modelPayment::$typeDefault => 'Use as my mailing address' But around "render" method call in my controller I see error "View require typeDefault parameter". Thanx!

sokyrko avatar Feb 10 '17 14:02 sokyrko

Hi! Bug is actually in latest releace?

nvlad avatar Apr 05 '17 14:04 nvlad

Yeap. image

sokyrko avatar Apr 05 '17 14:04 sokyrko

Please send using variable code part for test and IDE version

nvlad avatar Apr 05 '17 14:04 nvlad

IDE version: 2017.1

signup.php ... [ $modelPayment::$typeDefault => 'Type default', ... ] ...

sokyrko avatar Apr 10 '17 07:04 sokyrko

signup.php

$modelPayment = new ModelPayment();
$typeDefault = '123123';
$typeDefault2 = '123123';

$tmp = [
    $modelPayment::$typeDefault  => '123123',
    $modelPayment::$typeDefault2  => '123123',
];
$this->render('signup', ['loginForm' => $loginForm])

inspection is not mark this code as error

nvlad avatar Apr 11 '17 12:04 nvlad

He ment that key stored in constant. I acquire this bug.

insperedia avatar Jul 11 '17 15:07 insperedia

I see a close bug. Plugin shows my view requires variables: image but really these variables are just parameters of closure: image PhpStorm 2017.1.4

mike-kramer avatar Jul 15 '17 22:07 mike-kramer

Thanks for your contribution.

insperedia avatar Jul 16 '17 17:07 insperedia

Also, if in the view file I have something like this:

if (!isset($items)) {
    $items = [];
}

Then I don't think the $items variable should be required. Is it possible?

pappfer avatar Nov 14 '17 07:11 pappfer

v. 0.10.57.23

public function actionTest(): string {
    return $this->render('test', [
        'data' => Test::test()
    ]);
}

This View does not use parameters

However, the variable is used. I use DatePicker widget

<?= DatePicker::widget([
    'name' => 'date',
    'pluginOptions' => [
        'startDate' => array_shift($data),
	'endDate' => array_pop($data)
    ]
]); ?>

s1lver avatar May 21 '19 09:05 s1lver

Looks like this has been left for some time (appreciate the project hasn't been worked on for a while). However, like a number of issues still open here, the main problem here seems to have been fixed. At least, a closure in a view with arguments does not cause a warning that required parameters are not sent.

What is still broken is the behaviour described by @pappfer where view parameters cannot be optional. This is a real pain as it is common to use optional parameters in partials or shared views.

To fix this, I suggest that type annotations that could be null should not cause warnings, e.g. in view:

/* @var $this \yii\web\View */
/* @var $email string */
/* @var $name string|null */
...

Then render call as follows should not show a warning:

$this->render('my-view', [
    'email' => '[email protected]',
]);

spikyjt avatar Nov 29 '19 09:11 spikyjt

@spikyjt doesn't seem to work for me. Even if I put type annotations in my view, the render() method is still highlighted in red in the controller saying it requires that parameter.

pappfer avatar Nov 29 '19 10:11 pappfer

@pappfer please read my comment. I said that the behaviour you previously described still doesn't work. I then made a suggestion to improve it, but that has not been implemented yet. Are you saying closure arguments or class static vars still cause a warning?

spikyjt avatar Nov 29 '19 11:11 spikyjt