1

I need to get two text fields under the same custom form and have it return two different values to be stored in the database. Right now with what I have, it's storing the same variable for both, which I get why it's happening, but I can't come up with a viable solution to return two values.

setting the field

{% import "_includes/forms" as forms %}

{{ forms.textField({
    label: 'Value 1',
    instructions: 'test',
    name: name,
    style: 'width: 50%',
    value: value})
}}
{{ forms.textField({
    label: 'Value 2',
    instructions: 'test',
    name: name,
    style: 'width: 50%',
    value: value})
}}

Field function:

public function getInputHtml($value, ElementInterface $element = null): string
{
    return Craft::$app->getView()->renderTemplate('custom-plugin/cloudinary_field',
        [
                'name' => $this->handle,
                'value' => $value,
                'field' => $this,
        ]
    );
}

How can I make these two text fields each return a value to be stored in the database?

3
  • You can store an array in your DB as json. Would that be enough or do you need a custom table in order to enable advanced searching/filtering? Aug 31 '18 at 21:18
  • I believe that would work, but I'm hitting a wall trying to figure out how to use it as an array.
    – Mapbry
    Aug 31 '18 at 21:21
  • I've got the json arrays working, however I can't get the two fields to store the data in craft when we hit save. I believe it's because the input fields both have the same name. I'd assume these field names need to be unique? I found craftcms.stackexchange.com/questions/17808/… which mentions the defineContentAttribute function, but it does not appear to be available in craft 3.
    – Mapbry
    Sep 6 '18 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.