Jump to content

zeroVal usage in node-setting. Problems with checkbox


TSP

Recommended Posts

Hello, 

I seem to be unable to figure out what I'm doing wrong. 

This is what I had originally: 

if( \IPS\Settings::i()->anon_inforums === '0' )
{
	\IPS\Settings::i()->anon_inforums = 0;
}

# Forums where the selected members can post anonymously
$form->add( new \IPS\Helpers\Form\Node( 'anon_inforums', isset( \IPS\Settings::i()->anon_inforums ) ? \IPS\Settings::i()->anon_inforums : array(), NULL, array( 
	'class' => '\IPS\forums\Forum', 
	'multiple' => true,
	'zeroVal' => 'all'
) ) );

This worked for selecting multiple forums in the node selector. After saving it would still show the selected forums for the setting. (So far so good)

Then I try to deselect the selected forums. After saving it shows no selected forums for the setting. The "All"-checkbox is NOT checked. (So far so good)

Then I try check the All-checkbox. After saving, it still shows the checkbox as checked. (So far so good)

Then I try to uncheck the All-checkbox. After saving, it still shows the checkbox as checked (Not good)

In order to get no forums selected again, and also not have the "All"-checkbox selected, I have to uncheck "All" and select some random forums in the node selector. Then save. After saving I then have to edit the settings again, deselect the forums, then save. And it's working again. 

I also tried some other variants. Including this one: 

$form->add( new \IPS\Helpers\Form\Node( 'anon_inforums', \IPS\Settings::i()->anon_inforums ? explode( ',', \IPS\Settings::i()->anon_inforums) : 0, NULL, array( 
	'class' => '\IPS\forums\Forum', 
	'multiple' => true,
	'zeroVal' => 'all'
) ) );

In this case, if I don't have any forums selected, the "All"-checkbox will stay checked. I can't uncheck it at all. It will only stay unchecked if I also selected forums. 

I don't do anything special for saving the settings: 

if ( $values = $form->values() )
{
	$form->saveAsSettings();
	return TRUE;
}


1) What am I doing wrong? How should this be done?

2) I have also observed that the string 0 and integer 0 is treated differently. The integer 0 will check the checkbox, while the string 0 will leave the checkbox unchecked. 

Is this intended?

3) What value should I expect when the "All"-checkbox is checked? Because it seems to be returned by \IPS\Setting as a string, but it's only the integer 0 that checks the checkbox, which is why I did that workaround in the first version of the code. Should I take into account I could both receive 0 as a string and integer in my other code? Or should only one of them be necessary?

Link to comment
Share on other sites

Ended up doing this: 

$setting = \IPS\Settings::i()->anon_inforums;
$value = isset( $setting ) ? (is_numeric( $setting ) ? intval( $setting ) : $setting) : '';
$form->add( new \IPS\Helpers\Form\Node( 'anon_inforums', $value, NULL, array( 
	'class' => '\IPS\forums\Forum', 
	'multiple' => true,
	'zeroVal' => 'all'
) ) );

 

Link to comment
Share on other sites

Hello,

23 hours ago, TSP said:

The integer 0 will check the checkbox, while the string 0 will leave the checkbox unchecked. 

Is this intended?

{{if $value === 0}}checked{{endif}}

So your value should be 0 (integer)

23 hours ago, TSP said:

What value should I expect when the "All"-checkbox is checked?

0 (integer)

23 hours ago, TSP said:

Because it seems to be returned by \IPS\Setting as a string

Yes

23 hours ago, TSP said:

Should I take into account I could both receive 0 as a string and integer in my other code? Or should only one of them be necessary?

You can have 3 values

0 (string) - the checkbox checked

empty string - No selected forums and the checkbox unchecked

not empty string (ex. 1,2,3,4,5) - selected forums with IDs 1,2,3,4,5

23 hours ago, TSP said:

1) What am I doing wrong?

this

23 hours ago, TSP said:

if( \IPS\Settings::i()->anon_inforums === '0' )
{
	\IPS\Settings::i()->anon_inforums = 0;
}

 

You have checked checkbox, then uncheck it and save the form the code runs again so you have

1. you have checked checkbox

2. checks next code

if( \IPS\Settings::i()->anon_inforums === '0' )
{
    \IPS\Settings::i()->anon_inforums = 0;
}

3. you have overridden the setting value

4. save the form

\system\Settings\Settings.php

			if ( $this->$k != $valueToCache )
			{
				$this->$k = $valueToCache;
				\IPS\Db::i()->update( 'core_sys_conf_settings', array( 'conf_value' => $v ), array( 'conf_key=?', $k ) );

0 != '' return false. In your case

'0' != '' return true


I use

$form->add( new \IPS\Helpers\Form\Node(
	'setting_key',
	\IPS\Settings::i()->setting_key === '0' ? 0 : \IPS\Settings::i()->setting_key,

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

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