6

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?

  • 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 Jul 24 '14 at 22:15
  • 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 Jul 25 '14 at 1:51
  • 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 Jul 25 '14 at 4:49
3
+50

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:

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

<!-- Required Fields -->
<label>Field 1
    <input type="text" name="fieldName1" />
</label>
<label>Last Name
    <input type="text" name="fieldName2" />
</label>
<!-- End Required Fields -->

<!-- My Custom Fields -->
<textarea name="fields[body]" rows="8" cols="40">OMG IT WORKED</textarea>

<button type="submit">Submit Now</button>
</form>

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.