Is there any way to validate time attribute In MYSQL column contain time datatype. SO i can just validate it through YII.
So is it possible? LIKE
"[['from_date', 'to_date'], 'date']"?
Try this in model rules
['attr', 'date', 'format'=>'H:i']
For more on this please refer
Use like:
[['from_date', 'to_date'], 'date', 'dateFormat' => 'H:i:s']
You can use format attribute for time validation. It will handle both date and time validation.
['attribute_name', 'date', 'format' => 'yyyy-M-d H:m:s'],
OR
[['attribute_name1', 'attribute_name2'], 'date', 'format' => 'H:i:s']
Related
I'm trying to have a time input in TYPO3 9 LTS working together with MySQL 5.7.24.
In the ext_tables.sql the field gets defined like this:
some_field time default NULL
In the TCA the field gets defined like this:
'some_field' => [
'exclude' => 1,
'label' => 'Some field',
'config' => [
'type' => 'input',
'dbType' => 'time',
'eval' => 'time',
],
],
When saving the record in the backend without a time input (which should be possible) I'm getting the error:
These fields of record 1 in table "some_table" have not been saved correctly: some_field! The values might have changed due to type casting of the database.
When looking at the database record the some_field field gets the value 00:00:00 (although the db default is NULL).
When selecting a time the record can be saved and opened without error.
Is this a bug in TYPO3 or how could I fix this behavior?
The bug can be solved by having the following eval:
'eval' => 'time,null',
That means you have given the wrong type for the value on your ext_tables.sql. Additionally, TYPO3 v9 has renderTypes.
Try something like that:
ext_tables.sql
begin int(11) DEFAULT '0' NOT NULL
TCA
'begin' => [
'exclude' => true,
'label' => 'LLL:EXT:your_ext/Resources/Private/Language/locallang_db.xlf:tx_yourext_domain_model_modelname.begin',
'config' => [
'type' => 'input',
'renderType' => 'inputDateTime',
'size' => 10,
'eval' => 'datetime',
'default' => time()
],
],
Additional information!
If you want to display the time in FrontEnd you could use something like that
<f:format.date>{dateObject.begin}</f:format.date>
If you want to modify how it looks, you can use the format attribute as well:
<f:format.date format="%d. %B %Y">{dateObject.begin}</f:format.date>
More about that here: TYPO3 Date format
In a user table I have a created_at column. In a User AR model that becomes a property, and in \yii\grid\GridView I split that property onto 'columns' => ['created_at:date', 'created_at:time'],
Question: How do I habilitate a search field for those respective given columns, I mean they aren't actual db columns, therefore not model properties. so I can't do
$query->andFilterWhere([
'crated_at_date' => $this->crated_at_date,
'created_at_time' => $this->created_at_time,
]);
I know the search field is related to \yii\grid\Column::filterOptions but more specifically I don't know how to specify the andFilterWhere() clause within the search model.
In searchModel() you can try this way:
$query->andFilterWhere([
'like',
'FROM_UNIXTIME(created_at, "%Y-%m-%d")',
$this->crated_at_date
])
->andFilterWhere([
'like',
'FROM_UNIXTIME(created_at, "%h:%i:%s %p")',
$this->created_at_time
])
Well, It's not tested but I guess it will do the trick.
FROM_UNIXTIME()
You can filter more or less just by converting input datatime to time:
$query->andFilterWhere([
'>=',
'crated_at_date_time',
strtotime($this->crated_at_date)
])
I have an array like this:
['valid_from' => '2016-02-01']
In my model I have the following validation rule
$validator->date('valid_from')->allowEmpty('valid_from');
When I try to patch the entity with the array I get this:
'valid_from' => object(Cake\I18n\FrozenDate) {
'time' => '2168-12-02T00:00:00+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
}
The column in MySQL is of the type date. (I don't want to use datetime as I don't need the time for calculations)
What am I doing wrong?
Think your issue is that your locale is misconfigured try one of these methods.
The default locale can be set in your config/bootstrap.php folder by using the following line:
ini_set('intl.default_locale', 'fr_FR');
Changing the Locale at Runtime
use Cake\I18n\I18n;
I18n::locale('de_DE');
I have a Year field in a form and I am using FormHelper.
echo $this->Form->input('year', [
'type' => 'year',
'minYear' => date('Y')-10,
'maxYear' => date('Y')
]);
The table file validator looks like:
->add('year', 'valid', ['rule' => 'numeric'])
->allowEmpty('year')
I have a very similar input in another app that seems to work fine. I set the MySql column to int(5) to match what I had working elsewhere.
Checking debugkit it shows the "year" input as an array while the other inputs are strings. If I remove the validation rule it throws an illegal array to string conversion, so I assume this is where the error is.
Any help is greatly appreciated.
I have just tested with your above code and it is working fine for me. Try to delete the cache and check it once more.
Creates a select element populated with the years from minYear to maxYear. Additionally, HTML attributes may be supplied in $options. If $options['empty'] is false, the select will not include an empty option:
empty - If true, the empty select option is shown. If a string, that
string is displayed as the empty element.
orderYear - Ordering of
year values in select options. Possible values ‘asc’, ‘desc’. Default
‘desc’ value The selected value of the input.
maxYear The max year to
appear in the select element.
minYear The min year to appear in the
select element.
Try this one:
<?php
echo $this->Form->year('exp_date', [
'minYear' => date('Y')-10,
'maxYear' => date('Y'),
'id' => 'cc-year',
'class' => 'form-control',
'empty' => false,
'orderYear' => 'asc'
]);
?>
Official Documentation: CookBook - Creating Year Inputs
I have a table named Play and I'm showing details of each record in Yii2 detail view widget. I have an attribute in that table recurring which is of type tinyint, it can be 0 or 1. But I don't want to view it as a number, instead i want to display yes or no based on the value (0 or 1).
I'm trying to change that with a function in detailview widget but I'm getting an error: Object of class Closure could not be converted to string
My detail view code:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'name',
'max_people_count',
'type',
[
'attribute' => 'recurring',
'format'=>'raw',
'value'=> function ($model) {
if($model->recurring == 1)
{
return 'yes';
}
else {
return 'no';
}
},
],
'day',
'time',
...
Any help would be appreciated !
Unlike GridView which processes a set of models, DetailView processes just one. So there is no need for using closure since $model is the only one model for display and available in view as variable.
You can definitely use solution suggested by rkm, but there is more simple option.
By the way you can simplify condition a bit since the allowed values are only 0 and 1:
'value' => $model->recurring ? 'yes' : 'no'
If you only want to display value as boolean, you can add formatter suffix with colon:
'recurring:boolean',
'format' => 'raw' is redundant here because it's just text without html.
If you want add more options, you can use this:
[
'attribute' => 'recurring',
'format' => 'boolean',
// Other options
],
Using formatter is more flexible approach because these labels will be generated depending on application language set in config.
Official documentation:
DetailView $attributes property
Formatter class
Formatter asBoolean() method
See also this question, it's quite similar to yours.
Try
'value' => $model->recurring == 1 ? 'yes' : 'no'