Question

I'm adding multiple dropdown to form, but i would like to have some values selected by default.

form->addField('DropDown', 'fieldname','Field Caption')->setAttr('multiple')->setModel('SomeModel');

So when the user would open this form, values with id 1,5 and 6 would be selected. I checked internet, but didn't find any examples for this..

Also checked atk4\lib\Form\Field\DropDown.php, but couldn't find the solution either :(

I know the usual usage for forms is like this:

$form->addField('line','name')->set('John');

But id doesn't work here?

Thanks in advance!

solution:

if($campaign->get('labels')){
$jsfunction = 'var values="'.$campaign->get('labels').'";$.each(values.split(","), function(i,e){$("#nottyfly_createcampaign_form_labels option[value=\'" + e + "\']").prop("selected", true);})';
  $this->js(true,$jsfunction);
}
Était-ce utile?

La solution 2

Here is quick code:

$jsfunction = 'var values="'.$campaign->get('labels').'";$.each(values.split(","), function(i,e){$("#nottyfly_createcampaign_form_labels option[value=\'" + e + "\']").prop("selected", true);})';
$this->js(true,$jsfunction);

Autres conseils

This is not implemented in ATK4 Form_Field_DropDown class, but I guess you can do something with jQuery (JavaScript) in this case - select() values on document/field onLoad event with JS.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top