luyadev/luya-module-forms

LUYA CMS 的表单构建模块。

1.8.1 2023-10-31 07:11 UTC

This package is auto-updated.

Last update: 2024-09-14 19:25:56 UTC


README

LUYA Logo

LUYA CMS 表单构建器

LUYA Tests Maintainability Test Coverage

基于 LUYA CMS 块的拖放表单构建器。

可用的表单模块块扩展

安装

通过 composer 安装扩展

composer require luyadev/luya-module-forms

将模块添加到配置中

'modules' => [
    //...
    'forms' => [
        'class' => 'luya\forms\Module',
    ]
]

运行迁移命令,该命令将设置数据库表

./luya migrate

运行导入命令以设置所有必要的权限

./luya import

调整邮件组件

为了自定义用于发送邮件的邮件组件,请使用给定的回调定义表单组件。

'components' => [
    //...
    'forms' => [
        'class' => 'luya\forms\Forms',
        'emailMessage' => function (SubmissionEmail $email, Forms $form) {
        
            // your custom mailer integration is here, ensure to return a boolean
            // value whether sending was successfull or not!    
            return \Yii::$app->mailer->compose()
                ->setFrom(...)
                ->setTo($email->getRecipients())
                ->setSubject($email->getSubject())
                ->setTextBody($email->getBodyText())
                ->setHtmlBody($email->getBodyHtml())
                ->send();
        }
    ]
]

可能客户需要接收定制的电子邮件,因此您可以使用 $email->submission->getValueByAttribute('email_attribute_in_form') 提取属性值。

'emailMessage' => function (SubmissionEmail $email, Forms $form) {
    return Yii::$app->mailer->compose()
        ->setTo($email->submission->getValueByAttribute('email')) // receives the value from the user entered data.        
        ....
}

创建自定义表单字段块

默认块可能不符合您的需求,因此可以创建自己的输入块。

class MyDropDownBlock extends PhpBlock
{
    use FieldBlockTrait;
    
    public function name()
    {
        return 'Dropdown';
    }

    public function admin()
    {
        return '<p>My Dropdown {{vars.label}}</p>';
    }

    public function frontend()
    {
        Yii::$app->forms->autoConfigureAttribute(
            $this->getVarValue($this->varAttribute),
            $this->getVarValue($this->varRule, $this->defaultRule), 
            $this->getVarValue($this->varIsRequired),
            $this->getVarValue($this->varLabel),
            $this->getVarValue($this->varHint)
        );

        // Use all possible options with ActiveField or use the HtmlHelper
        return Yii::$app->forms->form->field(Yii::$app->forms->model, $this->getVarValue($this->varAttribute))
            ->dropDownList([1 => 'Foo', 2 => 'Bar']);
    }
}

开发

刷新消息文件

./vendor/bin/luya message msgconfig.php