Jump to content

Saving multiple Checkbox values


rick
 Share

Recommended Posts

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.

 

 

Link to comment
Share on other sites

I'm wondering about this part...

1 hour ago, rick said:

I am creating a dynamic form which contains a variable number of checkboxes

If these options (checkboxes) are added dynamically, are all the options also defined in the selectable options for the Select Options field? You cannot save an option to a Select Options field if it is not one of the pre-configured selectable options.

If you want to dynamically add to the selectable options then this reply has some example code:

 

  • Like 1
Link to comment
Share on other sites

8 hours ago, Robin S said:

If these options (checkboxes) are added dynamically, are all the options also defined in the selectable options for the Select Options field? You cannot save an option to a Select Options field if it is not one of the pre-configured selectable options.

You are right. This is somewhat convoluted. However, I did manage to solve my problem using the information from the post you linked. I also remember now where that selectable options array error came from. With debug=true and advanced=true you normally hover over the collapse icon to view the field you use when defining your API fields. In this case the field shows as "_options". There is also a link to an article which describes the means by which you can manipulate this type of field. The article is incomplete, in that it does not describe checkboxes (multiple value). The selectable options error occurs when you try and use the article information with this type of field.

The following is how you can create a multiple selection field via API utilizing checkboxes should anyone else come across this.

if( !$f = $this->wire( 'fields' )->get( 'myOptionsField' ) ) { // check if field already exists
	$f = $this->wire( new Field );
	$f->name = 'myOptionsField';
	$f->label = __( 'My Options Field Label', __FILE__ );
	$f->description = __( 'My Options Field Description.', __FILE__ );
	$f->notes = __( "My Options Field Notes.\nCan be more than one line.", __FILE__ );

// We want this field to store our options
	$f->type = $this->wire( 'modules' )->get( "FieldtypeOptions" );

// You must save your field definition at this point, which is the same procedure through the admin interface.
// You cannot modify other field parameters until the field is first saved.
	$f->save();  

// assign a group tag to group like fields if you want.
	$f->tags = 'groupTag'; 
	$f->required = 1; // required field = 1

// This is the same as selecting Checkboxes (Multiple values) on details tab.
	$f->inputfieldClass = 'InputfieldCheckboxes';   

// Create a function (if needed) to retrieve your desired options from whatever source.
	$myOptions = $this->getMyOptions(); // return a sorted array of options

// Set the number of columns based on the number of options.
// Here I hardcoded 15 options as the break point for a new column.
	$f->optionColumns = ( count( $myOptions ) % 15 ) + 1; 

// Create a function (if needed) to convert your array of options to newline delimited string. 
	$myOptions = $this->getOptionString( $myOptions );   

// Fire up the option manager and add your option string to this field.
	$manager = new SelectableOptionManager();
	$manager->setOptionsString( $f, $myOptions, false );

// Now you can save the modified field definition.
	$f->save();
}

Now your process module can work with the available options. In your form process function, you will need the form ($f) and page ($p):

private function processMyOptionsForm( $f, $p ) {
	$input = wire('input');
	if ( !$input->post->submit ) { // Is form submitted
		return false;
	}
	$f->processInput( $input->post );
	if ( $f->getErrors() ) { // Does form have errors
		$this->wire()->error( $f->getErrors() );
		return false;
	}

	// turn off page formatting before changing values.
	$p->of(false);

	// remove any checkbox options previously selected. 
	$p->myOptionsField->removeAll();

	// loop through the page fields
	foreach( $p->fields as $fld ) {
		if( $fld->name == 'title' ) continue; // We don't want to change the page name
		
		// Is this our options field?
		if( $fld->name == 'myOptionsField' ) {
// Here the array of user selected values is assigned to the selectable options of our field.
			$p->myOptionsField = $input->post('myOptionsField');
		} else {
			// Set page fields to form field values
			$p->set( $fld->name, $input->post($fld->name) );
		}
	}
	// Save the page with our new values
	$p->save();
}

I hope this saves someone from a headache or three.

 

  • Like 2
Link to comment
Share on other sites

When deleting a select options field (checkboxes multiple values) from the admin interface, the selectable option entries are not deleted from the fieldtype_options table.

Is this a bug?

PW 3.0.62

 

Note: I haven't marked this topic closed or solved because I want to keep all the questions and answers together.

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...