Answers.Body: <p>I believe you need to set the <code>on</code> value of your lightswitch field.</p>
  
  <pre><code>on: (settings.mailerShowInfo is defined) ? settings.mailerShowInfo : ""
  </code></pre>
  
  <p>So, the full settings of your twig code would be:</p>
  
  <pre><code>{{  forms.lightswitchField({
          label: "Show info in sidebar?"|t,
          id: 'mailerShowInfo',
          name: 'mailerShowInfo',
          value: settings.mailerShowInfo,
          required: true
          on: (settings.mailerShowInfo is defined) ? settings.mailerShowInfo : ""
      })
  }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-15T15:48:35.873Z
Answers.LastActivityDate: 2014-06-15T15:48:35.873Z
Answers.UserDisplayName: Ben Parizek
Body: <p>I'm trying to use a lightswitch-field in a plugin-config. But for some reason it won't get saved. The lightswitch-field is always off.</p>
  
  <pre><code>protected function defineSettings()
  {
      return array(
          ...
          'mailerShowInfo' =&gt; array(AttributeType::Bool, 'default' =&gt; false),
          ...
      );
  }
  </code></pre>
  
  <p>_settings.html:</p>
  
  <pre><code>{{  forms.lightswitchField({
          label: "Show info in sidebar?"|t,
          id: 'mailerShowInfo',
          name: 'mailerShowInfo',
          value: settings.mailerShowInfo,
          required: true
      })
  }}
  </code></pre>
  
  <p>I tried to use AttributeType::Number instead, and even used <a href="http://php.net/manual/en/function.settype.php">settype()</a> in <a href="http://buildwithcraft.com/docs/plugins/plugin-settings">prepSettings()</a> to force a boolean conversion, but still nothing.</p>
  
  <p>Any idea what I'm missing?</p>
  
Tags: <field><plugin-development><lightswitch>
Title: How do I maintain the on value with a lightswitch-field as a plugin setting?
ThreadText: # How do I maintain the on value with a lightswitch-field as a plugin setting?
  
  (Asked by: username_0 on 2014-06-15)
  
  I'm trying to use a lightswitch-field in a plugin-config. But for some reason it won't get saved. The lightswitch-field is always off.
  
  ```
  protected function defineSettings()
  {
      return array(
          ...
          'mailerShowInfo' => array(AttributeType::Bool, 'default' => false),
          ...
      );
  }
  
  ```
  
  \_settings.html:
  
  ```
  {{  forms.lightswitchField({
          label: "Show info in sidebar?"|t,
          id: 'mailerShowInfo',
          name: 'mailerShowInfo',
          value: settings.mailerShowInfo,
          required: true
      })
  }}
  
  ```
  
  I tried to use AttributeType::Number instead, and even used [settype()](http://php.net/manual/en/function.settype.php) in [prepSettings()](http://buildwithcraft.com/docs/plugins/plugin-settings) to force a boolean conversion, but still nothing.
  
  Any idea what I'm missing?
  
  **Tags:** field, plugin-development, lightswitch
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-06-15. Score: 13 | Accepted answer
  
  I believe you need to set the `on` value of your lightswitch field.
  
  ```
  on: (settings.mailerShowInfo is defined) ? settings.mailerShowInfo : ""
  
  ```
  
  So, the full settings of your twig code would be:
  
  ```
  {{  forms.lightswitchField({
          label: "Show info in sidebar?"|t,
          id: 'mailerShowInfo',
          name: 'mailerShowInfo',
          value: settings.mailerShowInfo,
          required: true
          on: (settings.mailerShowInfo is defined) ? settings.mailerShowInfo : ""
      })
  }}
  
  ```
  
  
  ---
  
  