'How to check checkbox by default?

i want to check checkbox by default, but i could not find how to do it, in the phpword documentation there is no mention about it, found one option using addFormField, but it does not work like checkbox.



Solution 1:[1]

I'm using:

$table = $section->addTable();
$table->addRow();
$cell = $table->addCell(11000);
$textrun = $cell->addTextRun();
$textrun->addFormField('checkbox')->setValue('<w:sym w:font="Wingdings" w:char="F0FE"/>');
$textrun->addText(htmlspecialchars(' Checkbox 1 '));

->setDefault() not working, but this solution works fine!

Solution 2:[2]

use "jumpski answer" if the code has undefined error, add this block code in PhpWord/Element/CheckBox.php

 public function setDefault($default = false)
        {
            $this->default = $default;
        }

Solution 3:[3]

this way if you using PhpWord template TemplateProcessor , you can just pass this Code XML as value to your parameter ${checkedBox} in Template

 use PhpOffice\PhpWord\TemplateProcessor;
 
  $templateProcessor = new TemplateProcessor('yourTemplate.docx');
   $checkedBox='<w:sym w:font="Wingdings" w:char="F0FE"/>';
   $unCheckedBox = '<w:sym w:font="Wingdings" w:char="F0A8"/>';
  $templateProcessor->setValue('checkBox',$checkedBox);
  
  
  
  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jumpski
Solution 2 monireh bastami
Solution 3