Answers.Body: <p>I can see how this is unexpected behavior. The code in <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#setContentFromPost-detail" rel="nofollow">BaseElementModel::setContentFromPost()</a> isn’t considering that the <a href="http://buildwithcraft.com/classreference/models/ContentModel" rel="nofollow">ContentModel</a> it’s setting data to is going to be doing its own manipulation of the data before the field type has had a chance to tweak it (thanks to <a href="http://buildwithcraft.com/classreference/models/BaseModel#setAttribute-detail" rel="nofollow">setAttribute()</a>). And in AttributeType::DateTime’s case, that means trying to convert the value to a DateTime object, else completely discarding the value.</p>
  
  <p>Anyway, I’ll tweak this for the next release. Shouldn’t have any negative side effects if we just hold off on assigning the value to the ContentModel until <em>after</em> the field type’s <a href="http://buildwithcraft.com/classreference/fieldtypes/BaseFieldType#prepValueFromPost-detail" rel="nofollow">prepValueFromPost()</a> has had a chance to do its thing.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-03T14:05:39.887Z
Answers.LastActivityDate: 2014-09-03T14:05:39.887Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I'm trying to create a FieldType that takes a POSTed day/month/year array and saves it into a datetime column, but I think because of the column type it's tripping up before it ever gets to <code>prepValueFromPost()</code>.</p>
  
  <p>So far I'm extending <code>Craft\DateFieldType</code>, which defines the content attribute as <code>AttributeType::DateTime</code> for me, and I've got my <code>prepValueFromPost()</code> function:</p>
  
  <pre><code>public function prepValueFromPost($value)
  {
      if ($value)
      {
          $value = join('-', array($value['year'], $value['month'], $value['day']));
  
          return parent::prepValueFromPost($value);
      }
  }
  </code></pre>
  
  <p>To work it into a string that <code>DateFieldType::prepValueFromPost()</code> can use (probably). The issue is, during <code>$entry-&gt;setContentFromPost($values)</code>, it first sets the raw post content (an array) to the attribute:</p>
  
  <pre><code>/vagrant/craft/app/models/BaseElementModel.php(888): Craft\BaseModel-&gt;__set("userDateOfBirth", array("day" =&gt; "01", "month" =&gt; "1", "year" =&gt; "1986"))
  </code></pre>
  
  <p>Which then looks at the column type for that field, sees its a DateTime and tries to shoehorn my array into <code>createFromString</code>:</p>
  
  <pre><code>/vagrant/craft/app/models/BaseModel.php(349): Craft\DateTime::createFromString(array("day" =&gt; "01", "month" =&gt; "1", "year" =&gt; "1986"))
  </code></pre>
  
  <p>Which is no good. And all this happens before my fieldtype's <code>prepValueFromPost()</code> ever gets called, so I'm not sure what to do :(</p>
  
Tags: <plugin-development><fieldtypes>
Title: Prepping a field's value from a fieldtype before it hits Craft\BaseModel->__set()
ThreadText: # Prepping a field's value from a fieldtype before it hits Craft\BaseModel->__set()
  
  (Asked by: username_0 on 2014-09-03)
  
  I'm trying to create a FieldType that takes a POSTed day/month/year array and saves it into a datetime column, but I think because of the column type it's tripping up before it ever gets to `prepValueFromPost()`.
  
  So far I'm extending `Craft\DateFieldType`, which defines the content attribute as `AttributeType::DateTime` for me, and I've got my `prepValueFromPost()` function:
  
  ```
  public function prepValueFromPost($value)
  {
      if ($value)
      {
          $value = join('-', array($value['year'], $value['month'], $value['day']));
  
          return parent::prepValueFromPost($value);
      }
  }
  
  ```
  
  To work it into a string that `DateFieldType::prepValueFromPost()` can use (probably). The issue is, during `$entry->setContentFromPost($values)`, it first sets the raw post content (an array) to the attribute:
  
  ```
  /vagrant/craft/app/models/BaseElementModel.php(888): Craft\BaseModel->__set("userDateOfBirth", array("day" => "01", "month" => "1", "year" => "1986"))
  
  ```
  
  Which then looks at the column type for that field, sees its a DateTime and tries to shoehorn my array into `createFromString`:
  
  ```
  /vagrant/craft/app/models/BaseModel.php(349): Craft\DateTime::createFromString(array("day" => "01", "month" => "1", "year" => "1986"))
  
  ```
  
  Which is no good. And all this happens before my fieldtype's `prepValueFromPost()` ever gets called, so I'm not sure what to do :(
  
  **Tags:** plugin-development, fieldtypes
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-09-03. Score: 2 | Accepted answer
  
  I can see how this is unexpected behavior. The code in [BaseElementModel::setContentFromPost()](http://buildwithcraft.com/classreference/models/BaseElementModel#setContentFromPost-detail) isn’t considering that the [ContentModel](http://buildwithcraft.com/classreference/models/ContentModel) it’s setting data to is going to be doing its own manipulation of the data before the field type has had a chance to tweak it (thanks to [setAttribute()](http://buildwithcraft.com/classreference/models/BaseModel#setAttribute-detail)). And in AttributeType::DateTime’s case, that means trying to convert the value to a DateTime object, else completely discarding the value.
  
  Anyway, I’ll tweak this for the next release. Shouldn’t have any negative side effects if we just hold off on assigning the value to the ContentModel until *after* the field type’s [prepValueFromPost()](http://buildwithcraft.com/classreference/fieldtypes/BaseFieldType#prepValueFromPost-detail) has had a chance to do its thing.
  
  
  ---
  
  