Forum

Thread tagged as: Question, Field-Types

Date Range for Year

Hello there,

We are using the date field in Perch for a funeral home website that requires date of birth and date of passing, however when using:

<perch:content type="date" id="dob" format="F d, Y" label="Date of Birth" />

We get the year range from 1918 to 2028. Is there anyway to extend that date range?

Thank you!

James Harrison

James Harrison 0 points

  • 2 weeks ago

James. You may need to create a custom field type to be able to extend the range. I am going to look at the field type and see if this is a limit Perch has set or something else. Another way is to add a select field with the year range.

I will update this thread when I have additional info.

Hello Robert,

I appreciate your reply. I wondered the the same about needing a custom field type. It is very tidy to use the date field as opposed to a very large select as the site requires a good number of filters based on date that will be a bit more complicated with needing a select (but not unachievable either).

I look forward to hearing what you come up with and appreciate you taking the time!

so here it is... the code responsible for the Year range

for ($i=strftime('%Y')-100; $i<strftime('%Y')+11; $i++) $years[] = array('label'=>$i, 'value'=>$i);

Basically it subtracts 100 from the current year as a starting option and adds 11 for ending option.

So yeah, perch is setting the range. Which means you would need to build your own fieldtype to alter this behaviour or use some JS to add to the selectable options.

Oh, I just realizied something... (Oops I actually Forgot Something)

Fieldtypes will not work in public facing forms, only in admin forms... so I do think your only option is JS...

Sorry :(

Thank you Robert for looking into this, I can see how that logic in Perch would work for most applications and it very nearly works for this one. I will not go down the field type route then and just go straight for writing some JS as you indicate. Thanks again for your research.

Hussein Al Hammad

Hussein Al Hammad 99 points
Registered Developer

Hello James,

Do you want to use the date field in the control panel or on the website?

If it's a public facing form, you would use perch:input, which shouldn't have the same restrictions as what you're describing: https://docs.grabaperch.com/templates/form/input/date/

<perch:input type="date" id="dob">

If you need to use the date field type in an edit form in the control panel, you can enable the native browser controls by adding the native attribute (works for HTML5 browsers) and you shouldn't see the restriction in the year range: https://docs.grabaperch.com/templates/field-types/date/#using-native-html5-controls

<perch:content id="date" type="date" label="Date" format="d F Y" native>

Hussein... Good catch...

I assumed the poster was using a public facing form to gather the information... but a closer look at his initial code shows (Admin) form tag, therefore the native attribute would likely solve the problem.

Perfect - I appreciate both your responses, I should have looked into that more, I didn't realize you could use native HTML 5 formatting on the date type. You are also correct, this is an input in the control panel and not a forward facing input.