Allow anonymous submission on a custom element type? I have a custom element type that I want to allow anonymous submission from the front-end. I have a solid concept on how the form data is passed but I seem to be missing something.

I have the controller setup like so: 

class PluginNameController extends BaseController
{
    /**
     * @var Allows anonymous access to this controller's actions.
     * @access protected
     */
    protected $allowAnonymous = array('actionSubmit');

    /**
     * Allow public submission of the element type
     */
    public function actionSubmit()
    {
        // require post request for this action
        $this-&gt;requirePostRequest();

        $entry = craft()-&gt;request-&gt;getPost();

        // set the form id from the post request
        $formId = craft()-&gt;request-&gt;getPost('formId');
        $entry = craft()-&gt;request-&gt;getPost();

        // if form id is empty, throw exception as we can't continue
        if (!$formId) {
            throw new Exception(Craft::t('The formId is required to submit.'));
        }

        if (craft()-&gt;pluginName-&gt;publicSubmission($formId, $entry))
        {
            $this-&gt;redirectToPostedUrl();
        }
        else
        {
            die('failed submission');
        }
    }


My element type has 4 fields that are required and those submit anonymously as expected as I am grabbing them using getPost. However, my plugin also utilizes the getSources method and each formId has a field layout assigned to the custom element type, lets say 'entry'. 

Whats causing the trip up is the fact that since this is an element type that utilizes a field layout, I will need to account for the fields based on the formId. So when using getPost I can namespace the attributes for the element type, but I am trying to find the best way to capture the fields specified in the field layout and save those to the appropriate table.. 

Any thoughts?
Ok, so I finally figured it out. The trick was using the following two methods:


assembleLayoutFromPost()
setContentFromPost('fields')


The controller now looks like this:

public function actionSubmit()
{
// require POST
$this-&gt;requirePostRequest();

// assume new element type
$elementtype = new PluginName_ElementTypeModel();

// assign the attributes specific to the element type
$elementtype-&gt;formId    = craft()-&gt;request-&gt;getPost('formId');
$elementtype-&gt;fieldName1 = craft()-&gt;request-&gt;getPost('fieldName1');
$elementtype-&gt;fieldName2  = craft()-&gt;request-&gt;getPost('fieldName2');

// if form id does NOT exist
if (!$elementtype-&gt;formId)
{
    // @TODO figure out how to return errors/validation
    dd('need a form id');
}
// if form id exists but we can't find it in the system
elseif (craft()-&gt;elementtype_forms-&gt;getFormById($elementtype-&gt;formId) == null)
{
    // @TODO figure out how to return errors/validation
    dd('form does not exist');
}

// setup a new form model
$form = new PluginName_FormModel();

// shared attributes
$form-&gt;id = $elementtype-&gt;formId;

// set the field layout
$fieldLayout = craft()-&gt;fields-&gt;assembleLayoutFromPost();
$fieldLayout-&gt;type = ElementType::Asset;
$form-&gt;setFieldLayout($fieldLayout);

// set content from post with the fields namespace
$elementtype-&gt;setContentFromPost('fields');

// Save it
if (craft()-&gt;elementtype-&gt;save($elementtype))
{
    $this-&gt;redirectToPostedUrl($elementtype);
}
else
{
    dd('not saved');
}

}


The main difference was on the front-end where I namespaced the fields that were assigned via the field layout, so the template to submit looked like this:

&lt;form id="plugin-form"guinname method="post" action="" accept-charset="UTF-8"&gt;
    &lt;input type="hidden" name="action" value="pluginname/submit"&gt;
    &lt;input type="hidden" name="redirect" value="pluginname?submit=success"&gt;
    &lt;input type="hidden" name="formId" value="1"&gt;

&lt;!-- Required Fields --&gt;
&lt;label&gt;Field 1
    &lt;input type="text" name="fieldName1" /&gt;
&lt;/label&gt;
&lt;label&gt;Last Name
    &lt;input type="text" name="fieldName2" /&gt;
&lt;/label&gt;
&lt;!-- End Required Fields --&gt;

&lt;!-- My Custom Fields --&gt;
&lt;textarea name="fields[body]" rows="8" cols="40"&gt;OMG IT WORKED&lt;/textarea&gt;

&lt;button type="submit"&gt;Submit Now&lt;/button&gt;
&lt;/form&gt;