Jump to content

Inputfield Checkboxes list


Recommended Posts

I had a look at this, and it seems it isn't possible to directly set the optionAttributes property of inputfields that extend InputfieldSelect. That is probably why @kixe made this module.

But what you can do is use a hook to remove the existing options, and then add them back with some attributes...

$wire->addHookBefore('InputfieldCheckboxes::render', function(HookEvent $event) {
    $inputfield = $event->object;
    // Only for a specific field
    if($inputfield->hasField != 'your_field_name') return;
    $options = $inputfield->options; // Get the existing options
    $inputfield->options = []; // Remove all the existing options
    // Add the options back with attributes
    foreach($options as $value => $label) {
        // Set whatever attributes you want as $key => $value in the last argument of addOption()
        $inputfield->addOption($value, $label, ['disabled' => 'disabled', 'data-foo' => 'bar', 'class' => $this->sanitizer->pageName($label, true)]);
    $event->return = $inputfield;


  • Like 2
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By Ksenia
      Hello all! I am pretty new to PW and now trying to figure out how to build a filter that uses AND logic and includes many fields. With help I have figured out Page Reference fields and Repeaters, but simple Text Filed with dropdown text tags makes me confused.
      As I think of it, first I need to get the array of given fixed options of Text Tags here:

      And then I just need to print it out as checkbox form and circle through each page to see if they have the checked value in place of this field. 
      Like what I have with page references:

      But for the life of me I cannot figure out how to get this list...So if you have suggestions, I would really appreciate it!
    • By jacmaes
      I have a series of videos, and the following search form (translated into English here) that allows to filter these videos on the frontend:

      I've built a few of these search forms, but only with text fields, selects and radio buttons. Here I'm using an array with checkboxes ("Level" field above), and it's causing me grief when I try to paginate these results. I've done a lot of searching in the forum and spent too many hours to try to get it to work. Here's how I'm building the selector:
      <?php if(count($input->get)): // Level is an array. Code adapted from Ryan's snippet here: // https://processwire.com/talk/topic/3472-enable-pagination-for-search-results/?tab=comments#comment-38042 if($input->get->level) { $level = array(); foreach($input->get->level as $id) $level[] = (int) $id; // sanitize to INTs $level = implode('|', $level); // convert to 123|456|789 string, ready for selector } else { $level = ''; } $data = array( 'training_type' => array('=', (int) $input->get->training_type), 'duration' => array('=', (int) $input->get->duration), 'level' => array('=', $level), 'limit' => array('=', (int) $input->get->limit) ); $selector = ''; // iterate through the $data we made above to create a selector string foreach($data as $field => $a) { list($operator, $value) = $a; if(empty($value)) continue; // send value to the whitelist so that it can be used in pagination $input->whitelist($field, $value); // append to our selector string $selector .= "$field$operator$value, "; } $videos = $page->children("$selector"); When I hit search, I get the expected results. So far so good. The GET parameters are the following with the options selected in the screenshot above:
      videos/?level[]=1476&level[]=1477&training_type=1473&duration=1485&limit=10 $selector echoes the following as the "level" field is an array with a pipe character:
      level=1476|1477, training_type=1473, duration=1485, limit=10 Now, when paginating these results, the following page (page 2) shows these GET parameters:
      videos/page2/?level=1476|1477&training_type=1473&duration=1486&limit=10 And I think that's where the problem lies. The "level" field is "lost" and I'm getting more results than expected on subsequent pages. If I manually add "page2" to the initial results in the URL, just to test, everything works fine:
      videos/page2/?level[]=1476&level[]=1477&training_type=1473&duration=1486&limit=10 But how can I achieve this in code? Do I need to revert to "level[]=1475&level[]=1477" instead of "level=1476|1477" for the pagination to work correctly, and can you PHP gods illuminate me?
      Any help would be really appreciated, really.
    • By rick
      Howdy all!
      In my process module I am creating a dynamic form which contains a variable number of checkboxes. The form displays correctly, and all but the selected checkboxes are saved. Tracy and the network show the array being passed in the post back to my process module. The target field was created as Select Options type, and the input is Checkboxes (Multiple values). I have tried the following without success:
      $p->of(false); $p->myOptionsField->removeAll(); // remove previous selected options foreach( $p->fields as $fld ) { // iterate page fields if( $fld->name == 'title' ) continue; // skip page title field if( $fld->name == 'myOptionsField' ) { bd( $input->post('myOptionsField') ); // Shows correct array of selected options $p->set( $fld->name, $input->post($fld->name) ); // No error produced //$p->myOptionsField = $input->post('myOptionsField'); // No error produced //$p->myOptionsField = $input->post($fld->name)->value; // Property of non object error } else { $p->set( $fld->name, $input->post($fld->name) ); // other fields are saved to database } } $p->save(); Note: I've also gotten an error about the sleepValue of the selectableOptionArray, but I don't remember what was wrong with the code to produce it. I left the tags in for this post to help others searching.
      Can someone please tell me how I should be doing this. I know it will be simple, as it always is, but my eyes are crossed.
    • By jacmaes
      I'm sure it's been asked before on the forums, but I haven't found a relevant answer after multiple searches.  I have a form on the frontend that mirrors the user profile on the backend. After logging in, I want the user to be able to edit multiple fields on his or her profile. For simple text entries (e.g. First Name, Street Address, Phone Number or Email), and single options (e.g. radio buttons for Gender: male or female or select dropdowns for a Country list), I have no issue and everything works fine: the user first fills out the form then can go back and update whichever field they want. That's standard profile editing stuff from the frontend.
      But I hit a roadblock when I want to offer multiple choices. For example, I have a Select Options field set up to show multiple checkboxes for choosing various types of driver's licences. With the following code (edited for brevity):
      $licence_types = array('B1', 'B', 'A1', 'C1', 'D1'); // same options as defined in my select options field foreach($licence_types as $licence) { $content .= "<label><input type='checkbox' name='licences[]' value='$product'> $licence</label>"; } .... $user->of(false); $user->licences = $licences; $user->save(); $user->of(true); the user can successfully submit this info from the frontend form:

      But now how do I show them the result  of what they submitted when they go back and want to update their profile from the same form? In other words, how do I insert the checked='checked' (or simply 'checked') HTML in the previously selected checkboxes?  For example, if they selected the first two options –B1 and B– in the example above, how do I make these two checkboxes ticked?
      With the following loop, I can show the previously selected checkboxes:
      foreach($user->licences as $licence) { $content .= "<label for='$licence->id'> <input name='licences[]' type='checkbox' id='$licence->id' value='$licence->id' checked>$licence->title</label>"; }  
      How do I merge the two loops so that they see this, as expected:

      Maybe with in_array()? Any help would be greatly appreciated.
    • By Mirza
      Hi All,
      Current I have a field with page reference type.
      Which is showing everything in one column like
      How can we specify the check boxes to show like 
      1    2
      3    4
      5    6
      is it possible to obtain these in the admin panel?

  • Create New...