Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Archive /

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

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

Testing data from Controller


Reply


4 Posts   1246 Views

Avatar
KatB

Community Member, 94 Posts

25 July 2007 at 5:48pm

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 [url=http://doc.silverstripe.com/doku.php?id=tutorial:2-extending-a-basic-site]Tutorial 2[/url]:
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.

Avatar
Willr

Forum Moderator, 5513 Posts

25 July 2007 at 5:59pm

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

Avatar
KatB

Community Member, 94 Posts

25 July 2007 at 6:23pm

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?

Avatar
Sam

Administrator, 685 Posts

25 July 2007 at 8:14pm

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]