Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Testing data from Controller

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: 1162 Views
  • KatB
    Avatar
    Community Member
    94 Posts

    Testing data from Controller Link to this post

    Thanks for putting up with me !

    It really feels like most of what I want will be here, eventually :
    http://doc.silverstripe.com/doku.php?id=page-types

    Going back to Tutorial 2:
    A user creates a new ArticlePage, writes their article, puts in the date, but forgets to add their name.

    How can I create a function from ArticlePage_Controller that sends back a boolean as to whether or not this particular ArticlePage has an author?

    function hasAuthor(){
    return this->'Author' !="" ? true : false;
    }

    Q. Is 'this->'Author' ' the correct way to get access to the data in the database as created by the user?

    I realise that this is possible in the templating system with <% if $Author != ""> but what if I wanted to know whether or not a combination of things were in existence? Or at least a certain amount of the available fields were filled in?

    Example: A postal address doesn't necessarily have to have all parts of the available fields to be correct. Example, some addresses have just building names, others have street numbers and names, some have suburbs and cities, others have just one or the other. So if we determine that a certain number of fields need to be entered to be correct, then we can count the number of fields and determine whether or not this entry has a postal address.

    function postal(){
    $postal = false;
    $count = 0;
    if (this->'Postal_Street' != "")
    $count++;
    if (this->'Postal_Suburb' !="")
    $count++;
    if (this->'Postal_Postcode' != "")
    $count++;

    return $count > 2? true : false;
    }

    Then in the template, this particular child would be queried if it had a postal address, and if it did,you would set up HTML code for the address and then you could query each field individually.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Testing data from Controller Link to this post

    In the ss template you can just have <% if Author %> and <% if Date %>

  • KatB
    Avatar
    Community Member
    94 Posts

    Re: Testing data from Controller Link to this post

    As I said, I did realise that. Nonetheless, how do I test whether or not the user has put anything meaningful into a particular field for a particular object?

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: Testing data from Controller Link to this post

    Your question seems to be around complex fields - like your postal address question.

    I woudl probably make a function on your Page / ArticlePage / etc class that builds the complex field, or returns null if it's blank. For exampe:

    function FullPostal(){
    $parts = array();
    if (this->'Postal_Street' != "")
    $parts[] = $this->Postal_Street;
    if (this->'Postal_Suburb' !="")
    $parts[] = $this->Postal_Suburb;
    if (this->'Postal_Postcode' != "")
    $parts[] = $this->Postal_Postcode;

    if(sizeof($parts)) >= 2) return implode("<br>", $parts);
    }

    Then you can simply go:

    [html]
    <% if FullPostal %>
    postal address: $FullPostal
    <% end_if %>
    [/html]

    Or

    [html]
    <% if FullPostal %>
    suburb: $Postal_Suburb
    ...
    <% end_if %>
    [/html]

    1162 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.