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

CC BY-SA 4.0
2019-07-18T16:30:46.497Z
2019-07-18T16:30:46.497Z
Lewis Jenkins
<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>

<plugin-development><craft3>
Modify a field value before the element is saved
# 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 is what I needed.
public function serializeValue($value, ElementInterface $element = null)
{   
    if ($value == 'foo') {
        $value = 'bar';
    }

    return $value;
}


---

