Skip to content

Cannot read/get the value of FormFields #2281

@Anubarak

Description

@Anubarak

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions