Jump to content

Only "year" in Datetime field?


Go to solution Solved by SiNNuT,

Recommended Posts

Hi everybody!

As I may have already wrote, I really like processwire. After a short time of not knowing whether this thing is really able to get everything going (= bigger more complex sites), this slowly kicks of. Coming from MODx processwire really is a treat - and a lot faster!

So now: I just tried adding a new field which I named "book_exhibit_year" used the type "Datetime" and tried setting the "Date/Time Output Format Code" to "Y" (year with four digits) and also did the same with "Date Input Format Code".

My customer wants to add his own "books" as child pages and requires them to have them sorted by exhibition year. To make it as easy as possible I want him/her to be ONLY to enter years - not whole datetimes... I also thought about just using a simple "text", but then I maybe not able to sort all child pages based on the year (?).

Long story short: using only "Y" (see above) will not save anything. If I enter "2014" it will always reset to "1970". I suppose the datetime expects "good" (whole) dates... I'm out of ideas right now. :undecided:

Maybe using a text field and then converting the value to a datetime object? ???

Thanks!

Link to post
Share on other sites
  • Solution

I'm not sure but i think your out of luck with a Datetime field. As far as i know this will always require some form of date input, not just a year (which of course isn't a date). Maybe someone who has done more with Datetime fields can verify or prove me wrong.

What i would do however is simply make a field of Type = Text. When you have saved the field you can go to the Input tab and use a pattern of your liking.

For example if you wanted to only allow years in the range 1900-2100 you could use a pattern like this:

^(19[0-9]{2}|20[0-9]{2}|2100)$

When a customer tries to enter anything that falls out of this range and hits save there even is a nice client side inline error message (in capable browsers) or else there will be server-side validation. Of course, you should make a regex that suits your needs.

You will be able to sort pages according to year via such a setup, in the page tree as well as on the front-end using PW selectors.

EDIT

You can also use the Integer type, where you can optionally set (on the Input tab) the Numeric Input Type to Number (HTML5) and set a Minimum and Maximum Value. This gives a nice input to the customer and with the same nice html5 validations.

The Integer type might be preferable, because of the nice input but also database efficiency.

  • Like 7
Link to post
Share on other sites

The example of @SiNNuT works with 20th / 21st centurys, Simpler and shorter (and 20th / 21st centurys):
 
 

(19|20)\d\d

Only 21st century:
 

(20)\d\d
  • Like 2
Link to post
Share on other sites

So which option did you take? Text as well as Integer both are fine, but Integer might make more sense.

If you chose the Text solution and use the regex of blad i would still add assertions for start and end of string to that, because if you literally copy/pasted that, customers would be able to enter something like '09201409' into the field. Of course this might be obvious to you.

^(19|20)\d\d$
  • Like 3
Link to post
Share on other sites

So which option did you take? Text as well as Integer both are fine, but Integer might make more sense.

If you chose the Text solution and use the regex of blad i would still add assertions for start and end of string to that, because if you literally copy/pasted that, customers would be able to enter something like '09201409' into the field. Of course this might be obvious to you.

^(19|20)\d\d$

Completely right :) Thanks

Link to post
Share on other sites
  • 2 years later...

Another solution would be to hook to Inputfield::processInput and change the Input-Data to a full datestring that can be converted from php´s strtotime function.
(for example YYYY-mm-dd). (I guess the Datepicker on focus option should be disabled)

Here is what I am using in my custom module for DateTime Fields named "date" and "date_to":

in init function

$this->addHookAfter('Inputfield::processInput', $this, 'processInputDate');

the function

public function processInputDate($event) {
    $inputfield = $event->object;
    $input = $event->arguments[0];
    if($inputfield->name == "date" || $inputfield->name == "date_to") {
        if(isset($input[$inputfield->name])) {
            $value = $inputfield->value;
            if($value && ctype_digit($value) && strlen($value) == 4) {
                // seems to be only a year, so add 01-01 to it.
                $value = $this->sanitizer->int($value);
                $newValue = '01-01-'.$value;
                $inputfield->setAttribute('value',$newValue);
                $this->message("Converted ".$value." to a full date string: ".$newValue.")");
            }
        }
    }
}

