1

After following the "How to build a plugin" from the official CraftCMS docs, I've started extending the Control Panel Template from 1 to multiple fields, all are of the same type string and all "fields" use a simple Textfield

Here the Model:

class Settings extends Model {
    public $pluginName = 'AgeGate';
    public $isAgeGateEnabled = true;
    public $minimumAgeAllowed = 10;
    public $agegateTitle = 'Age Verification';
    public $agegateContent = 'Verify your age';
    public $dayPlaceHolder = 'DD';
    public $monthPlaceHolder = 'MM';
    public $yearPlaceHolder = 'YYYY';

    public $agreeButtonLabel = 'Enter';
    public $declineButtonLabel = 'Leave';
    public $failureMessage = 'You are not old enough to enter this site';


    public function defineRules(): array {
        return [
        ];
    }
}

and here the settings template

{% import '_includes/forms.twig' as forms %}

{{ forms.textField({
    first: true,
    label: 'Minimum Age Allowed',
    name: 'minimumAgeAllowed',
    value: settings.minimumAgeAllowed
}) }}

{{ forms.textField({
    label: 'Title of Age Gate',
    name: 'agegateTitle',
    value: settings.agegateTitle
}) }}

{{ forms.textField({
    label: 'Content of agegate',
    name: 'agegateContent',
    value: settings.agegateContent
}) }}

{{ forms.textField({
    label: 'Agree Button',
    name: 'agegateAgreeButton',
    value: settings.agreeButtonLabel
}) }}

{{ forms.textField({
    label: 'Decline Button',
    name: 'agegateDeclineButton',
    value: settings.declineButtonLabel
}) }}

I don't have any problems saving the first three fields, but when changing the content of the last 2 fields my changes don't get updated.

I just can't see the reason why certain fields get updated and others not. Did anyone had this issue before?

Note: defineRules is empty on purpose for testing as currently i don't care about validating input

6
  • 3
    It looks like your input name attributes don't quite match your Settings model properties. I see agegateDeclineButton in the template, but agreeButtonLabel in the model; it does seem like the correct properties are being set back on the input for their value attributes, though! (i.e. settings.agreeButtonLabel) Commented Feb 23, 2023 at 19:31
  • I see I see.. so the name and value need to be the same.
    – funkysoul
    Commented Feb 23, 2023 at 19:50
  • @funkysoul if that ends up fixing it for you, let us know so August can add it as an official answer!
    – Brad Bell
    Commented Mar 2, 2023 at 22:06
  • Hi Brad, yes definitely, Augusts comment was the right answer.
    – funkysoul
    Commented Mar 3, 2023 at 13:24
  • @AugustMiller you're up!
    – Brad Bell
    Commented Mar 3, 2023 at 16:20

1 Answer 1

3

Craft automatically sets values from the incoming POST data on your Settings model, which means the input name attributes must match exactly (or that the Settings model must expose an appropriate “setter” method).

It appears that your class property is agreeButtonLabel (not agegateAgreeButton), which means that its field’s macro call should look like this:

{{ forms.textField({
    label: 'Agree Button',
    name: 'agreeButtonLabel',
    value: settings.agreeButtonLabel,
}) }}

I suspect this was ultimately just a typo, because you already had the correct property name for the value part of the macro!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.