<p>Ok, so I finally figured it out. The trick was using the following two methods:</p>

<ul>
<li><code>assembleLayoutFromPost()</code></li>
<li><code>setContentFromPost('fields')</code></li>
</ul>

<p>The controller now looks like this:</p>

<pre class="lang-php prettyprint-override"><code>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');
}

}
</code></pre>

<p>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:</p>

<pre><code>&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;
</code></pre>

CC BY-SA 3.0
2014-07-25T09:08:57.877Z
2014-07-25T09:15:36.113Z
2014-07-25T09:15:36.113Z
Jason McCallister
<p>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.</p>

<p>I have the controller setup like so: </p>

<pre class="lang-php prettyprint-override"><code>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');
        }
    }
</code></pre>

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

<p>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 <code>formId</code>. So when using <code>getPost</code> 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.. </p>

<p>Any thoughts? </p>

CC BY-SA 3.0
2014-07-24T22:15:16.003Z
Just to clarify... Is the question about **how to pass data** to a controller? The title implies that you're having trouble submitting anonymously, but it looks like you're doing that part correctly. Where in the script is it failing?
Lindsey D
CC BY-SA 3.0
2014-07-25T01:51:56.423Z
I need to clean up the question a little more.. Will do that now to make more sense of exactly what I am asking.
Jason McCallister
CC BY-SA 3.0
2014-07-25T04:49:16.400Z
I edited my original question a little bit. The easy part is grabbing the attributes for my element type, but since the et is using a field layout... I'm looking at using `craft()->fields->assembleLayoutFromPost();` and seeing how far that gets me.
Jason McCallister
<plugin-development><controller><elementtype><services><models>
Allow anonymous submission on a custom element type?
# Allow anonymous submission on a custom element type?

(Asked by: username_0 on 2014-07-18)

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->requirePostRequest();

        $entry = craft()->request->getPost();

        // set the form id from the post request
        $formId = craft()->request->getPost('formId');
        $entry = craft()->request->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()->pluginName->publicSubmission($formId, $entry))
        {
            $this->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?

**Tags:** plugin-development, controller, elementtype, services, models
> **Comments:**
> **username_1**: Just to clarify... Is the question about **how to pass data** to a controller? The title implies that you're having trouble submitting anonymously, but it looks like you're doing that part correctly. Where in the script is it failing?  
> **username_0**: I need to clean up the question a little more.. Will do that now to make more sense of exactly what I am asking.  
> **username_0**: I edited my original question a little bit. The easy part is grabbing the attributes for my element type, but since the et is using a field layout... I'm looking at using `craft()->fields->assembleLayoutFromPost();` and seeing how far that gets me.  

---

## 1 answers

### Answer by username_0 on 2014-07-25. Score: 3 | Accepted answer

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->requirePostRequest();

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

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

// if form id does NOT exist
if (!$elementtype->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()->elementtype_forms->getFormById($elementtype->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->id = $elementtype->formId;

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

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

// Save it
if (craft()->elementtype->save($elementtype))
{
    $this->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:

    
    
    


Field 1
    

Last Name
    




OMG IT WORKED

Submit Now


---

