Dynamically adding input fields in Yii2

11,690

Solution 1

Its not what complex its looks like..

There are lots of solutions out there and you may find many examples..

see these example i've found for you.

See this fully working example where you can multiple address and you can change these fields to match fields what you need.

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

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

exmaple dynamic field creation with yii2

Solution 2

If anyone is here in 2019, have a look at Yii2 documentation - Here

$('#contact-form').yiiActiveForm('add', {
    id: 'address',
    name: 'address',
    container: '.field-address',
    input: '#address',
    error: '.help-block',
    validate:  function (attribute, value, messages, deferred, $form) {
        yii.validation.required(value, messages, {message: "Validation Message Here"});
    }
});

To enable client validation for these fields, they have to be registered with the ActiveForm JavaScript plugin

Share:
11,690
richp32
Author by

richp32

Updated on June 22, 2022

Comments

  • richp32
    richp32 almost 2 years

    I'm having some trouble with forms in Yii2. I need to create a form which includes three dropdown menus which ask the user what time(s) in a week work best for him or her. The first menu would be days of the week, and the next two would be start and end times.

    But my project is more ambitious than that. I need to also allow the user to specify any time configuration, and so the form must be expanded and made dynamic, allowing the user to add more dropdown menus to the form to specify more times.

    I've tried a lot of solutions, but the trouble seems to lie in having user-created input fields. I believe there was a way to do this in Yii 1, but Yii 2 was apparently such a code overhaul that old workarounds are rarely useful. I've investigated many extensions, particularly Krajee's extensions, and though many of them are very pretty and user-friendly, they don't offer dynamic field creation. I've tried some hacks myself, but nothing has worked, and I'm wondering if there's a way at all to create a form with a dynamic number of input fields in Yii 2. If not, I'm tempted to just write some simple Javascript and Php outside of Yii to get the job done, but I'd like to stay within the framework.

    • Kshitiz
      Kshitiz over 9 years
      found one 9lessons.info/2010/04/… Looking for better solution. I don't know above link will work or not
    • Kshitiz
      Kshitiz over 9 years
      Is there is any limitation for the total input generated?
    • richp32
      richp32 over 9 years
      There could be a limit, something that the user would never hit anyways. For example, I don't expect the user will specify 20 different time configurations.
    • Kshitiz
      Kshitiz over 9 years
      sanwebe.com/2013/03/… not found any way in yii2.
    • Kshitiz
      Kshitiz over 9 years
    • Pomme.Verte
      Pomme.Verte over 9 years
      I'm not sure I understand the question. Are you trying to add inputs on the fly client side and don't know how? Or are you having an issue upon form validation with fields that were dynamically created? etc?Also are you using an AR to validate your form or a form model?
    • Kshitiz
      Kshitiz over 9 years
    • Felipe
      Felipe over 9 years
      A framework is supposed to address the 80% of web apps that is common to all and make the other 20% possible for advanced users. I think that what you want is quite specific and you will probably need to write some custom javascript yourself.
    • Beowulfenator
      Beowulfenator almost 9 years
      I've had great success with this library: github.com/jdorn/json-editor.
  • sadlyfe
    sadlyfe over 3 years
    What do the input keywords stand for? No explanation or documentation on that example exists.