Jump to content

[SOLVED] How to select form fields displayed from checklist values


esspea
 Share

Recommended Posts

Hello,

I have a form, a simple radio button list of items populated by page list. 

I have a field, a checklist page list with the same pages (but not the same field)

What I want to do is have the radio button list only display items checked in the checklist.  I hope this makes sense ?

Now I have this hook, but it does not work:

$wire->addHookBefore('FormBuilderProcessor::renderReady', function($event) {
  $form = $event->arguments(0);
    $field = $form->getChildByName('formfield'); // get form field in this case a radio page array
    if(!$field) return;

    $parent = wire('page')->parent; //connect to parent page, that contains the checklist
    //echo $parent; //Returns the correct page


  $pages = $parent->get('parent_field'); //Get checked field values from parent page
  $sidur = explode(',', $pages);  //turns the "get list" into array

  if(is_array($sidur)) {

    foreach ($sidur as $basID) {
          $field->removeOption($basID);
    }
}
});

Nothing is removed.  Where does it go wrong?

Thanks!

 

Link to comment
Share on other sites

  • esspea changed the title to How to select form fields displayed from checklist values

I asked chatGPT for advice and after a few rounds of gradually more specific questions, I got this working.
Here is the resulting hook:

$wire->addHookBefore('FormBuilderProcessor::renderReady', function($event) {
  $form = $event->arguments(0);
    $field = $form->getChildByName('formfield'); // get form field in this case a radio page array
    if(!$field) return;

    $parent = wire('page')->parent; //connect to parent page
    //echo $parent; //Returns the correct page
  $pages = $parent->get('parent_field'); //Get field values from parent page
  $sidur = explode('|', $pages);  //echo $pages; //Returns the correct selected pages

  if(is_array($sidur)) {
     $newOptions = [];
     foreach ($sidur as $basID) {
       $page = wire('pages')->get($basID);
       if($page) {
         $newOptions[$page->id] = $page->title;
       }
     }

     usort($newOptions, function($a, $b) {
           return strcmp($a, $b);
         });

     $field->options = $newOptions;
   }
});

 

Link to comment
Share on other sites

  • esspea changed the title to [SOLVED] How to select form fields displayed from checklist values

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...