0

I need to change the CSS class name on the wrapping div and therefore tried to use setTemplates example from CakePHP book.

I have just copied and pasted the following lines from the example:

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

After running the code I keep getting the Array to string conversion warning. The code is still working, but that warning is annoying as it is breaking the layout with debug turned on. I don't want to turn the debug off because of that silly warning. Any hint on how to get rid of it?

EDIT

As per suggestion here is the stacktrace:

include - APP/Template\Layout\default.ctp, line 49
Cake\View\View::_evaluate() - CORE\src\View\View.php, line 1010
Cake\View\View::_render() - CORE\src\View\View.php, line 971
Cake\View\View::renderLayout() - CORE\src\View\View.php, line 642
Cake\View\View::render() - CORE\src\View\View.php, line 600
Cake\Controller\Controller::render() - CORE\src\Controller\Controller.php, line 623
App\Controller\PagesController::display() - APP/Controller\PagesController.php, line 61
Cake\Controller\Controller::invokeAction() - CORE\src\Controller\Controller.php, line 440
Cake\Http\ActionDispatcher::_invoke() - CORE\src\Http\ActionDispatcher.php, line 119
Cake\Http\ActionDispatcher::dispatch() - CORE\src\Http\ActionDispatcher.php, line 93
Cake\Http\BaseApplication::__invoke() - CORE\src\Http\BaseApplication.php, line 78
Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65
Cake\Routing\Middleware\RoutingMiddleware::__invoke() - CORE\src\Routing\Middleware\RoutingMiddleware.php, line 59
Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65
Cake\Routing\Middleware\AssetMiddleware::__invoke() - CORE\src\Routing\Middleware\AssetMiddleware.php, line 88
Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65
Cake\Error\Middleware\ErrorHandlerMiddleware::__invoke() - CORE\src\Error\Middleware\ErrorHandlerMiddleware.php, line 93

Complete warning:

Notice (8): Array to string conversion [APP/Template\Layout\default.ctp, line 49]

Line 49:

'inputContainer' => '<div class="form-control">{{content}}</div>'
Morpheus
  • 8,829
  • 13
  • 51
  • 77
  • @Ayaou v3.4, read the title. – michail_w Feb 20 '17 at 16:13
  • my bad :(, did you try `$this->Form->templates($myTemplates);` instead ! – beta-developper Feb 20 '17 at 16:14
  • 1
    @Ayaou `templates` will be deprecated, but answering your question, yes I tried and the same warning is raised. – Morpheus Feb 20 '17 at 16:15
  • Whenever receiving errors, please always post **the _complete_ error**, that is, **including the _full_ stacktrace** (ideally copied from the logs where it is available in a properly readable fashion), even if the problem might be obvious to people who are familiar with CakePHP! Also show/describe the corresponding context, ie show/highlight the code that actually triggers the error, and please always mention your _exact_ (x.x.x) CakePHP version - thanks! – ndm Feb 20 '17 at 16:19
  • @ndm edited the question and added more details as per suggestion. – Morpheus Feb 20 '17 at 16:26
  • Are you you sure that you are looking at the correct file/stacktrace, respectively showing the correct code? I cannot see how defining an array element would cause that error. – ndm Feb 20 '17 at 16:33
  • That's a weird error, the `StringTemplateTrait::templates()` is not even mentionned !! – beta-developper Feb 20 '17 at 16:35
  • @ndm yes, I am sure. Whenever I remove these lines of code the warning is disappearing. Gonna create the helper file and see if that will do anything differently. – Morpheus Feb 20 '17 at 16:39
  • @Ayaou I believe that part is not even reached. – Morpheus Feb 20 '17 at 16:39
  • Using `$this->loadHelper('Form', [ 'templates' => 'app_form', ]);` helper worked. Thank you for your help guys! – Morpheus Feb 20 '17 at 16:44
  • Anyone please can give me an explanation, isn't `FormHelper` loaded by default in CakePHP ?? – beta-developper Feb 20 '17 at 16:45
  • @Ayaou it is loaded by default. Line from the docs - `To change the templates when the helper is loaded you can set the templates option when including the helper in your controller` – Morpheus Feb 20 '17 at 16:50
  • 1
    No helpers are loaded by default, but rather the view supports autoloading via magic property access. Loading the helper explicitly shouldn't fix anything with regards to defining an array in a variable. I still believe that you are not showing the full/correct context, I can only imagine that you echo the variable somehow (for example by using the wrong PHP tags, ie `=`), but even then the error message should point to a different line, the line with the closing bracket – ndm Feb 20 '17 at 16:55
  • i think this will Help you [link](https://stackoverflow.com/a/50749052/5968928) – Usman Ali Maan Jun 07 '18 at 23:42

0 Answers0