Answers.Body: <p>It turns out that <a href="https://docs.craftcms.com/api/v3/craft-base-field.html#method-serializevalue" rel="nofollow noreferrer">serializeValue</a> is what I needed.</p>
  
  <pre><code>public function serializeValue($value, ElementInterface $element = null)
  {   
      if ($value == 'foo') {
          $value = 'bar';
      }
  
      return $value;
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-18T16:30:46.497Z
Answers.LastActivityDate: 2019-07-18T16:30:46.497Z
Answers.UserDisplayName: Lewis Jenkins
Body: <p>I have a simple plugin with a field that extends the Craft PlainText field.</p>
  
  <p>Is there some kind of function I can use to modify the field's submitted value before the element is saved?</p>
  
  <pre><code>class myField extends craft\fields\PlainText
  {
      private function modifyPostedValue()
      {
          if ($PostedValue == “foo”)  {
              $PostedValue = “bar”;
              // save “bar” to database
          } else {
              // save “foo” to database
          };
      }
  }
  </code></pre>
  
Tags: <plugin-development><craft3>
Title: Modify a field value before the element is saved
ThreadText: # Modify a field value before the element is saved
  
  (Asked by: username_0 on 2019-07-18)
  
  I have a simple plugin with a field that extends the Craft PlainText field.
  
  Is there some kind of function I can use to modify the field's submitted value before the element is saved?
  
  ```
  class myField extends craft\fields\PlainText
  {
      private function modifyPostedValue()
      {
          if ($PostedValue == “foo”)  {
              $PostedValue = “bar”;
              // save “bar” to database
          } else {
              // save “foo” to database
          };
      }
  }
  
  ```
  
  **Tags:** plugin-development, craft3
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-07-18. Score: 1
  
  It turns out that [serializeValue](https://docs.craftcms.com/api/v3/craft-base-field.html#method-serializevalue) is what I needed.
  
  ```
  public function serializeValue($value, ElementInterface $element = null)
  {   
      if ($value == 'foo') {
          $value = 'bar';
      }
  
      return $value;
  }
  
  ```
  
  
  ---
  
  