Hi Guys.
What the best & simple way to save a currency and date value in MySQL?
On my form I have 2 inputs (date and monetaty/currency). I need fill it using Brazilian formmat and save it using DB format.
EDITED
My problem til now is the validate, when fill the form with 1.587,69 it show me an error Value must be a number.
END EDITED
Ex.:
Date (in form): 21/03/2016, Value (in form): 1.587,69
Date (in DB): 2016-03-21, Value (in DB): 1587.69
My config is:
web.php
...
'language' => 'pt-br',
'components' => [
'formatter' => [
'class' => 'yii\i18n\Formatter',
'dateFormat' => 'php:d/m/Y',
'datetimeFormat' => 'php:d/m/Y H:i:s',
'timeFormat' => 'php:H:i:s',
'decimalSeparator' => ',',
'thousandSeparator' => '.',
'currencyCode' => 'R$',
],
/*
* Antes de inserir os dados no banco, formatá-los para se tornarem compatíveis com determinados tipos de campos.
*/
'formatterDB' => [
'class' => 'yii\i18n\Formatter',
'dateFormat' => 'php:Y-m-d',
'datetimeFormat' => 'php:Y-m-d H:i:s',
'timeFormat' => 'php:H:i:s',
'decimalSeparator' => '.',
],
...
index.php
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'valor:currency',
'data:date',
// 'created_at:datetime',
// 'updated_at:datetime',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
view.php
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'valor:currency',
'data:datetime',
'created_at:datetime',
'updated_at:datetime',
],
]) ?>