8

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.

9
  • found one 9lessons.info/2010/04/… Looking for better solution. I don't know above link will work or not Commented Sep 18, 2014 at 6:12
  • Is there is any limitation for the total input generated? Commented Sep 18, 2014 at 6:21
  • 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. Commented Sep 18, 2014 at 15:00
  • sanwebe.com/2013/03/… not found any way in yii2. Commented Sep 19, 2014 at 12:10
  • 1
    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? Commented Sep 19, 2014 at 18:28

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

What do the input keywords stand for? No explanation or documentation on that example exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.