Dynamically add input fields in Yii2

I am having problems with forms in Yii2. I need to create a form that includes three drop-down menus that ask the user what time (s) per week is best for him or her. The first menu is the days of the week, and the next two are the start and end times.

But my project is more ambitious. I also need to allow the user to specify any temporary configuration, and therefore the form must be expanded and made dynamic, allowing the user to add several drop-down menus to the form to indicate more time.

I tried many solutions, but the problem seems to be to create custom input fields. I believe there was a way to do this in Yii 1, but Yii 2 seems to have been so redesigned by code that old workarounds are rarely useful. I researched many extensions, in particular Krajee extensions, and although many of them are very beautiful and user-friendly, they do not offer the creation of a dynamic field. I tried some hacks myself, but nothing worked, and I wonder if there is any way to create a form with a dynamic number of input fields in Yii 2. If not, I am tempted to just write some simple Javascript and Php outside of Yii to execute this work, but I would stay within.

+6
source share
2 answers

It doesn't look that hard.

There are many solutions, and you can find many examples.

see this example i found for you.

See this fully working example where you can specify multiple addresses, and you can change these fields to fit the fields you need.

http://wbraganca.com/yii2extensions/dynamicform-demo1/create

https://github.com/wbraganca/yii2-dynamicform

exmaple creating a dynamic field with yii2

+1
source

When working with a complex form, fields can be added (or removed) dynamically from the form. New fields must also be confirmed.

This link demonstrates an example scenario in which you need to solve the problem of checking dynamic fields.

0
source

Source: https://habr.com/ru/post/975450/


All Articles