Jump to content


Photo

How to detect an empty field?


  • Please log in to reply
3 replies to this topic

#1 Michael Murphy

Michael Murphy

    Distinguished Member

  • Members
  • PipPipPip
  • 92 posts
  • 53

  • LocationBasel, Switzerland

Posted 19 October 2011 - 01:32 PM

It sounds simple, but I'm having some real issues detecting if a field is empty (has no content)

I have two fields :

  • summary - textarea
  • summary_de - textarea

I want to detect if $summary_de is empty
if its empty show $summary
if $summary is empty show nothing


I have tried the following tests :

// Test A
if ($page->summary_de) {
echo "Test A : True";
} else {
echo "Test A : False";
}

echo "<hr />";

// Test B
if (empty($page->summary_de)) {
echo "Test B : True";
} else {
echo "Test B : False";
}

echo "<hr />";

// Test C
if (isset($page->summary_de)) {
echo "Test C : True";
} else {
echo "Test C : False";
}

echo "<hr />";

// Test D
if (isset($page->summary_de) && $page->summary_de === '') {
echo "Test D : True";
} else {
echo "Test D : False";
}

echo "<hr />";

// Test E
if (is_null($page->summary_de)) {
echo "Test E : True";
} else {
echo "Test E : False";
}



The results on a published page when $summary_de is empty

Test A : True
Test B : False
Test C : True
Test D : False
Test E : False

adding some text into $summary_de and saving :

Test A : True
Test B : False
Test C : True
Test D : False
Test E : False

after removing the $summary_de text and saving again (so its empty) - same results.

Test A : True
Test B : False
Test C : True
Test D : False
Test E : False

So all the tests are the same. Any ideas where I am going wrong? Tried deleting cache, but still the same.
I'm using latest version of 2.1 from github.

Thanks,
Michael

#2 ryan

ryan

    Hero Member

  • Administrators
  • 5,771 posts
  • 3108

  • LocationAtlanta, GA

Posted 19 October 2011 - 01:49 PM

Your first test (Test A) is what I would personally use, i.e.

if($page->summary_de) { ... }

Are these TinyMCE fields? I think you need to look at what's actually in the field. I have a feeling you've got some whitespace in there or something like one of these annoying TinyMCE droppings: "<p>&nbsp;</p>" or maybe just a carriage return or couple of spaces. Get the value out of an HTML context to see what's really in the field:

echo htmlentities(var_dump($page->summary_de));

If you are dealing with possible whitespace in the field (as opposed to HTML tags or entities) then your if statement could account for that by doing this:

if(trim($page->summary_de)) { ... }

Don't bother with using isset() or is_null() -- those are really more useful in determining of the page's template carries those fields at all. But I don't think they are particularly useful for what you are trying to do.

#3 Michael Murphy

Michael Murphy

    Distinguished Member

  • Members
  • PipPipPip
  • 92 posts
  • 53

  • LocationBasel, Switzerland

Posted 20 October 2011 - 04:50 AM

Thanks Ryan, you are right. It was not TinyMCE interfering but the Markdown text formatter. Removing the Markdown text formatter so it's just a plain textarea field, and the standard check :

if($page->summary_de)  { ... }

works as normal.

If using the Markdown text formatter then the php trim function clears up any extra whitespace added, and the following test works :

if(trim($page->summary_de))  { ... }


#4 ryan

ryan

    Hero Member

  • Administrators
  • 5,771 posts
  • 3108

  • LocationAtlanta, GA

Posted 20 October 2011 - 07:34 AM

Ah right I forgot about Markdown, but that makes perfect sense. Glad you found a solution there and thanks for the follow-up.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users