Yii2: Pass form instance to widget.

Created on 22 Dec 2016  路  13Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

Use a file input widget instead of the built in input field.

Related to #8779

What is the expected result?

The widget should (be able to) set the enctype for the form.

What do you get instead?

The InputWidget doesn't get a reference to the form it is used in, so it cannot change the forms enc type.

Proposed solution

  • [ ] Add $config['form'] to yii\widget\ActiveField::widget().
  • [ ] Add $form property to yii\widgets\InputWidget.
  • [ ] Add hasForm() method to yii\widgets\InputWidget, analogous to hasModel().
  • [ ] Add tests.

This will allow input widgets to alter the form in which they are embedded; it will add virtually no overhead for people not using it. It will not break BC.

enhancement

Most helpful comment

InputWidget should be tied to ActiveField instead of ActiveForm as ActiveField::$form already allows getting form from field.

class MyInputWidget extends InputWidget
{
    public function run()
    {
        $form = $this->field->form;
    }
}

All 13 comments

its a bit of a pain to pass "form" to all the widgets..

i overwrited the controller widget stack to allow me to find the first form so i could access its from out there.

$widget->owner->geWidgetFromStack(Form::class)

I considered that as well. I'd prefer to have it standardized inside the framework though.

I agree it to be logical to have a mutual connection between related objects like a Form and Fields. Enough usecases can be imagined. It seems to be a BC break though for those not using InputWidget as a widget tied to ActiveField.

@dynasource It doesn't break BC, the form parameter is optional, this is basically the same approach as used by the various parts of Yii that work both with and without a model. --> See my original post.

whats the usecase to use it without the inputfield?

@TerraSkye I don't understand what you mean.

In what usecase can u not use form->textfield->widger

InputWidget should be tied to ActiveField instead of ActiveForm as ActiveField::$form already allows getting form from field.

class MyInputWidget extends InputWidget
{
    public function run()
    {
        $form = $this->field->form;
    }
}

That's fine with me as well, @klimov-paul how do you propose to implement that?

Same as my proposal for form but then replaced with field?

  • hasField
  • public $field
    ?
class InputWidget extends Widget
{
    // ...
    public $field;
    // ...
}

class ActiveField  extends Component
{
    // ...
    public function widget($class, $config = [])
    {
        $config['field'] = $this;
        // ...
    }
}

Looks good to me, want me to make a PR or will you just push the changes directly?

One way or another it is the matter for 2.0.12 and will be considered only after 2.0.11 release.

Resolved by commit 3946ac0dabf3cfce62ed251b89d6b66aa422e3be

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Locustv2 picture Locustv2  路  3Comments

skcn022 picture skcn022  路  3Comments

schmunk42 picture schmunk42  路  3Comments

indicalabs picture indicalabs  路  3Comments

SamMousa picture SamMousa  路  3Comments