luyadev/luya-module-contactform

此模块提供了一种非常快速且安全的方式来创建可定制的联系表单。

1.2.0 2023-04-20 08:17 UTC

This package is auto-updated.

Last update: 2024-08-30 01:13:57 UTC


README

LUYA Logo

联系表单模块

Tests Maintainability Test Coverage Total Downloads

此模块提供了一种非常快速且安全的方式来创建可定制的联系表单。

安装

通过composer安装联系模块

composer require luyadev/luya-module-contactform

将联系表单模块添加到您的配置中

'modules' => [
    // ...
    'contactform' => [
        'class' => 'luya\contactform\Module',
        'useAppViewPath' => true, // When enabled the views will be looked up in the @app/views folder, otherwise the views shipped with the module will be used.
        'mailTitle' => 'Contact Form',
        'attributes' => [
            'name', 'email', 'street', 'city', 'tel', 'message',
        ],
        'rules' => [
            [['name', 'email', 'street', 'city', 'message'], 'required'],
            ['email', 'email'],
        ],
        'recipients' => [
            'admin@example.com',
        ],
    ],  
    // ...
],

为了定义属性标签,您可以按照以下方式配置模块

'attributeLabels' => [
    'email' => 'E-Mail',
],

默认情况下,LUYA会将值包装进Yii::t('app', $value)函数中,以便您能够翻译属性标签。上面的例子将看起来像这样Yii::t('app', 'E-Mail')

集成模块

为了在LUYA CMS中使用此模块,只需选择模块块,将其拖放到页面上。放下块后,编辑模块块并选择contactform模块。这可能会抛出一个异常,表明没有视图文件。为了创建视图文件,请参阅下一节。

视图文件

通常视图文件位于项目根目录下的views文件夹中,在views/contactform/default/index.php创建对应模型数据的视图文件,内容如下

<?php

use yii\widgets\ActiveForm;
use yii\helpers\Html;

/** @var \yii\base\Model $model Contains the model object based on DynamicModel yii class. */
/** @var \luya\web\View $this The current View object */
/** @var ActiveForm $form The ActiveForm Object */
?>
<?php if (Yii::$app->session->getFlash($this->context::CONTACTFORM_SUCCESS_FLASH)): ?>
    <div class="alert alert-success">The form has been submitted successfully.</div>
<?php else: ?>
    <?php $form = ActiveForm::begin(); ?>
    <?= $form->field($model, 'name'); ?>
    <?= $form->field($model, 'email'); ?>
    <?= $form->field($model, 'street'); ?>
    <?= $form->field($model, 'city'); ?>
    <?= $form->field($model, 'tel'); ?>
    <?= $form->field($model, 'message')->textarea(); ?>
    <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
    <?php ActiveForm::end(); ?>
<?php endif; ?>

当表单验证成功时,变量$success将为true,此外还将设置一个Yii2闪存消息Yii::$app->session->setFlash('contactform_success'),键名为contactform_success

为了确保表单只提交一次,请使用LUYA Core SubmitButtonWidget。

SubmitButtonWidget::widget(['label' => 'Send', 'pushed' => 'Sending...', 'activeForm' => $form, 'options' => ['class' => 'btn btn-primary']]);

提示:为了使用星号样式标记必填字段,您可以使用以下CSS

div.required label.control-label:after {
   content: " *";
   color: red;
}

成功后触发

您可以定义一个匿名函数,该函数将在成功后触发,函数的第一个参数可以是模型,将被分配为[[\luya\base\DynamicModel]]。示例回调

'modules' => [
    // ...
    'contactform' => [
        // ...
        'callback' => function($model) {
            // insert the name of each contact form into `contact_form_requests` table:
            Yii::$app->db->createCommand()->insert('contact_form_requests', ['name' => $model->name])->execute();
        }
    ],
];

电子邮件中的字段排序

您可以在发送给联系表单收件人的邮件中排序字段。这可以放在联系表单配置的规则之前。

对于简短和简短的表单,也可以使用此表示法

'modules' => [
    // ...
    'contactform' => [
        // ...
        'detailViewAttributes' => ['name', 'newsletter:bool', 'city', 'message:text:Label for Message'],    
       // ...
    ],
];

对于长而复杂的表单,我们建议使用此表示法

'modules' => [
    // ...
    'contactform' => [
        // ...
        'detailViewAttributes' => [
            ['attribute' => 'name'],
            ['attribute' => 'email'],
            ['attribute' => 'newsletter:bool'],
            ['attribute' => 'city:integer'],
            ['attribute' => 'message'],
        ],       
       // ...
    ],
];

高级配置