Cakephp 3.0 - using Chronos and datetimepicker, how to input a date without time? - cakephp-3.0

In my Cakephp3.0 app, I use Chronos and datetimepicker.
In my Entity file, I have:
#property \Cake\I18n\Time $purchase_date
In my Table, I put this:
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
Then for the Validator:
$validator
->date('purchase_date', ['mdy'])
->allowEmpty('purchase_date');
In edit.ctp, I put this:
<?= $this->Form->input('purchase_date', ['empty' => true]) ?>
When I click on the glyphicon-calendar button, I keep getting a value including the time, like : "10/17/2017, 01:58 PM".
What should I do to input only Date in this format: "10/17/2017" ?
Thanks in advance.

instead of:
<?= $this->Form->input('purchase_date', ['empty' => true]) ?>
This code worked for me:
<div class='input-group date' id='datetimepicker6'><?= $this->Form->input('purchase_date', ['format' => 'mdy', 'empty' => true]) ?></div>

Related

Yii2 how to format <sup> tags in forms and GridView?

I have an attributeLabels for an attribute that has a <sup> tag:
public function attributeLabels() {
return [
'density' => Yii::t('yii', 'density (kg/dm<sup>3</sup>)'),
];
}
In the view it looks appropriate, but in the form and in the GridView as label it's simply like (kg/dm<sup>3</sup>)
I have tried to add labelOptions with many different format values to it, but no luck.
<?= $form->field($model, 'density', ['labelOptions' => ['format' => '(html/text/raw etc.)']])->textInput() ?>
Is it possible to make it look like a real <sup> (kg/dm3) text in the form, and if yes, can you please tell me how? Thank you.
Call it like that:
<?= $model->getAttributeLabel('density'); ?>
<?= $form->field($model, 'density')->textinput()->label(false) ?>
and after that format your template
Edit (better way):
<?= $form->field($model, 'density', ['labelOptions' => ['label' => $model->getAttributeLabel('density')]])->textinput() ?>

How to user MaksedInput definitions property in Yii2

I need to understand how to use MaskedInput definitions property because i have "phone" field and it have always contain + at start and after it it have contain 10-15 decimal characters. How to implement it?
I've used something like this
<?= $form->field($model, 'phone')->widget(\yii\widgets\MaskedInput::className(), [
'mask' => '99-99999999-9',
]) ?>
See http://www.yiiframework.com/doc-2.0/yii-widgets-maskedinput.html and https://github.com/RobinHerbots/Inputmask
You could try
<?= $form->field($model, 'phone')->widget(\yii\widgets\MaskedInput::className(), [
'alias' => 'phone',
]) ?>

Yii2 Add a Form field not in model

As we know,
<?= $form->field($model, 'name_field')->textInput() ?>
Adds a text field connected to 'name_field' in the model/table.
I want to add a field NOT in the model/table, and then run some JS when it loses focus to calculate the other fields.
How firstly do you add a free text field not connected to the model ?
Second, does anyone have any examples of adding JS/Jquery to the _form.php ?
The Html class contains the functions for generation of fields. In fact, your code above ends up calling Html::textInput(). To add a field
<?= Html::textInput("name", $value) ?>
To add javascript to a view just use registerJs():
$this->registerJs("alert('true');");
You can have the field rendered the same way as the ActiveField, with a label and classes. For example, let’s add a Cc field to a Mail form.
First display the To: field (in the model):
<?= $form->field($model, 'to')->textInput() ?>
Let’s add the Cc field (not in the model):
<?= Html::beginTag('div', ['class' => 'form-group field-mail-cc']) ?>
<?= Html::label('Cc:', 'mail-cc', ['class' => 'control-label']) ?>
<?= Html::textInput('Mail[cc]', '', ['id' => 'mail-cc', 'class' => 'form-control']) ?>
<?= Html::endTag('div') ?>
The class and id names mail-cc and field-mail-cc follow the ActiveForm naming pattern. The input name Mail[cc] adds your field to the ActiveForm group, so you can easily retrieve it with the usual
$form = Yii::$app->request->post('Mail');

How to use datepicker in yii2 basic?

I want to use yii2 datepicker but I'm having trouble to implement this. It does not show the date picker and I don't know what is missing in my code. I'm still new in this yii
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
?>
<div class="row">
<div class="col-lg-6">
<div class="myproj-index">
<?php $form = ActiveForm::begin(['layout' => 'horizontal']); ?>
<? //$form->field($model, 'periodfrom')
echo DatePicker::widget([
'model' => $model,
'attribute' => 'periodfrom',
'language' => 'en',
'dateFormat' => 'yyyy-MM-dd',
]);
?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
Edit
I downloaded this file here jui
I found out that in my file I have no jui folder under yiisoft folder. My question in appasset how can I declare this files ?
Answer
I fixed it, I downloaded the jquery-ui files then added them to the bower folder in yii2.
First install this extension through composer.
run: php composer.phar require --prefer-dist yiisoft/yii2-jui "*" in your project directory.
update composer.
In your view file use use yii\jui\DatePicker;.
You probably did not install Datepicker in the vendor package.
Thanks.
Do you have use .....\DatePicker; ?
Also are you including the jui asset and everything, take a look at the source, do you have jui and all the JS included?
Edit:
Have you done a "composer update" lately?
Use this code if you are using kartik datetime picker
<?= $form->field($model, 'date')->widget(DatePicker::classname(),['options' => ['placeholder' => 'Enter event time ...'],'pluginOptions' => [....]]) ?>

DropDownList yii 2.0 example

I am using yii 2.0 Framework.
How i can make options from my database.
I found this, but it's yii 1.1:
<?php echo CHtml::dropDownList('listname', $select,
array('M' => 'Male', 'F' => 'Female'));
I want to pass it to form:
<?php $form->dropDownList() ?>
How i can fill my dropdownlist from my database table?
If you use ActiveForm widget use this:
<?php
$items = ArrayHelper::map(Model::find()->all(), 'id', 'name');
$form->field($model, 'attribute')->dropDownList($items)
?>
Use yii\helpers\Html it contains Html::dropDownList().
echo Html::dropDownList('listname', $select, ['M'=>'Male', 'F'=>'Female']);
Check Yii Framework 2.0 API
Controller
public function actionSomething() {
$sexes = ['M'=>'Male', 'F'=>'Female'];
$this->render('yourView', ['sexes'=>$sexes]);
}
View
<?php
::
echo Html::dropDownList('listname', $select, $sexes);
::
?>
Yes if you use ActiveForm widget , u dont have to change anything in the controller , in views, in the form, add this where u want the dropdown
use yii\helpers\ArrayHelper;
<?php
$city = \app\models\City::find()->all();
$listData=ArrayHelper::map($city,'cityId','cityName');
?>
<?= $form->field($model, 'cityId')->dropDownList($listData,['prompt'=>'Choose...']) ?>
<?= $form->field($model, 'name_of_field')->dropdownList(['1' => 'aaa', '2' => 'bbb'], ['prompt' => '---Select Data---']) ?>