0

We got a custom jform field as below

    <field name="select_course" 
           type="courselist"
           label="Select Course" 
           multiple="true">
    </field>

Assume the list got the below options

<option value="16">9th Standard</option>
<option value="17">10th Standard</option>
<option value="18">8th Standard</option>
<option value="19">7th Standard</option>

For some reason, we have to select some of the options as selected. We want to do that on the view directly. That means, the model returns the ids that needs to be selected. For example, the model will return an array containing 17 and 19.

In the view, we got the below code to mark those values as default

 $storedCourses = $this->storedCourses;
 foreach($storedCourses as $course){
   $this->form->setFieldAttribute('select_course', ***How can I set that option as selected**);
 }
 echo $this->form->getControlGroup('select_course')

How to I set those options as selected?

0

You can use Joomla\CMS\Form\Form::setValue().

$this->form->setValue('select_course', null, [17, 19]);
| improve this answer | |
  • Sharky, your answer has been auto-flagged as low quality by the Stack Exchange system due to its length/quality. Please edit your answer to be more generous/educational. Resolving an issue is just the beginning of a great answer. Please include a link to relevant documentation, a plain English explanation of what the syntax means, or any insights you have based on personal experience. Ideally, I'd prefer that you offer all of these things. Not only will this benefit the OP and thousands of future researchers, this will role model good posting for other volunteers. Thank you. – mickmackusa Apr 18 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.