cakephphelperformhelper

global helper config in cakephp 3


hi im new to stack overflow and i usually find my answers from others questions . i want to config cakephp form helper for entire project i want to know where should i put these configurations to load for entire project.

my config is like this

$this->Form->setTemplates([
'button'               => '<button{{attrs}}>{{text}}</button>' ,
'checkbox'             => 'haji' ,
'checkboxFormGroup'    => '<label class="col-lg-2 control-label">{{mytitle}}</label><div class="col-md-10"> <div class="md-checkbox-list"><div class=""><input id="checkbox30" class="md-check" type="checkbox">
            <label for="checkbox30">
                <span></span>
                <span class="check"></span>
                <span class="box"></span> Option 1 </label></div></div></div>' ,
'checkboxWrapper'      => '<div class="aaaaaaaaaaaaaaa">{{label}}</div>' ,
'dateWidget'           => '{{year}}{{month}}{{day}}{{hour}}{{minute}}{{second}}{{meridian}}' ,
'error'                => '<div class="error-message">{{content}}</div>' ,
'errorList'            => '<ul>{{content}}</ul>' ,
'errorItem'            => '<li>{{text}}</li>' ,
'file'                 => '<input type="file" name="{{name}}"{{attrs}}>' ,
'fieldset'             => '<fieldset{{attrs}}>{{content}}</fieldset>' ,
'formStart'            => '<form class="form-horizontal" {{attrs}}><div class="form-body">' ,
'formEnd'              => '</div> </form>' ,
'formGroup'            => '{{label}}<div class="col-md-10">{{input}}<div class="form-control-focus"> </div></div>' ,
'hiddenBlock'          => '<div style="display:none;">{{content}}</div>' ,
'input'                => '<input class="form-control" type="{{type}}" name="{{name}}"{{attrs}}/>' ,
'inputSubmit'          => '<input type="{{type}}"{{attrs}}/>' ,
'inputContainer'       => '<div class="form-group form-md-line-input">{{content}}</div>' ,
//  'inputContainer'       => '<div class="input {{type}}{{required}}">{{content}}</div>' ,
'inputContainerError'  => '<div class="input {{type}}{{required}} error">{{content}}{{error}}</div>' ,
'label'                => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>' ,
'nestingLabel'         => '{{hidden}}<label{{attrs}}>{{input}}{{text}}</label>' ,
'legend'               => '<legend>{{text}}</legend>' ,
'multicheckboxTitle'   => '<legend>{{text}}</legend>' ,
'multicheckboxWrapper' => '<fieldset{{attrs}}>{{content}}</fieldset>' ,
'option'               => '<option value="{{value}}"{{attrs}}>{{text}}</option>' ,
'optgroup'             => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>' ,
'select'               => '<select class="form-control" name="{{name}}"{{attrs}}>{{content}}</select>' ,
'selectMultiple'       => '<select name="{{name}}[]" multiple="multiple"{{attrs}}>{{content}}</select>' ,
'radio'                => '<input type="radio" name="{{name}}" value="{{value}}"{{attrs}}>' ,
'radioWrapper'         => '{{label}}' ,
'textarea'             => '<textarea class="form-control" name="{{name}}"{{attrs}}>{{value}}</textarea>' ,
'submitContainer'      => '<div class="">{{content}}</div>' ,

]); ?>

thanks a lot guys .and sorry for bad english


Solution

  • Make a config file for the templates and place it in the config folder, you can name it whatever you would like, in this example it's form-templates

    /config/form-templates.php

    <?php
    return [
    'button'               => '<button{{attrs}}>{{text}}</button>' ,
    'checkbox'             => 'haji' ,
    'checkboxFormGroup'    => '<label class="col-lg-2 control-label">{{mytitle}}</label><div class="col-md-10"> <div class="md-checkbox-list"><div class=""><input id="checkbox30" class="md-check" type="checkbox">
                <label for="checkbox30">
                    <span></span>
                    <span class="check"></span>
                    <span class="box"></span> Option 1 </label></div></div></div>' ,
    'checkboxWrapper'      => '<div class="aaaaaaaaaaaaaaa">{{label}}</div>' ,
    'dateWidget'           => '{{year}}{{month}}{{day}}{{hour}}{{minute}}{{second}}{{meridian}}' ,
    'error'                => '<div class="error-message">{{content}}</div>' ,
    'errorList'            => '<ul>{{content}}</ul>' ,
    'errorItem'            => '<li>{{text}}</li>' ,
    'file'                 => '<input type="file" name="{{name}}"{{attrs}}>' ,
    'fieldset'             => '<fieldset{{attrs}}>{{content}}</fieldset>' ,
    'formStart'            => '<form class="form-horizontal" {{attrs}}><div class="form-body">' ,
    'formEnd'              => '</div> </form>' ,
    'formGroup'            => '{{label}}<div class="col-md-10">{{input}}<div class="form-control-focus"> </div></div>' ,
    'hiddenBlock'          => '<div style="display:none;">{{content}}</div>' ,
    'input'                => '<input class="form-control" type="{{type}}" name="{{name}}"{{attrs}}/>' ,
    'inputSubmit'          => '<input type="{{type}}"{{attrs}}/>' ,
    'inputContainer'       => '<div class="form-group form-md-line-input">{{content}}</div>' ,
    //  'inputContainer'       => '<div class="input {{type}}{{required}}">{{content}}</div>' ,
    'inputContainerError'  => '<div class="input {{type}}{{required}} error">{{content}}{{error}}</div>' ,
    'label'                => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>' ,
    'nestingLabel'         => '{{hidden}}<label{{attrs}}>{{input}}{{text}}</label>' ,
    'legend'               => '<legend>{{text}}</legend>' ,
    'multicheckboxTitle'   => '<legend>{{text}}</legend>' ,
    'multicheckboxWrapper' => '<fieldset{{attrs}}>{{content}}</fieldset>' ,
    'option'               => '<option value="{{value}}"{{attrs}}>{{text}}</option>' ,
    'optgroup'             => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>' ,
    'select'               => '<select class="form-control" name="{{name}}"{{attrs}}>{{content}}</select>' ,
    'selectMultiple'       => '<select name="{{name}}[]" multiple="multiple"{{attrs}}>{{content}}</select>' ,
    'radio'                => '<input type="radio" name="{{name}}" value="{{value}}"{{attrs}}>' ,
    'radioWrapper'         => '{{label}}' ,
    'textarea'             => '<textarea class="form-control" name="{{name}}"{{attrs}}>{{value}}</textarea>' ,
    'submitContainer'      => '<div class="">{{content}}</div>'
    ];
    

    Then in your View/AppView.php initialize method, load the form helper with the template config file above:

    public function initialize()
    {
        parent::initialize();
        $this->loadHelper('Form', ['templates' => 'form-templates']);
    }