Jump to content

API Fed Dropdown Select Value Not Recorded on Formbuilder Submission


cosmicsafari
 Share

Recommended Posts

Hi all,

Just a little context, I have need of a select/drop down on a form to update automatically.

So I believed I had achieved this, by programmatically updating said field via an webhook, code included below.

Which looked like it worked, as all the correct options do now infact show in the drop down on page load....

However, whenever I select one of these newly added options on the drop down and hit submit, it always throws an error as if the field has infact been left blank?

Out of curiosity I removed the 'Required' settings from the field, which when I tested does allow me to select a development and then submit without the error, however if you then check the entries tab within Formbuilder that field is just left blank?

I also removed the webhook for dynamic population wherein the field on the form started behaving normally again, so presumably something below is causing the problem?

Any ideas?

    $wire->addHookBefore('InputfieldSelect::render', function($event) {

        $inputfield = $event->object;

        if($inputfield->name != 'priority_development') return;

        $options = $inputfield->options; // Get the existing options

        //GET CURRENT PUBLISHED DEVELOPMENTS
        $options_new = [];
        $pages = Processwire\wire('pages')->find('template=development');
        foreach($pages as $page){
            $region = $page->development_region->title;
            $options_new[$page->id] = $region.', '.$page->title;
        }
        asort($options_new);

        $inputfield->options = []; // Remove all the existing options

        // Add the options back with attributes
        $inc = 0;
        foreach($options_new as $value => $label) {
            // Set whatever attributes you want as $key => $value in the last argument of addOption()
            if($inc == 0) {
                $inputfield->addOption($value, $label, array('selected' => 'selected'));
            } else {
                $inputfield->addOption($value, $label);
            }
            $inc++;
        }
        $event->return = $inputfield;
    });

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...