PHPWord icon indicating copy to clipboard operation
PHPWord copied to clipboard

Cannot read/get the value of FormFields

Open Anubarak opened this issue 3 years ago • 1 comments

Describe the Bug

I try to get the selected / filled values from FormFields in my word document. But whatever I try - it fails. When I create a word document and add Form Controls by myself, the getElements() function always skips them and cannot read them. When I add formFields via function, they are always considered as Text instead of a control field.

Steps to Reproduce

Please provide a code sample that reproduces the issue.

<?php
require __DIR__ . '/vendor/autoload.php';

$dir = __DIR__ . '/helloWorld.docx';

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addFormField('dropdown')
    ->setEntries(['Choice 1', 'Choice 2', 'Choice 3', ''])
    ->setValue(1);

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord );
$objWriter->save($filePath);

// try to read it...
$word = IOFactory::load($filePath);
foreach ($word->getSection(0)->getElements() as $element){
    echo = $element::class;  // prints "PhpOffice\PhpWord\Element\PreserveText"
    if($element instanceof PreserveText){
        var_dump($element->getText()); // prints [ 0 => '{FORMDROPDOWN}' ]
    }
}

Expected Behavior

->getElements() should return an array with a PhpOffice\PhpWord\Element\FormField instead of a PhpOffice\PhpWord\Element\PreserveText

Current Behavior

I cannot read the inputs, it's only text

Context

Please fill in your environment information:

  • PHP Version: 8,0
  • PHPWord Version: 0.18.3

Anubarak avatar Aug 19 '22 13:08 Anubarak

I created a Pull request #2282 to fix this behaviour. Feel free to test this and see if this fixes your problem

vincentKool avatar Aug 21 '22 11:08 vincentKool