PHPackages                             andrewdanilov/yii2-feedback - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. andrewdanilov/yii2-feedback

ActiveYii2-extension[File &amp; Storage](/categories/file-storage)

andrewdanilov/yii2-feedback
===========================

Feedback and callback forms

1.0.23(8mo ago)2186MITPHPPHP &gt;=5.6.0

Since Jan 16Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/AndrewDanilov/yii2-feedback)[ Packagist](https://packagist.org/packages/andrewdanilov/yii2-feedback)[ Patreon](https://www.patreon.com/andrewdanilov)[ RSS](/packages/andrewdanilov-yii2-feedback/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (2)Versions (25)Used By (0)

Feedback forms
==============

[](#feedback-forms)

Component for creating feedback forms with customizable set of fields, user validators, html templates, file uploads, javascript callbacks.

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
composer require andrewdanilov/yii2-feedback "~1.0.0"

```

or add

```
"andrewdanilov/yii2-feedback": "~1.0.0"

```

to the `require` section of your `composer.json` file.

Usage
-----

[](#usage)

In common/config/main\_local.php config file setup mailer component:

```
return [
    // ...
    'components' => [
        // ...
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'useFileTransport' => false,
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'username' => 'admin@example.com',
                'password' => 'yourpassword',
                'host' => 'smtp.example.com',
                'port' => '465',
                'encryption' => 'ssl',
            ],
        ],
    ],
];
```

In `frontend/config/main.php` add following lines to controllerMap section:

```
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' => ['admin@example.com' => 'My Site'],
            'to' => ['admin@example.com', 'admin2@example.com'],
            'subject' => 'Mail from site', // optional
            // Your own model for loading, validating and sending data through email
            // must implements validateData(), sendFeedback() methods. You can extend
            // it from default class \andrewdanilov\feedback\FormModelClass
            'formModelClass' => 'andrewdanilov\feedback\FormModelClass', // optional
            'fields' => [
                'name' => [
                    'required' => true, // optional
                    'label' => 'Name', // optional
                    'placeholder' => 'Enter your name', // optional
                    'type' => 'text', // optional, default 'text'
                    'maxlength' => 255, // optional
                    'class' => 'field-name', // optional
                    'style' => 'margin-bottom: 10px;', // optional
                    'errors' => [ // optional, default values is:
                        'required' => 'Field "{label}" is required.', // if requred field is empty
                        'maxlength' => 'Field "{label}" length of {maxlength} exeeded.', // if field length exeeded
                        'error' => 'Field "{label}" is incorrect.', // if validator returns false or empty error string
                    ],
                ],
                'address', // simple notation without config array
                'email' => [
                    'required' => true,
                    'label' => 'Email',
                    'placeholder' => 'Enter your e-mail',
                    'type' => 'email',
                    'class' => 'field-email',
                    '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
                ],
            ],
        ],
    ],
];
```

You can add as many controller mappings as you want. Each controller mapping represents one feedback form instance.

You can use your own validators for field values. You just need to define 'validator' property of field defintion (see config above). For example, you can use php callable array:

```
// key is the field name
'my_email' => [
    //...
    'validator' => ['frontend\components\validators\MyValidatorClass', 'myEmailValidator'],
];
'my_phone' => [
    //...
    'validator' => ['frontend\components\validators\MyValidatorClass', 'myPhoneValidator'],
];

```

Then you need to create methods `myEmailValidator` and `myPhoneValidator` within class `MyValidatorClass`:

```
