Jump to content

Only "year" in Datetime field?


moonwhaler
 Share

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 comment
Share on other sites

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 comment
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 comment
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 comment
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 comment
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
 Share

×
×
  • Create New...