Customizing the Templates FormHelper for just plugins CakePHP 3? - php

I would like to customize a form helper template for just a plugin, not across the App.
Example: 'inputContainer' => '<div class="form-control">{{content}}</div>',
How do I go about doing this in CakePHP 3?

As per the docs - to change theme inline at runtime use setTemplate:
$myTemplates = [
'inputContainer' => '<div class="form-control">{{content}}</div>',
];
?>
<?= $this->Form->create('Users') ?>
<? $this->Form->setTemplates($myTemplates); ?>
<?=
$this->Form->input('email', [
'class' => 'form-control',
'templates' => [
'formGroup' => '{{input}}{{label}}'
]])
?>
You can't use the templates option on the Form for a dynamic template, it will only look for a config file listing template items in /config.

Related

DatePicker widget brakes bootstrap's css

Now I am building my first project on Yii2. One page has a datepicker field, it's works ok but that feature is braking up my css styles especially on this page. How can I repair?
It's mine form:
<?= $form->field($model, 'first_date')->widget(kartik\date\DatePicker::className(), ['pluginOptions' => ['format' => 'yyyy-mm-dd','todayHighlight' => true,'startDate' => 'today','autoclose' => true];])?>
I've tried to add Asset but it isn't help.
class DatepickerAsset extends AssetBundle{
public $sourcePath = '#bower/bootstrap-datepicker/dist';
public $js = ['js/bootstrap-datepicker.min.js'];
public $css = ['css/bootstrap-datepicker.min.css'];}
What is wrong?
May be I have to add some options in config/web.php ? Something like that:'view' => ['theme' => ['class' =>yii\base\Theme::className(), 'basePath' => ]]
braked fonts
good view
Development console:
I've found css file which hides main css and refactor that.

How to use FormHelper::postLink() inside of a form?

I want to create a Cakephp delete post link within form like the following. But the very first delete post button doesn't include delete Form when I inspect in browser and can't delete but the others include as I want and can delete.
Is it cakephp bug or something I need to change my source code?
<?php
echo $this->Form->create('Attendance', array('required' => false, 'novalidate' => true));
foreach($i = 0; $i < 10; i++):
echo $this->Form->input('someinput1', value => 'fromdb');
echo $this->Form->input('someinput2', value => 'fromdb');
echo $this->Form->postLink('Delete',array('action'=>'delete',$attendanceid),array('class' => 'btn btn-dark btn-sm col-md-4','confirm' => __('Are you sure you want to delete')));
endforeach;
echo $this->Form->button('Submit', array('class' => 'btn btn-success pull-right'));
echo $this->Form->end();
?>
Forms cannot be nested, the HTML standard forbids that by definition. If you try to, most browsers will drop the nested form and render its contents outside of the parent form.
If you need post links inside of existing forms, then you must use the inline or block options (available as of CakePHP 2.5, inline has been removed in CakePHP 3.x), so that the new form is being set to a view block that can be rendered outside of the main form.
CakePHP 2.x
echo $this->Form->postLink(
'Delete',
array(
'action' => 'delete',
$attendanceid
),
array(
'inline' => false, // there you go, disable inline rendering
'class' => 'btn btn-dark btn-sm col-md-4',
'confirm' => __('Are you sure you want to delete')
)
);
CakePHP 3.x
echo $this->Form->postLink(
'Delete',
[
'action' => 'delete',
$attendanceid
],
[
'block' => true, // disable inline form creation
'class' => 'btn btn-dark btn-sm col-md-4',
'confirm' => __('Are you sure you want to delete')
]
);
Close the main form and output post link forms
// ...
echo $this->Form->end();
// ...
echo $this->fetch('postLink'); // output the post link form(s) outside of the main form
See also
CakePHP 2.x
API > FormHelper::postLink()
Cookbook > Views > Using view blocks
CakePHP 3.x
API > \Cake\View\Helper\FormHelper::postLink()
Cookbook > Views > Helpers > Form > Creating Standalone Buttons and POST links
Cookbook > Views > Using View Blocks

YII booster widget not appear in server

I need help here. I have a system developed with Yii framework. I used Yii Bootstrap and Yii Booster for the extensions. However, the booster widgets that I have implemented inside the system is not appearing when I uploaded the files into the LINUX server. It works fine in my laptop (development environment). The textarea, modal, tbselect2 all works well. However, it's not appear on the server side.
example:
//booster for tbckeditor
$this->widget('booster.widgets.TbCKEditor',array(
'model'=>$model, # Data-Model (form model)
'attribute'=>'content', # Attribute in the Data-Model
'editorOptions' => array(
'plugins' => 'basicstyles,toolbar,enterkey,entities,floatingspace,wysiwygarea,indentlist,list,dialog,dialogui,button,indent,fakeobjects',
)
) );
//booster for tbselect2
$this->widget('booster.widgets.TbSelect2',array(
'model' => $model,
'attribute' => 'job_cat_id',
'data' => CHtml::listData(AdmJobCat::model()->findAll(array('order'=>'jc_title ASC')), 'jc_id', 'jc_title'),
'options' => array('placeholder' => '--Please Select--','width' => '100%', 'allowClear'=>true,),
'htmlOptions' => array('multiple' => 'multiple'),
)
);
<?php $this->beginWidget('booster.widgets.TbModal', array('id' => 'withdrawModal')); ?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4>Withdraw Application(s)</h4>
</div>
<div class="container-modal">
<?php
$this->renderPartial('form/_withdraw_form'); //calling external form
?>
</div>
<?php
$this->endWidget(); //end modal widget
?>

Yii2: How can I place something into a Breadcrumbs widget?

I find the Breadcrumbs widget quite useful. However, on the right side within the widget there is enough space for something else. If I'd like to put a link ('a' tag, but could be actually any other small thing) right aligned into the Breadcrumbs, how could I do that? What is a simple and proper solution? Should I extend the class, develop my own, use begin and end of the widget somehow?
If you look at yii\widgets\Breadcrumbs you see that there is a third parameter in the breadcrumbs items
From the Yii2 file
[
'label' => 'Post Category',
'url' => ['post-category/view', 'id' => 10],
'template' => "<li><b>{link}</b></li>\n", // template for this link only
],
So by adding something like this in your main layout file
$this->params['breadcrumbs'][] = [
'label' => 'Your Label',
'url' => ['controller/action'],
'template' => "<li style="float: right;">{link}</li>\n"
];
you will get a link at the right side. This link will come with a / prefixed, but you can bind it to a .class instead and configure it the way you want.
'template' => "<li class=\"yourClass\">{link}</li>\n"

how to insert cakephp image code into form?

I got a insert form. I would like to insert the below code into the form.
<?php
echo $html->image("slide_03.jpg", array(
"alt" => "Event Banner",
'class' => '',
));
?>
In my view layer I want the the image to be display. However, the image did not display and got this code in my view layer instead
image("slide_03.jpg", array( "alt" => "Event Banner", 'class' => '', )); ?>
In CakePHP 2.x you have to use $this->Html->image(). $html->image() was used in previous CakePHP versions and no longer works in CakePHP 2.x.
In general (not just for images) you can set option after or before if you want additional html elements near of your input element.
As you see from sample below you can combine input options with $this->Html->image().
<?php
echo $this->Form->input('field', array(
'before' => $this->Html->image(),
'after' => $this->Html->image(),
));
More details about options on documentation.

Categories