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


I can see how this is unexpected behavior. The code in BaseElementModel::setContentFromPost() isn’t considering that the 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()). 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() has had a chance to do its thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.