adrian Posted May 18, 2014 Share Posted May 18, 2014 Hi everyone, I am trying to add a datetime field to a process module and am having a weird problem here. This is my code: $f = $this->modules->get("InputfieldDatetime"); $f->name = 'changes_since'; $f->label = 'Changes since'; $f->datepicker = 3; $f->attr('data-dateformat', "yy-mm-dd"); $f->attr('data-timeformat', "h:mm tt"); $f->attr('value', "2014-03-13 1:45 pm"); $f->attr('class', 'FieldtypeDatetime'); $form->add($f); When this field is displayed the value is just: 2014-03-13. For some reason the time is missing. However, when I use the datetime picker a make a selection, the field is then populated with the full date and time. Not that it helps, but if I change the field type to InputfieldText, the full date time is shown when the input renders. Also, something else weird I noticed is that when I submit the form, $input->post returns the correct date and time, but $form->processInput($this->input->post); echo date("Y-m-d g:m", $form->get('changes_since')->value); returns: "2014-03-13 12:03", no matter what time I select. I think this is to do with the am/pm in the time format. If I compare the generated classes, ids, etc for this input field vs one in the admin when editing a page, they are the same so I am currently at a loss. Thanks for any help! Link to comment Share on other sites More sharing options...
adrian Posted May 18, 2014 Author Share Posted May 18, 2014 Some progress. Adding: $f->attr('dateInputFormat', "Y-m-d"); $f->attr('timeInputFormat', "g:i a"); populates the time as well, but it is always 12:00 am, ignoring what is set in the value. I'll keep experimenting and report back. EDIT: Ok, I got it - you need to set the value to the unix timestamp Final code: $f = $this->modules->get("InputfieldDatetime"); $f->name = 'changes_since'; $f->label = 'Changes since'; $f->datepicker = 3; $f->attr('data-dateformat', "yy-mm-dd"); $f->attr('data-timeformat', "hh:mm:ss"); $f->attr('dateInputFormat', "Y-m-d"); $f->attr('timeInputFormat', "H:i:s"); if($form->get('changes_since')->value){ $f->attr('value', $form->get('changes_since')->value); $f->attr('data-ts', $form->get('changes_since')->value); } $f->attr('class', 'FieldtypeDatetime'); $form->add($f); 3 Link to comment Share on other sites More sharing options...
Soma Posted May 18, 2014 Share Posted May 18, 2014 Just only need $f->dateInputFormat = "Y-m-d"; $f->timeInputFormat = "H:i:s"; the attr(key,value) is for html attributes and data-dateformat will get written by the inputfield automaticly. 2 Link to comment Share on other sites More sharing options...
Soma Posted May 18, 2014 Share Posted May 18, 2014 Oh and field name would be a attr $f->attr("name", "myname"); Something like this works perfectly. $f = $modules->InputfieldDatetime; $f->label = "MyDate"; $f->attr("name+id", "mydate"); $f->dateInputFormat = "Y-m-d"; $f->timeInputFormat = "H:i:s"; $f->attr("value", time()); echo $f->render(); 2 Link to comment Share on other sites More sharing options...
adrian Posted May 18, 2014 Author Share Posted May 18, 2014 (edited) Thanks Soma, That is certainly cleaner and simpler Only thing I needed to add back in was: $f->datepicker = 3; And in case someone is wondering about the 3: https://github.com/ryancramerdesign/ProcessWire/blob/03387f8283d518e9cc405eff8f05cd6a5bf77c4c/wire/modules/Inputfield/InputfieldDatetime/InputfieldDatetime.module#L24 const datepickerNo = 0; // no datepicker const datepickerClick = 1; // datepicker on click const datepickerInline = 2; // inline datepicker, always visible (no timepicker support) const datepickerFocus = 3; // datepicker on field focus I ended up in one of those situations where I was trying to add attributes to match the generated source of a working datetime field, rather than figuring out exactly what PW needed to set them itself. Can I blame it on the fact that it was 1 am Edited May 18, 2014 by adrian Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now