HELPME
HELPME

Reputation: 744

'value' function doesn't work in Yii2 view

in my view I'm trying to add 'value' function, which shows the field if the id = 0, otherwise it doesn't show it. I'm trying to do it like so:

echo $form->field($model, 'test', [
'options' => [
    'class' => $twoColumns . ' required',
    'value' => function ($model) {
        return $model->testvar == 0 ? 'Test' : null;
    }
]
])

I've added function into 'value', but somehow it doesn't work. Could someone explain me why?

I'm getting htmlspecialchars error

Upvotes: 0

Views: 34

Answers (1)

Bizley
Bizley

Reputation: 18021

Closure here is not supported and there is no reason to use it anyway.

You only need something like:

$model->interest = null;
if ($model->is_interest_variable == 0) {
    $model->interest = 'Test';
}
echo $form->field($model, 'interest', [
'addon' => [
    'append' => [
        'content' => '%',
    ],
],
'options' => [
    'class' => $twoColumns . ' required',
]
])->textInput([
    'placeholder' => '0.0000', 
    'class' => 'form-control amount-4'
]); 

Upvotes: 3

Related Questions