Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I am finishing up the last few details on my custom element type. I have custom statuses and public submissions working. The last detail is providing validation on custom fields set through the field layout outside of the control panel.

Here is a brief overview of the plugin:

  1. PluginNameElementType - that allows custom fields via the field layout.
  2. PluginNameController - which has an action 'actionSubmit' which is allowed public access to submit entries outside of the control panel.

The code for public submission is on this post:

Allow anonymous submission on a custom element type?

I have seen some Yii examples that include CActiveForm::validate() and pass a model to validate the input, but I don't see that anywhere in Craft.

The way I see it, there are two parts to validation on custom element types from outside of the control panel.

The first part being providing authentication for the attributes defined as 'required' in the element types model.

For example, if you have the following code:

protected function defineAttributes()
{
    return array_merge(
        parent::defineAttributes(), array(
            'formId' => array(
                AttributeType::Number,
                'required' => true
            ),
            'fieldName1' => array(
                AttributeType::String,
                'required' => true
            ),
            'fieldName2' => array(
                AttributeType::String,
                'required' => true
            ),
        )
    );
}

How would I access and return this in the controller action method that allows public submission? Does Craft provide an additional layer on top of Yii to assist with validation?

The second part of the question is similar, but unique all the same. The other part I need to provide validation for is the custom fields set in the field layout of the element type. If a user sets the fields to required in the same method they would with a regular entry field. How would I access validation on the custom fields level?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Custom field validation happens automatically for you when you pass your element into craft()->elements->saveElement(). That function will return true or false, largely depending on whether custom field validation passes.

See EntriesService::saveEntry() for an example.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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