2amigos widgets format "dd-mm-yyyy" - date

I use datepicker to create an input in a form.
I use like this :
<?=
$form->field($model, 'DATE_BIRTH')->widget(
DatePicker::className(), [
// inline too, not bad
'inline' => true,
// modify template for custom rendering
'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
'clientOptions' => [
'autoclose' => true,
'format' => 'dd-mm-yyyy',
'todayBtn' => true
]
]);
?>
But when insert into database, it gives me error coz incorrect value.
Mysql have a format "yyyy-mm-dd".
How can I still get my date format in datepicker, coz in Indonesia, we know 'dd-mm-yyyy' .
Thanks

You can use beforeSave method in your model file to change the date format before saving into the database like below.
public function beforeSave($insert) {
//if you want only on insert you can use this if condition else remove the if condition.
if($insert){
$this->DATE_BIRTH = Yii::$app->formatter->asDatetime(strtotime($this->DATE_BIRTH), "php:Y-m-d");
}
return parent::beforeSave($insert);
}

Related

How to make the value (to be read) of form field widget in yii2 different from what users see in the input text box

I am modifying a system already developed in yii2 framework.
There is a functionality where a user views a list of customers and against each record there is a button for editing. When clicked, a form is displayed with values automatically filled in for the customer against whom the 'edit' button was clicked.
Now, the is a field for address which is filled with the database record id of the address instead of the name of the location. The id is a number, and the field was validated to check that the value is a number when the 'Update' button is clicked.
Problem:
Instead of filling the field with the number (i.e the id) I am requested by users to fill that field with the name of the location which is not a number and therefore will violate the validation rule.
I want to be able to make the value of the input field (i.e the value attribute) to be the id -a number so that it does not violate the set rule - but display the name of the location in the text input so that the users have a readable 'value' to look at.
How can I achieve this?
CODE
There was this:
<div class="col-sm-6">
<?= $form->field($model, 'village')->widget(\kartik\widgets\Select2::classname(), [
'options' => ['value' =>$model->village],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
</div>
The value in $model->village is the id of the village. This is what users do not want.
I tried doing this:
<div class="col-sm-6">
<?php
$villageName = Yii::$app->db->createCommand('SELECT village_name FROM v_village WHERE id='.$model->village)->queryOne();
$streetName = Yii::$app->db->createCommand('SELECT street FROM v_street WHERE street_id='.$model->street)->queryOne();
?>
<?= $form->field($model, 'village')->widget(\kartik\widgets\Select2::classname(), [
'options' => ['value' => $villageName['village'], 'id' => $model->village],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
</div>
But that violates the validation rule and the error I get says 'Village must be a number'
You have to set data array of your select.
<div class="col-sm-6">
<?php
$villageName = Yii::$app->db->createCommand('SELECT village_name FROM v_village WHERE id='.$model->village)->queryOne();
$streetName = Yii::$app->db->createCommand('SELECT street FROM v_street WHERE street_id='.$model->street)->queryOne();
?>
<?= $form->field($model, 'village')->widget(\kartik\widgets\Select2::classname(), [
'data' => [$model->village => $villageName['village']],
'pluginOptions' => [
'allowClear' => true //are you sure you want to be able to clear the option? If you save it empty the query above will break
],
]); ?>
</div>
I'm not sure what is your purpose, but if you have only this value you can just show it like text.
If you want to be able to change the village you code must be like this:
<?= $form->field($model, 'village')->widget(\kartik\widgets\Select2::classname(), [
'data' => ArrayHelper::map(Village::find()->all(), 'id', 'village_name'),
'pluginOptions' => [
'allowClear' => true //are you sure you want to be able to clear the option? If you save it empty the query above will break
],
]); ?>
Also you can remove the query and set data like this:
'data' => ArrayHelper::map(Village::find()->where(['id' => $model->village])->all(), 'id', 'village_name')

yii2 2amigos datapicker. Can't save to model

I have start_date and end_date columns in my model.
In the form I put:
<?= $form->field($model, 'start_date')->widget(
DatePicker::className(), [
'inline' => false,
'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
'clientOptions' => [
'autoclose' => true,
'format' => 'dd-M-yyyy'
]
]);?>
It's saving null parameters in start_date and end_date columns.
P.S. Bth, how to hide a calendar? I want to make it hidden, and when I click on input it will be automatically shown to choose date.
Try this
<?= $form->field($model, 'start_date')->widget(
DatePicker::className(), [
'inline' => false,
// remove the template
'clientOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd' //change the date format
]
]);?>
The calender remained open because of the template. Use yyyy-mm-dd date format to save the date

How to implement jquery date time picker with zend framework 2 using zf FORM

Thanks in advance,
Want to implement jquery date time picker with zend framework 2 using ZF FORM, with validation of form date selection depending on to date.
Please suggest the best practice.
You can proceed as follows
In your form you add a date field:
$this->add([
'name' => 'birthDate',
'type' => 'Zend\Form\Element\Date',
'attributes' => [
'id' => 'birthDate',
'class' => 'required datepicker-date',
'type' => 'text'
],
'options' => [
'label' => 'Birth date'
]
]);
notice the 'type' => 'text'.
Then in the view you print the form element as you please and add the following javascript snippet:
$(document).ready(function() {
$( ".datepicker-date" ).datepicker({
dateFormat: "dd-mm-yy",
changeMonth: true,
changeYear: true
});
});
Adjust the options according to your needs.
You can do it by using bootstrap-datepicker
So firstly you want to insert following CSS and JS files.Those are
1.bootstrap.css
2.bootstrap-datetimepicker.min.css
3.jquery.min.js
4.bootstrap-datepicker.min.js
<script type="text/javascript">
$(document).ready(function() {
$('#yourTextFieldIDName').datepicker({
format: "yyyy-mm-dd",
viewMode: "day",
minViewMode: 'day'
});
</script>

cakephp 3: change class input error

I build my form template according the documentation. It seemed everything was fine until I get fields errors. Now I have two problems:
How can I change the class name of the forms fields when they get error?
Solution:
$this->loadHelper('Form', [
'templates' => 'your_template_file',
'errorClass' => 'your-class',
]);
How can I set escape => false in the error-message from cakephp, when the field get error? Because I have icon within that div, such as
<div class="error-message"><i class="fa fa-times"></i> My error</div>
Well, I got part of th solution. To escape HTML I could put $this->Form->error('field', null, ['escape' => false]); in all fields, but it´s a hard manually task. I´d like to keep escape with default of all fields errors. I could edit the FormHelper.php class. However, I think that is not good idea.
My form template is:
'formStart' => '<form {{attrs}} class="form-horizontal" novalidate>',
'inputContainer' => '{{content}}',
'input' => '<input type="{{type}}" name="{{name}}" {{attrs}} class="form-control"/>',
'checkbox' => '<input type="checkbox" value="{{value}}" name="{{name}}" {{attrs}}/>',
'textareaContainerError' => '{{content}}',
'textarea' => '<textarea name="{{name}}" {{attrs}} class="form-control"></textarea>',
'select' => '<select name="{{name}}" {{attrs}} class="form-control">{{content}}</select>',
'button' => '<button {{attrs}} class="btn btn-primary">{{text}}</button>',
'nestingLabel' => '{{input}}',
'formGroup' => '{{input}}',
to the second part of the question: you can extend FormHelper like in code below, so that escape will be set to false by default
// extended FormHelper, this goes in src/View/Helper
namespace App\View\Helper;
use Cake\View\Helper;
class MyFormHelper extends Helper\FormHelper
{
public function error($field, $text = null, array $options = [])
{
if (!isset($options['escape'])) {
$options['escape'] = false;
}
return parent::error($field, $text, $options);
}
}
next create alias for this helper in AppController.php
public $helpers = [
'Form' => ['className' => 'MyForm']
];
this also allows you to add more customization of your own and at any time, you can go back to default implementation of FormHelper, just remove that alias from AppController.php.
For those who wants an 'easy solution' to escape error message on some fields, you cant simply set escape options to false :
<?= $this->Form->input('email', [
"label" => "Email",
"error" => [
"escape" => false
]
]) ?>

Hide label for input field

I am trying to hide the label for a specific field in _form.php without success.
I have tried couple of variation like, but none is working:
<?= $form->field($model, 'sample_text')->textArea('label'=>false) ?>
and alternate code:
<?= $form->field($model, 'sample_text')->textArea('label'=>'') ?>
What is the right approach to hide a label?
Ok, I found the solution.
<?= $form->field($model, 'sample_text')->textArea()->label(false) ?>
Or you can modify template value for particular field and remove {label} part from it. I.e.:
<p><?= $form->field($page, 'image', [
'template' => '<div class=\"\">{input}</div><div class=\"\">{error}</div>'
])->fileInput() ?></p>
At time of writing after digging into the core code, I have found this to be the best solution to hide the label and prevent rendering the full field template with errors etc. for hiddenInput.
<?=
$form->field($model, 'address_uuid', [
'template' => '{input}',
'options' => ['tag' => false]
])->hiddenInput([
'readonly' => true,
])->label(false)
?>
<?= $form->field($model, 'password', [
'inputOptions'=>[
'class'=>'form-control',
'placeholder'=>'Password'
]
])->passwordInput()->label(false); ?>
<?= $sffForm->field($sffModel, 'url_keywords', ['enableLabel' => false])->textInput(['placeholder' => 'URL / keywords']) ?>
You can disable label, while creating form field class
$form->field($model, 'email', [
'inputOptions' => [
'enableLabel' => false,
]
])
The best way to hide the label in form input field, is to pass empty value to array on 'attributeLabels()' function in the model.
i.e you have input filed name 'client_name', so on the 'attributeLabels()' function's return array pass the empty string as array value
public function attributeLabels()
{
return [
'id' => 'ID',
'gender' => 'Gender',
'client_name' => '',
.
.
.
]
}