Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Determining if a field is empty

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1
Go to End
Author Topic: 1812 Views
  • UncleCheese
    Avatar
    4085 Posts

    Determining if a field is empty Link to this post

    I have a function that I add to most of my data types that looks like this:

    function has($field)
    {
    return !empty($this->{$field});
    }

    I use this to determine at the template level whether or not a specific field has any data, because sometimes the template will require different display logic if it doesn't. For instance:

    <% if has(Email) %>
    $Email
    <% else %>
    $Phone
    <% end_if %>

    My question is, first of all, is this a common practice, or is there an easier way? Secondly, how come when I had this function in my controller it was throwing a PHP error on the template syntax, but when I moved it to my data model, it worked fine? Aren't all template functions supposed to be in the controller? Weird.

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Determining if a field is empty Link to this post

    I just do

    <% if Email %>
    $Email
    <% else %>
    $Phone
    <% end_if %>

    without any function.

    1812 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.