1

I'm trying to collect birthdate information in my Safecracker form. I have a field that is a Date field type. It seems that I am being forced to enter in this format: YYYY-MM-DD HH:MM. I don't want to force my users to enter hours and minutes. How do I work around this such that I only collect year, month, day? I am not using that date picker feature (I have datepicker="no") and would like to implement something "cleaner" (it seems like include_jquery="yes", safecracker_head="yes", etc. mucks up my markup).

Later on, I want to implement a search by age (I'll be using Low Search). Am I on the right track by collecting the birthdate?

2 Answers 2

1
+50

As you mentioned that you are not using the date picker feature so you are instructing the users to enter date of birth in input text in the format of YYYY-MM-DD and field type is Date. Right ??? The Date field type of EE will always require time also (HH:MM) so you can apply some JavaScript stuff which will amend the date value with time. For example: an user is entering date of birth as 1980-01-31 so you have to put some JavaScript so that it changes the date of birth value to 1980-01-31 12:00PM before submit. Rest of the things will be performed by EE. Also, you can populate that date of birth in YYYY-MM-DD format by passing "format" parameter with the field.

You mentioned that you are using Low Search module and want to implement a search by age feature. You can achieve it by adding Range Filter (http://gotolow.com/addons/low-search/docs/tags#form-tag-ranges). You can see within the doc that Low Search automatically detects the date field type of the channel and perform search as well.

For range filter, you will need to have this field something like:

<select name="range:date_of_birth_field">
    <option value="">All</option>
    <option value="<?php echo date('Y-m-d', strtotime('-20 years')).';'.date('Y-m-d', strtotime('-25 years')) ;?>">20 - 25</option>
    <option value="<?php echo date('Y-m-d', strtotime('-25 years')).';'.date('Y-m-d', strtotime('-30 years')) ;?>">25 - 30</option>
    <option value="<?php echo date('Y-m-d', strtotime('-30 years')).';'.date('Y-m-d', strtotime('-35 years')) ;?>">30 - 35</option>
    <option value="<?php echo date('Y-m-d', strtotime('-35 years')).';'.date('Y-m-d', strtotime('-40 years')) ;?>">35 - 40</option>
    <option value="<?php echo date('Y-m-d', strtotime('-40 years')).';'.date('Y-m-d', strtotime('-45 years')) ;?>">40 - 45</option>
    <option value="<?php echo date('Y-m-d', strtotime('-45 years')).';'.date('Y-m-d', strtotime('-50 years')) ;?>">45 - 50</option>
    <option value="<?php echo date('Y-m-d', strtotime('-50 years')).';'.date('Y-m-d', strtotime('-55 years')) ;?>">50 - 55</option>
    <option value="<?php echo date('Y-m-d', strtotime('-55 years')).';'.date('Y-m-d', strtotime('-60 years')) ;?>">55 - 60</option>
    <option value="<?php echo date('Y-m-d', strtotime('-60 years')).';'.date('Y-m-d', strtotime('-65 years')) ;?>">60 - 65</option>
    <option value="<?php echo date('Y-m-d', strtotime('-65 years')).';'.date('Y-m-d', strtotime('-70 years')) ;?>">65 - 70</option>
</select>

You can see, I am using PHP code to output the proper range for the date of birth so you can to enable PHP for that template.

5
  • Yes, I am using EE's date field. I'd like to use HTML5's input type="date", as well. I did think of just adding 00:00 to whatever date is entered via JS -- but I am hoping to handle this without JS. Any other ideas? Is there some sort of hook I can tap into to pre-process the date prior to submitting to EE? Jul 2, 2013 at 2:30
  • If you are using Safecraker, you can use safecracker_submit_entry_start to change the date value before submitted data processing otherwise hook entry_submission_ready will work for you. Jul 2, 2013 at 2:40
  • Is this where I would also validate a minimum age? Or can EE handle that for me elsewhere? Jul 2, 2013 at 2:42
  • Yes, you can do this validation easily with the hooks entry_submission_start and safecracker_submit_entry_start. You will need to update the errors array within the extension for this field error or global error. Jul 2, 2013 at 2:51
  • Important: The lesser value should be on the left side of the range and the bigger value should be on the right side. Otherwise, it won't work. Jul 10, 2013 at 3:49
0

If you're not using the datepicker feature, as you mentioned, then the easiest solution is to use the excellent DropDate addon. I normally default to that when dealing with date collection in both the front and back-end. In the front-end you are then free to take advantage of all of PHP's date formatting options. For instance:

{dropdate format='jS F, Y'}     // 2nd July, 2013
{dropdate format='Y-m-d'}       // 2013-07-02
4
  • How do you use this in a Safecracker form? I assume I make a series of drop down selects, but what name attributes to give them? Jul 2, 2013 at 22:14
  • I tried using <input type="date">, but that didn't seem to work. I entered a valid date. I used the field's short name in the name attribute, but nothing was saved. Jul 2, 2013 at 22:34
  • You literally just output it as a custom field {field:my_field_name}. Where my_field_name is the name that you have called the field. Make sure it's within your Safecracker form and it should output.
    – Ian Young
    Jul 4, 2013 at 9:03
  • Sorry, I wasn't asking how to output this. I want to know how to accept input for this in a Safecracker form. In the CP, I see a series of drop downs. I don't want to use the CP; I want to use this in SF. Jul 5, 2013 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.