Cakephp 3 different custom formhelpers templates

So, I'm at work (working with sensitive data, I can add for posterity), and the powers that are being solved, we need to use the whole powerful and least documented new Cakephp 3.0 tool (beta at this time).

Edit: My goal is to create several different templates for forms that invoke forms or input methods through the template. This is actually not a good example. Setting up the FormHelper template Usage: As you can see from the book (and nowhere else on the Internet), a very short documentation is as follows: http://book.cakephp.org/3.0/en/core-libraries/helpers/form.html#customizing-the- templates-formhelper-uses

The site says that you can use the template method and then give an indefinite "use":

$myTemplates = [ 'inputContainer' => '<div class="form-control">{{content}}</div>', ]; 

$ this-> form-> templates ($ myTemplates);

He then says that you can use the input () method, for which it gives no example. Last but not least, the custom FormHelper template should allow you to “create” or “create” as many of these custom forms as possible, but they provide no example how to do this !? lulwut?

I can easily use it once, as their example, but where is the power in one custom template? It doesn't do me any good.

So, for a new possible solution, I will try to get a new error.

I get this error (in my view) (from the following code):

Fatal error Error: Class 'Configure' not found

 //within bootstrap.php Configure::write('templates', [ 'shortForm' => [ 'formstart' => '<form class="" {{attrs}}>', 'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>', 'input' => '<div class="col-md-4"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>', 'select' => '<div class="col-md-4"><select name="{{name}}"{{attrs}}>{{content}}</select> </div>', 'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}} </div>', 'checkContainer' => '',], 'longForm' => [ 'formstart' => '<form class="" {{attrs}}>', 'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>', 'input' => '<div class="col-md-6"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>', 'select' => '<div class="col-md-6"><select name="{{name}}"{{attrs}}>{{content}}</select> </div>', 'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}} </div>', 'checkContainer' => '',], 'fullForm' => [ 'formstart' => '<form class="" {{attrs}}>', 'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>', 'input' => '<div class="col-md-10"><input type="{{type}}" name="{{name}}" {{attrs}} /> </div>', 'select' => '<div class="col-md-10"><select name="{{name}}"{{attrs}}>{{content}}</select> </div>', 'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}} </div>', 'checkContainer' => '',] ]); //within my view <?php $this->Form->templates(Configure::read('templates.shortForm')); ?> 

Old update : I added

 use "Cake\Core\Configure;" 

in my view, and everything works fine, but I would like to add this to the corresponding file in the hierarchy, so I don’t need to add this to all the views,

Unless, of course, this causes performance problems for the entire application. Does anyone know which file it should go to? Regards and TIA!

New update . I just figured it out. So simple! check out my answer below! Hope this helped someone

+6
source share
2 answers

This fix allows you to create custom template forms (from cakephp 3 !!!!) using bootstrap. If you want to set the dimensions using a form helper and all this is kindness (security and what not).

Jose Zap from CakePHP told me to try bootstrap plugins and widgets and what not, but the real way to do this was to be as follows:

Step 1: create config / templatesConfig.php and add your own form elements.

 <?php $config = [ 'Templates'=>[ 'shortForm' => [ 'formStart' => '<form class="" {{attrs}}>', 'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>', 'input' => '<div class="col-md-4"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>', 'select' => '<div class="col-md-4"><select name="{{name}}"{{attrs}}>{{content}}</select></div>', 'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}}</div>', 'checkContainer' => '',], 'longForm' => [ 'formStart' => '<form class="" {{attrs}}>', 'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>', 'input' => '<div class="col-md-6"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>', 'select' => '<div class="col-md-6"><select name="{{name}}"{{attrs}}>{{content}}</select></div>', 'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}}</div>', 'checkContainer' => '',], 'fullForm' => [ 'formStart' => '<form class="" {{attrs}}>', 'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>', 'input' => '<div class="col-md-10"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>', 'select' => '<div class="col-md-10"><select name="{{name}}"{{attrs}}>{{content}}</select></div>', 'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}}</div>', 'checkContainer' => '',] ] ]; 

Step 2: From your controller inside the method, call this line to view it correctly.

Remember to add this to the top of your controller.

 use Cake\Core\Configure; $this->set('form_templates', Configure::read('Templates')); 

Step 3. Add this to bootstrap.php

 // Load an environment local configuration file. // You can use this file to provide local overrides to your // shared configuration. Configure::load('templatesConfig','default'); //fixed 

Step 4 (final): Add this line with the name of the template you want to use Bam !.

 <?php $this->Form->templates($form_templates['shortForm']); ?> 
+7
source

Let's say you need all the inputs to use special markup in the form to display the label after input (by default it is before) and another class for the hard-coded error-message for errors:

 $this->Form->create($entity, ['templates' => [ 'formGroup' => '{{input}}{{label}}', 'error' => '<div class="error">{{content}}</div>' ]]); 

If you want to configure only one input, pass the same "template" keys to the FormHelper::input() parameters, for example:

 $this->Form->input('fieldname', ['templates' => [ 'formGroup' => '{{input}}{{label}}', 'error' => '<div class="error">{{content}}</div>' ]]); 

If you need to define several patterns and reuse them whenever you want, here is what you can try (I write it here, never used it before):

 // in bootstrap (as this is a config i believe Configure::write('templates', [ 'foo' => [....], 'bar' => [....] ]); // in any view $this->Form->templates(Configure::read('templates.foo')); $this->Form->create(...); .... 

You can also create your own helper and define templates there, etc.

In fact, it all depends on what you want to achieve, but this should give you a good idea of ​​how templates work (not only in forms).

0
source

Source: https://habr.com/ru/post/975909/


All Articles