1. Go to this page and download the library: Download andrewdanilov/yii2-feedback library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
return [
// ...
'controllerMap' => [
// ...
'callback' => [
'class' => 'andrewdanilov\feedback\FeedbackController',
// If you want you can use your own views for form and mail.
// Just copy views files from `src/views` and `src/mail` folders
// of extension to your location, for example, to `@frontend/views/feedback`
// and `@frontend/mail/feedback` and set correspondent `formView`,
// `mailView` and `mailLayout` paths here:
'formView' => '@frontend/views/feedback/default', // optional
'mailView' => '@frontend/mail/feedback/default', // optional
'mailLayout' => '@frontend/mail/feedback/layouts/html', // optional
// label for extra field in mail template
'extraFieldLabel' => 'Extra data', // optional
'from' => ['[email protected]' => 'My Site'],
'to' => ['[email protected]', '[email protected]'],
'subject' => 'Mail from site', // optional
'fields' => [
'name' => [
' 'validator' => ['MyValidatorClass', 'myEmailValidator'], // optional, validator as an anonymous function, a function name as a string, or a valid PHP callable array
],
'country' => [
'label' => 'Select country',
'type' => 'select',
'items' => [
0 => 'Select your country',
1 => 'Great Britain',
2 => 'Germany',
3 => 'Norway',
],
'class' => 'field-country',
],
'phone' => [
'label' => 'Phone',
'placeholder' => 'Enter your phone',
'type' => 'tel',
'class' => 'field-phone',
],
'comment' => [
'label' => 'Comment',
'placeholder' => 'Enter your comment',
'type' => 'textarea',
'maxlength' => 1000,
'class' => 'field-comment',
],
'img' => [
'label' => 'Upload Image',
'type' => 'file',
'multiple' => true, // optional, default is false
'maxFiles' => 10, // optional, default is 0, that equals no restriction
'extensions' => 'pdf, docx', // optional, default is empty string, that equals to any extension
'uploadDir' => '@webroot/upload/files', // optional, default is '@webroot/upload'
'class' => 'field-file',
],
'accept_agreement' => [
'label' => 'Accept user agreement',
'type' => 'checkbox',
'default' => 1,
'class' => 'field-agreement',
'exclude' => true, // field will be excluded from mail
],
],
],
],
];
namespace frontend\components\validators;
class MyValidatorClass
{
// method accepts three parameters: first is validating field name,
// second - its value, third - all form values in case if you need
// to check some other fields to correctly validate current field
public static function myEmailValidator($field_name, $field_value, $fields_values)
{
// check field only if it is filled
if ($field_value && !filter_var($field_value, FILTER_VALIDATE_EMAIL)) {
// if it is not ok, return an arror
return [
'error' => 'Email is incorrect',
];
// or you can just return boolean false: "return false;"
}
return true; // if all is ok - return boolean true
}
public static function myPhoneValidator($field_name, $field_value, $fields_values)
{
// check field only if it is filled
if ($field_value && !preg_match('~^[\d\-\(\)\+ ]+$~', $field_value)) {
// you can just return boolean false instead of an error array,
// then default error message will be used
return false;
}
return true; // if all is ok - return boolean true
}
}
return [
// ...
'urlManager' => [
// ...
'enableStrictParsing' => true,
'rules' => [
// ...
'<controller>/send' => '<controller>/send', // this needs to be added to represent ajax handler
'' => 'site/index',
],
],
];
<?= \andrewdanilov\feedback\FeedbackWidget::widget([
// controller id configured in 'controllerMap' section of your config
'controller' => 'callback',
// optional: widget ID
'id' => 'mywidgetID',
// optional: instead of form displays button, which call floating form on click
'lightbox' => [
// optional: tag represents button triggering form appearance (div, span, i, a, etc.)
// if not set or null, button will not be displayed, than you need to
// create it manually, i.e.:
// <a href="javascript:;" data-fancybox data-src="#feedback-mywidgetID" data-extra="extra message">Call me back!</a>
// in that case you can pass extra data to your form, and each triggering button
// can provide its own data to single form instance.
'button' => 'div',
// optional: button name
'label' => 'Call me!',
// optional: options for yii Html::tag() helper
'options' => ['class' => 'callback-btn'],
// optional: delay before lightbox form will be closed
'delay' => 2500,
// optional: close button template
'closeBtn' => '<a href="javascript:$("[data-fancybox-close]").click();$.fancybox.close(true);" class="close-btn">x</a>',
// optional: lightbox window title
'title' => 'Call me back!',
],
// optional: javascript code to execute after success submit happen
'jsCallback' => 'ga("send", "event", "my_form", "submit"); yaCounter100500.reachGoal("my_goal");',
// optional: javascript code to execute after form send error happen
'jsErrorCallback' => 'alert("Error happened");',
// optional: redirect visitor to page after submitting form
'redirect' => \yii\helpers\Url::to(['site/index']),
// optional: success form submit message
'successMessage' => 'Message sent. Please, wait for an answer.',
// optional: name and options for submit button
'submitButton' => [
'name' => 'Send msg', // optional
'options' => ['class' => 'form-submit-button'], // optional
],
// optional: class to be added to input element and its parent in case an error, default is 'has-errors'
'errorFieldClass' => 'error',
// optional: scroll to first error field after form send if there is an error, default is true
'scrollToFirstError' => false,
// optional: class of element containing error message, default is 'help-block'
'errorFieldAlertElementClass' => 'alert',
// optional: form block options
'options' => [
'class' => 'form-block-class',
],
])