PHP code example of metalguardian / yii2-form-builder

1. Go to this page and download the library: Download metalguardian/yii2-form-builder 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/ */

    

metalguardian / yii2-form-builder example snippets


php composer.phar 

    
    namespace app\models;
    
    use metalguardian\formBuilder\ActiveFormBuilder;
    
    /**
     */
    class Example extends \yii\db\ActiveRecord
    {
        .....
        /**
         * @return array
         */
        public function getFormConfig()
        {
            return [
                'label' => [
                    'type' => ActiveFormBuilder::INPUT_TEXT,
                ],
                'content' => [
                    'type' => ActiveFormBuilder::INPUT_TEXTAREA,
                    'hint' => 'hint about field',
                ],
                'type' => [
                    'type' => ActiveFormBuilder::INPUT_DROPDOWN_LIST,
                    'items' => [1 => 'One', 2 => 'Two'],
                    'options' => [
                        'prompt' => 'select',
                    ],
                ],
                'published' => [
                    'type' => ActiveFormBuilder::INPUT_CHECKBOX,
                ],
                'redactor' => [
                    'type' => ActiveFormBuilder::INPUT_WIDGET,
                    'widgetClass' => \vova07\imperavi\Widget::className(),
                ],
                'raw_data' => [ // need to define attribute `raw_data` in model 
                    'type' => ActiveFormBuilder::INPUT_RAW,
                    'value' => 'raw html data',
                ],
            ];
        }
    }

Now in form view you can write something like this:

    .....
     $form = \metalguardian\formBuilder\ActiveFormBuilder::begin(); 
 \metalguardian\formBuilder\ActiveFormBuilder::end(); 

    
    namespace app\helpers;
    
    use metalguardian\formBuilder\ActiveFormBuilder;
    
    /**
     */
    class Helper
    {
        .....
        /**
         * @return array
         */
        public static function getLabelConfig()
        {
            return [
                'type' => ActiveFormBuilder::INPUT_TEXT,
            ];
        }
        
        /**
         * @return array
         */
        public static function getContentConfig()
        {
            return [
                'type' => ActiveFormBuilder::INPUT_TEXTAREA,
            ];
        }
    }

Now you can use different models in one form

    .....
     $form = \metalguardian\formBuilder\ActiveFormBuilder::begin(); 
 \metalguardian\formBuilder\ActiveFormBuilder::end();