Answers.Body: <p>If it's only one single field and you want to bypass all Craft validations for required fields you can change the value directly in your database using <a href="https://www.yiiframework.com/doc/guide/1.1/en/database.query-builder#building-data-manipulation-queries" rel="nofollow noreferrer">Yii commands</a> and the <code>update</code> function</p>
  
  <pre class="lang-PHP prettyprint-override"><code>function update($table, $columns, $conditions='', $params=array())
  </code></pre>
  
  <pre class="lang-PHP prettyprint-override"><code>$command = craft()-&gt;db-&gt;createCommand();
  $command-&gt;update('craft_content', 
      array(
          'field_yourLightSwitchField' =&gt; 1,
      ), 
      'elementId=:id', array(':id'=&gt;1)
  );
  </code></pre>
  
  <p>If you have a multilocale setup you need to include the condition for the correct locale as well</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-28T11:48:34.543Z
Answers.Text: ii think we dont need to add `craft_` as YII did this for us.
Answers.UserDisplayName: inrsaurabh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-28T11:22:50.893Z
Answers.LastActivityDate: 2019-02-28T11:22:50.893Z
Answers.UserDisplayName: Robin Schambach
Body: <p>How can I edit a lightswitch field in an entries field layout via PHP? This is my current approach</p>
  
  <pre class="lang-PHP prettyprint-override"><code>public function actionSponsoredEntriesPriority() {
      $this-&gt;requireAdmin();
      $this-&gt;requireAjaxRequest();
      $success = false;
      $message = Craft::t('Any message from controller');
  
      $newPriority = craft()-&gt;request-&gt;getPost('newPriority');
      $entryId = craft()-&gt;request-&gt;getPost('entryId');
  
      $entries = craft()-&gt;entries;
  
      $entry = $entries-&gt;getEntryById($entryId);
  
      if ($entry != null) {
          $entry-&gt;getContent()-&gt;setAttributes(array(
              'lightswitchFieldHandle' =&gt; 1
          ));
  
          $success = $entries-&gt;saveEntry($entry);
          $success == true;
      } else {
          //return null;
          $success = false;
      }
  }
  </code></pre>
  
  <p>But since there are other required fields that are currently empty for this entry I receive the following response</p>
  
  <blockquote>
    <p>message: {featureImage: ["Feature Image cannot be blank."], hero: ["Header cannot be blank."]}</p>
  </blockquote>
  
  <p>I tried to resave the required fields still stuck at matrix field.
  Here is code <a href="https://gist.github.com/maddyBoy/feed5b2c2daa97398d5ac93d63418fc5" rel="nofollow noreferrer">Reset required field</a></p>
  
  <blockquote>
    <p>Error : Correct the errors listed above.</p>
  </blockquote>
  
  <p>Do i need to update the required field as well or there is a way to bypass while updating a field ?</p>
  
  <blockquote>
    <p>Update : Solution as suggested in answer [Update field directly ]</p>
  </blockquote>
  
  <pre><code>public function actionSponsoredEntriesPriority() {
          // $this-&gt;requireAdmin();
          // $this-&gt;requireAjaxRequest();
          $newPriority = craft()-&gt;request-&gt;getPost('newPriority');
          $entryId = craft()-&gt;request-&gt;getPost('entryId');
          $success = false;
          $message = Craft::t('Any message from controller');
  
          // entries object
          $entries = craft()-&gt;entries;
          // entries  by id ( $entryId passed by js )
          $entry = $entries-&gt;getEntryById($entryId);
  
          if ($entry != null) {
                  $command = craft()-&gt;db-&gt;createCommand();
                  $success = $command-&gt;update(
                      'content', // table name
                      array(
                      'field_lightswitchFieldHandle' =&gt; 1,
                      'field_sponsoredEntriesPriority' =&gt; $newPriority
                      ), // fields
                      'elementId=:id',
                      array(':id'=&gt; $entryId)
                      );
                  $message = Craft::t('Priority saved');
                  $success == true;
              } else {
              //return null;
              $success = false;
          }
          // send response true/false with message
         if($success == true){
             $this-&gt;returnJson(
              array(
                  'success' =&gt; $success,
                  'message' =&gt; $message
              ));
         }else{
             $this-&gt;returnJson(
              array(
                  'success' =&gt; $success,
                  'message' =&gt; $entry-&gt;getErrors()
              ));
         }
      }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-02-28T10:08:34.957Z
