2

I have a plugin that is listening for a form submit. Within that form are two fields firstName and agreeToTerms.

When the form is submitted, I am updating an existing entry with the values that were posted.

I can get all fields to update except my checkbox. I'm not sure what I need to send to craft if the user has checked the checkbox.

Here is what I have so far:

MyController

$postDateTime = ...

...
$entry->setContentFromPost(array(
    'agreeToTerms' => craft()->request->getPost('fields.agreeToTerms'),
    'firstName' => craft()->request->getPost('fields.electronicSignature'),
    'signatureDate' => $postDateTime
));
...

This returns "1" which is the value of the checkbox.

craft()->request->getPost('fields.agreeToTerms') // returns 1

But that isn't what I need to send to Craft. If I were using jquery, I am looking for something like

$.prop('checked', true);

EDIT

To help clarify in case someone else comes across this thread, as @carlcs stated I was in fact talking about a checkbox field.

The plugin I am using allows users to create entries in a section. When an entry is created, one of the fields is a checkbox. I was attempting to set the checkbox field as checked if the user had checked the corresponding checkbox when the entry was created.

0

If I understand correctly you're talking about a Checkboxes field type. Because you can have multiple checkboxes within such a field, Craft stores an array to the database.

Try if this works:

$entry->setContentFromPost(array(
    'agreeToTerms' => array(
        craft()->request->getPost('fields.agreeToTerms'),
    )
));
| improve this answer | |
  • Thank you both! I knew I needed to pass an array I think I tried every version of agreeToTerms[] I could think of...except this one. – Damon Jan 22 '16 at 1:35

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.