Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read/get the value of FormFields #2281

Open
Anubarak opened this issue Aug 19, 2022 · 1 comment
Open

Cannot read/get the value of FormFields #2281

Anubarak opened this issue Aug 19, 2022 · 1 comment

Comments

@Anubarak
Copy link

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
@vincentKool
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants