abwebdevelopers / oc-forms-plugin
为OctoberCMS定制的表单构建器
1.5.1
2021-08-04 23:28 UTC
Requires
- php: >=7.0
- composer/installers: ~1.0
- yetii/html-element: 1.1.3
Requires (Dev)
- mockery/mockery: 1.2.2
- phpunit/phpunit: ~5.7
README
October插件,用于创建和自定义样式表单,具有多种自定义功能、电子邮件、存储提交等。
使用方法(October CMS)
简单。首先,你需要一个表单。安装此插件将自动生成一个基本的联系表单。你可以删除它或将其作为参考 - 由你决定。
设置
主要有三个设置级别:网站范围设置
> 表单设置
> 字段设置
。
覆盖网站范围和表单设置的表单和字段设置(分别)旁边有一个“覆盖”复选框,选中后允许相应的设置覆盖“更全局”的设置版本。
某些设置仅在全局级别可用(Google recaptcha密钥、队列电子邮件等),而某些设置仅在字段级别可用。
创建表单
配置全局设置后,转到后端菜单中的自定义表单导航项并点击“创建”。
在这里,你可以输入表单的标题,以及一个代码(用于在布局/组件中引用正确的表单)。
请注意 表单字段的延迟绑定尚未配置,这意味着你需要在添加任何字段之前保存表单。请随意提交修复此问题的PR。
保存表单后,你现在可以点击“创建字段”按钮添加字段
这是一个相当直接的过程,每个字段都有一个注释简要说明字段的目的。
可以配置验证 - 接受由管道符号(|)分隔的规则字符串,如正常的Laravel / October 验证规则。目前仅支持每个字段一个消息。
将表单添加到布局
就像组件一样,打开布局并插入相应的组件。此组件有一个必需属性
- 使用表单(formCode): 这通过代码字段引用一个
Form
。请确保它已正确设置。
事件
目前大约有24个可能触发的事件(当然取决于你的配置)。如果你觉得缺少了重要的事件,请提交问题或PR。
// Runs at the beginning of "onRun" (when loading a page with a CustomForm) Event::listen('abweb.forms.beforeRun', function (CustomForm $customForm) { // Do something... Log::debug('Loaded form: ' . $customForm->form->name); }); // Runs at the end of "onRun" (when loading a page with a CustomForm) Event::listen('abweb.forms.afterRun', function (CustomForm $customForm) { // Do something... }); // Runs at the beginning of "onFormSubmit" (when submitting a CustomForm) Event::listen('abweb.forms.beforeFormSubmit', function (CustomForm $customForm) { // Do something... Log::debug('User submitted form: ' . $customForm->form->name); }); // Runs before validating the payload of a form. Can adjust data, rules, and messages Event::listen('abweb.forms.beforeValidateForm', function (CustomForm $customForm, array &$data, array &$rules, array &$messages, Validator $validator) { // Do something... }); // Runs if validating the payload of a form fails Event::listen('abweb.forms.onValidateFormFail', function (CustomForm $customForm, array $data, array $rules, array $messages, Validator $validator) { // Do something... }); // Runs if validating the payload of a form is successful Event::listen('abweb.forms.afterValidateForm', function (CustomForm $customForm, array $data, array $rules, array $messages) { // Do something... }); // Runs if validating the recaptcha response fails Event::listen('abweb.forms.onRecaptchaFail', function (CustomForm $customForm, string $recaptchaResponse) { // Do something... }); // Runs if validating the recaptcha response is successful Event::listen('abweb.forms.onRecaptchaSuccess', function (CustomForm $customForm, array $data, array $rules, array $messages) { // Do something... }); // Runs at the end of "onFormSubmit" (when submitting a CustomForm) Event::listen('abweb.forms.afterFormSubmit', function (CustomForm $customForm, array $data, $response) { // Do something... }); // Runs before rendering the form (or retrieving pre-rendered cache) Event::listen('abweb.forms.beforeRenderPartial', function (CustomForm $customForm, bool $cachingEnabled) { // Do something... if ($cachingEnabled) { // Do something... } }); // Runs after rendering the form (or retrieving pre-rendered cache). Can adjust HTML. Event::listen('abweb.forms.afterRenderPartial', function (CustomForm $customForm, string &$html) { // Do something... $html .= '<script src="https://api.google.com/.../library.js"></script>'; }); // Runs before sending notification emails. Can adjust data and recipient Event::listen('abweb.forms.beforeSendNotification', function (CustomForm $customForm, array &$data, array &$to) { // Do something... }); // Runs before validating notification recipients. Can adjust data, recipient and rules Event::listen('abweb.forms.beforeNotificationValidation', function (CustomForm $customForm, array $data, array &$to, array &$rules, Validator &$validator) { // Do something... }); // Runs if validating notification recipients fails Event::listen('abweb.forms.onNotificationValidationFail', function (CustomForm $customForm, array $data, array $to, array $rules, Validator $validator) { // Do something... Log::info($validator->messages()->toArray()); }); // Runs if validating notification recipients is successful Event::listen('abweb.forms.onNotificationValidationSuccess', function (CustomForm $customForm, array $data, array $to, array $rules) { // Do something... }); // Runs when configuring the $message to send a notification to recipients Event::listen('abweb.forms.onSendNotification', function (CustomForm $customForm, &$message, $to) { // Do something... $message->replyTo('noreply@domain.com'); }); // Runs after sending (or queueing) notification to recipients Event::listen('abweb.forms.afterSendNotification', function (CustomForm $customForm, array $data, bool $success) { // Do something... if (!$success) { Log::debug('Dammit whats wrong now?'); } }); // Runs before sending auto reply email. Can adjust data, recipient name and email Event::listen('abweb.forms.beforeSendAutoReply', function (CustomForm $customForm, array &$data, &$toEmail, &$toName) { // Do something... $toEmail = 'new@example.org'; $toName = 'Mr. Nobody'; }); // Runs if validating auto reply recipient fails. Event::listen('abweb.forms.onAutoReplyValidationFail', function (CustomForm $customForm, array $data, $toEmail, $toName, string $failedOn) { // Do something... if ($failedOn === 'email') { Log::debug('Invalid auto-reply email'); } else { // 'name' Log::debug('Invalid auto-reply name'); } }); // Runs when configuring the $message to send an automatic reply to the user Event::listen('abweb.forms.onSendAutoReply', function (CustomForm $customForm, &$message, $to) { // Do something... $message->bcc('mwahahaha@domain.com'); }); // Runs after sending (or queueing) auto reply email Event::listen('abweb.forms.afterSendAutoReply', function (CustomForm $customForm, array $data, bool $success) { // Do something... if (!$success) { Log::debug($data); } }); // Runs before saving the submission in the database. Can adjust the Submission's data Event::listen('abweb.forms.beforeSaveSubmission', function (CustomForm $customForm, array &$submissionData) { // Do something... $submissionData['extraField'] = 'Add this to the database please'; }); // Runs after saving the submission in the database Event::listen('abweb.forms.afterSaveSubmission', function (CustomForm $customForm, Submission $submission) { // Do something... if ($submission->url == '/') { $submission->delete(); } }); // Runs before setting email template vars. Can adjust the variables Event::listen('abweb.forms.beforeSetTemplateVars', function (CustomForm $customForm, array &$vars) { // Do something... $vars['date'] = \Carbon\Carbon::now()->format('jS F Y'); });
错误和功能请求
我们鼓励开源,所以如果你发现任何错误、错别字、问题或想出一些很棒的功能,请在GitHub仓库中提交问题或PR。