1

I'm having a strange issue where I can't seem to determine whether a standard date field in EE2 contains data. For the date variable {birth_date}:

{if birth_date} Do stuff {/if}

In my code, this does the same thing (validates as true) regardless if the date field in an entry has data or not. I have swapped in other field types in place of this and they all behave as expected in the logic, but not the date field. Any idea if logic for date fields needs to be written differently? Seems like it shouldn't but I've spent several hours trying to figure this one out...

1 Answer 1

1

So, this is an old boat that doesn't always sail well (EE2 conditionals depending on field type [sorry for the random metaphor]).

Here is what you should start off by trying. First, make sure you have two entries with opposite testing criteria (one entry with birth_date filled in, one without). Then, in your template, add these tests and check both entries:

{!-- First test --}
{if birth_date}
    Test one win!
{/if}

{!-- Second test --}
{if {birth_date}}
    Test two win!
{/if}

{!-- Second test --}
{if "{birth_date}"}
    Test three win!
{/if}

There have always been some issues with field truthiness testing in EE2, depending on the version. One of these usually wins.

For a bonus round, do some self-discovery about how the field outputs, regardless of if it is null, false, a valid string, etc... Go ahead and set this template to enable PHP on output. Then, try this in your template with different entry values for birth_date:

<?php
echo "<br/>Start of dump $$";var_dump("{birth_date}");echo "$$ End of dump<br/>";
?>
1
  • 1
    Thank you! The second and third ways of writing it seem to test as expected, but the second way would cause syntax errors if combined into an AND/OR condition, but the 3rd way with the quotes seems to fix both issues. Very strange!
    – Russ
    Jan 4, 2018 at 15:27

Your Answer

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

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