Comments.Text: As soon as an Element has content and is enabled it will be validated, in Craft 2 there is no "easy and fast" way to prevent it. You could either recreate the `EntriesService` and include the parameter `$success = craft()->elements->saveElement($entry, false);` or if you only want to edit the single field create a query and set it directly in your content column
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-02-28T10:20:27.730Z
Comments.Text: if i use direct query is there any dependency on any another table while updating one field value.
Comments.UserDisplayName: inrsaurabh
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-02-28T10:24:06.640Z
Comments.Text: No, the `dateUpdated` value in your entry won't be updated but if that's no problem there are no other dependencies. Well and certain events won't be fired such as `afterSave` but if you don't need them there are no disadvantages
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-02-28T10:42:08.480Z
Comments.Text: Ok thank you, will try to update directly, In craft 3 is this solved ?
Comments.UserDisplayName: inrsaurabh
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-02-28T10:43:46.853Z
Comments.Text: Yes Craft 3 handles everything much better. If you can I would suggest you to update for countless reasons. The main one is the switch from Yii to Yii2. Yii is really old
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-02-28T10:52:56.283Z
Comments.Text: Actually we are doing modification in an existing project, but alongside we are working on version 3 as well.
Comments.UserDisplayName: inrsaurabh
Tags: <controller><craft2><saveentry>
Title: Update a lightswitch field in Craft CMS 2
ThreadText: # Update a lightswitch field in Craft CMS 2
  
  (Asked by: username_0 on 2019-02-28)
  
  How can I edit a lightswitch field in an entries field layout via PHP? This is my current approach
  
  ```PHP
  public function actionSponsoredEntriesPriority() {
      $this->requireAdmin();
      $this->requireAjaxRequest();
      $success = false;
      $message = Craft::t('Any message from controller');
  
      $newPriority = craft()->request->getPost('newPriority');
      $entryId = craft()->request->getPost('entryId');
  
      $entries = craft()->entries;
  
      $entry = $entries->getEntryById($entryId);
  
      if ($entry != null) {
          $entry->getContent()->setAttributes(array(
              'lightswitchFieldHandle' => 1
          ));
  
          $success = $entries->saveEntry($entry);
          $success == true;
      } else {
          //return null;
          $success = false;
      }
  }
  
  ```
  
  But since there are other required fields that are currently empty for this entry I receive the following response
  
  > message: {featureImage: ["Feature Image cannot be blank."], hero: ["Header cannot be blank."]}
  
  I tried to resave the required fields still stuck at matrix field.
  Here is code [Reset required field](https://gist.github.com/maddyBoy/feed5b2c2daa97398d5ac93d63418fc5)
  
  > Error : Correct the errors listed above.
  
  Do i need to update the required field as well or there is a way to bypass while updating a field ?
  
  > Update : Solution as suggested in answer [Update field directly ]
  
  ```
  public function actionSponsoredEntriesPriority() {
          // $this->requireAdmin();
          // $this->requireAjaxRequest();
          $newPriority = craft()->request->getPost('newPriority');
          $entryId = craft()->request->getPost('entryId');
          $success = false;
          $message = Craft::t('Any message from controller');
  
          // entries object
          $entries = craft()->entries;
          // entries  by id ( $entryId passed by js )
          $entry = $entries->getEntryById($entryId);
  
          if ($entry != null) {
                  $command = craft()->db->createCommand();
                  $success = $command->update(
                      'content', // table name
                      array(
                      'field_lightswitchFieldHandle' => 1,
                      'field_sponsoredEntriesPriority' => $newPriority
                      ), // fields
                      'elementId=:id',
                      array(':id'=> $entryId)
                      );
                  $message = Craft::t('Priority saved');
                  $success == true;
              } else {
              //return null;
              $success = false;
          }
          // send response true/false with message
         if($success == true){
             $this->returnJson(
              array(
                  'success' => $success,
                  'message' => $message
              ));
         }else{
             $this->returnJson(
              array(
                  'success' => $success,
                  'message' => $entry->getErrors()
              ));
         }
      }
  
  ```
  
  **Tags:** controller, craft2, saveentry
  
  > **Comments:**
  > **username_1**: As soon as an Element has content and is enabled it will be validated, in Craft 2 there is no "easy and fast" way to prevent it. You could either recreate the `EntriesService` and include the parameter `$success = craft()->elements->saveElement($entry, false);` or if you only want to edit the single field create a query and set it directly in your content column  
  > **username_0**: if i use direct query is there any dependency on any another table while updating one field value.  
  > **username_1**: No, the `dateUpdated` value in your entry won't be updated but if that's no problem there are no other dependencies. Well and certain events won't be fired such as `afterSave` but if you don't need them there are no disadvantages  
  > **username_0**: Ok thank you, will try to update directly, In craft 3 is this solved ?  
  > **username_1**: Yes Craft 3 handles everything much better. If you can I would suggest you to update for countless reasons. The main one is the switch from Yii to Yii2. Yii is really old  
  > **username_0**: Actually we are doing modification in an existing project, but alongside we are working on version 3 as well.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-02-28. Score: 2 | Accepted answer
  
  If it's only one single field and you want to bypass all Craft validations for required fields you can change the value directly in your database using [Yii commands](https://www.yiiframework.com/doc/guide/1.1/en/database.query-builder#building-data-manipulation-queries) and the `update` function
  
  ```PHP
  function update($table, $columns, $conditions='', $params=array())
  
  ```
  
  ```PHP
  $command = craft()->db->createCommand();
  $command->update('craft_content', 
      array(
          'field_yourLightSwitchField' => 1,
      ), 
      'elementId=:id', array(':id'=>1)
  );
  
  ```
  
  If you have a multilocale setup you need to include the condition for the correct locale as well
  
  > **Comments:**
  > **username_0**: ii think we dont need to add `craft_` as YII did this for us.  
  
  ---
  
  