Defaulting a datetime
Defaulting a datetime
TomBajzek
Posts: 164Questions: 37Answers: 1
I would like to default a datetime in a form to the current time, without allowing the user to edit it when creating a new record. The current time appears to come up as the default without my doing setting the def, but the input field is editable by the user. I would like to be able to capture the current time without allowing the user to change it, or requiring the user to confirm the current time
Is this possible?
The field definition for this field is:
fields: [
{
label: "tickettime:",
name: "tickettime",
type: "datetime",
// def: "return new Date();",
format: "YYYY-MM-DD HH:mm:ss"
},
]
Thanks,
Tom
This discussion has been closed.
Replies
Is this useful?
https://datatables.net/forums/discussion/40475/set-datetime-cell-to-current-time-when-a-row-is-edited#latest
So it is either tangerine's solution if you don't want to display the datetime at all. Or if you want to display it but not make it editable by the user you can either exclude it from Editor by simply not listing it there. Or you can list it in Editor and disable it like this:
https://editor.datatables.net/reference/api/disable()
That sets the
def
parameter to be a string! You want to have it be a function that returns aDate
object when executed. Like in this example.Allan
By combining Allan's answer with that of RF1234, I have what I need for setting the creation date of a record. I appreciate the help with this.
I then moved on to the next issue, which is setting an 'updated' field to record the time of any updates to the record. I'm aware of the MySQL suggestion made by Tangerine, and have defined the 'updated' field to 'ON UPDATE CURRENT_TIMESTAMP' but this does not take effect for some reason. (It does work when I update a record in my database utility for MySQL.) If I select a new datetime from the picker, it is correctly saved, but I'd like that to happen automatically without editing by the user.
I'm thinking that the Editor is supplying a value for the 'updated' field that is overriding the 'ON UPDATE CURRENT_TIMESTAMP' in the field definition, so I've tried setting the default to '' or to zero, or to function () { return new Date(); } with the field disabled and not, but none of those result in saving the time of the update.
My current field definition for 'updated' in the JS is as follows:
I know I'm missing something here, but I don't know what that is. Any insight would be appreciated.
Thanks,
Tom
I would suggest using the
Field->setValue()
method in the PHP libraries for this kind of thing. Otherwise it's trivial for someone to tamper with the latest updated value from the client-side! Having said that, if you really do want to do it from the client-side, useinitEdit
in combination withfield().val()
.Allan
if you specify the field in Editor it tries to insert or update it through MySql. Clauses in MySql like CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP are not taking effect then. They are only being used if nothing is specified for that field in the SQL. The first clause inserts CURRENT_TIMESTAMP upon insert. The latter clause updates the field with the CURRENT_TIMESTAMP upon any update of that row - provided the field is NOT specified in the SQL-Statement.
So if you have the field in Editor because you want to display it to the user you would need to do something like this on the server side in PHP
This returns the date in the format you wish to display to your user. In my case it is either the German or the British way. For the US you would need to change it to 'm/d/Y' I guess. Writing to the database the setFormatter formats the datetime value into the format MySql expects. That works from any valid date format. So the setFormatter can always be the same no matter what language.
An alternative getFormatter would be this one which only displays the date not the time:
For more details look at this please and search for MySQL:
http://php.net/manual/en/function.date.php
Example #4 date() Formatting
```
<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
<?php > ``` ?>$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
$today = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (the MySQL DATETIME format)
I found your example to be very helpful. I learned a great deal in adapting your solution to my actual needs, and by taking your suggestion to set the values on the server. It turns out that the MySQL that I have does not yet support both DEFAULT and ON UPDATE in the same record, so I had to set one of these programmatically, and had to keep the Editor from sending anything to the server. I also initially misinterpreted the effect of disabling a field.
This aspect of my project seems to be functioning as desired now.
Thanks much,
Tom