However, I think this makes only sense in certain cases. I need the possibilty to enter also a full date. In my case its okay that the 01. January is some kind of a "special day", where all the items are going wich have only a "year" but no month/day.

 

  • Like 1
Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By sambadave
      Hi there
      Short version of question
      Let's say I have a page in the admin that contains a field... Is it possible to output the content from that field on another page in the admin? Almost like a reference.
      Longer version of question (with example)
      A house builder with multiple (60+) developments. They want to be able to create notices/messages that can be added to one or many developments. Handy for things like regional covid lockdowns or temporary office closures due to bad weather.
      My approach for the admin editing options:
      Add each message to each development
      Pros: You edit the message on the development page in context
      Cons: Very time consuming and repetitive if the same message needs to be applied to 60+ developments
        Control all the messages from one admin page and say which development each message should be applied to
      Pros: Easier to add/remove messages to more than one development at a time. Control all messages from one place.
      Cons: Content is not added on development page, which is where typical admin users may expect to find it I went for option 2 due to flexibility, and created a page within the admin for global development notices. This contains a repeater with:
      Field for message to display Checkbox list of all developments. The user can select which ones to apply each message to It's working really well but the only thing is that if the user goes to a specific development in the admin, the relevant messages aren't displayed in context (as they aren't edited on that page and instead on the global development notices page)... which may cause confusion when a new staff member / content admin tries to edit the text but there is no field when they go to the development admin page where they expect to see it...
      Solution???
      I don't require the message(s) to also be editable on the development page, but I wondered if there was a nice way to show it/them somehow. I feel like I am missing something really simple as I'm sure ProcessWire will have a nice way of achieving this, or maybe there are field settings that allow this kind of thing to happen?
      Any ideas on approaches or similar experiences would be much appreciated, even if it is just a much simpler example with the content from one field being shown on another admin page to get the ball rolling.
      Thanks in advance for any advice :)
    • By rjgamer
      Hi guys,
      the field "redirect_last" of type DateTime got not updated. The update on the field "redirect_counter" works and got saved.
      Does anybody know what I did wrong in my code?
      if ($input->urlSegment(1) === 'redirect') { $page->of(false); $page->redirect_last = time(); $page->redirect_counter += 1; if ($page->save('redirect_counter')) { $session->redirect($page->website_url, 302); } } Thanks.
    • By daniel_puehringer
      Hi community, thanks for the great work you do 🙂

      I´m looking for a global field which can be used in the footer and has only one instance for all pages. So I´m kind of looking for "One field to rule them all".
       
      Reason for my question: The website I´m currently building has one footer for all pages. The content of the footer should be dynamic - therefore I made a global field which is automatically inserted into each page.
      Problem: if I want to change something in the footer, I have to do it for all fields, because each field has it´s own instance.

      Question: is it possible to have one global field in which changes the content for the footer of all pages?

      In most CMS Systems a simple solution to this is often impossible, I hope that processwire is different 🙂

      All the best,
      Daniel
       
    • By Pip
      Hi, Everyone! 
      I'm currently working on a page reference field and set it for multiple pages (AsmSelect) for the input. Is there a way for me to add an image field (aka Avatar) and the title of page in the radio button? 
      I used the field name enclosed in the { }. Didn't work. It appeared a text instead. 
      Thanks in advance and hope to hear from you soon!
    • By EyeDentify
      Hello dear PW gurus.

      I have stumbled over a strange error that i all of sudden got when trying to upload an image to a images field on a page.
      There where images allready stored in the field that i wanted to keep, but during the upload the error apear and after that all images are gone from the field and i can´t upload any, i just get the error every time.
      I am running ProcessWire 3.0.153 dev.
      Update:
      After looking in the assets folder i find the folder for the page and the image files seems to be there including the ones i tried to upload when the error occured.
      But they don´t show up in the images field in the page editor.
       
      The error reported:
       
      SQLSTATE[01000]: Warning: 1265 Data truncated for column 'ratio' at row 1 And here is a screenshot of the event:

      The TracyDebugger Error reporting:

      I hope you fine folks could point me in a direction.
      But it seems our old pal set_time_limit() is back.

      Regards, EyeDentify
×
×
  • Create New...