4

In mysite/code/Connectors.php I've created a form with a custom template in the Page_Controller here is the code:

class Connectors_Controller extends Page_Controller {
    private static $allowed_actions = array (
        'TestForm',
        'TestFunction'
    );

    public function TestFunction(){
        return 'Hello World!';
    }

    public function TestForm(){

        $fields = new FieldList(
            new TextField('Test', 'Test')
        );

        $actions = new FieldList(
            new FormAction('doSubmit', 'Submit')
        );

        $form = new Form($this, 'TestForm', $fields, $actions);
        $form->setTemplate('ContactForm');

        return $form;
    }
} 

I created an include page themename/templates/Includes/ContactForm.ss

<form $FormAttributes id="contactform" action="$Link/Connectors" method="post" class="validateform AjaxForm">

    <% loop $Fields %>
        $Field 
    <% end_loop %>

    $Actions.dataFieldByName(action_doSubmit)

    // I want this function to print Hello World but it doesn't
    $TestFunction

</form>

This works fine until I want to run another function from the same controller in the template.

Normally I'd simply create a public function and call it within the template - but this doesn't work.

How can I access a function from within a custom form template?

I've tried various methods of accessing it such as $Top.TestFunction, $TestFunction() and $Parent.TestFunction

Thanks - Ash

m02ph3u5
  • 3,022
  • 7
  • 38
  • 51

2 Answers2

5

that's a scope problem. Putting functions in your controller works fine, when the Controller is rendering the template. In your case, the Form is rendering the template, you have to tell your Form what to use when it should replace $TestFunction, using customise(), e.g. when returning it:

return $form->customise(array(
    'TestFunction' => $this->TestFunction()
));
wmk
  • 4,598
  • 1
  • 20
  • 37
  • One more question. Is there a way of being able to return parameters to the function though the template. Like `$TestFunction(9)` or `$TestFunction($Pos)` at the moment I can only run the function not pass anything to it. – Ashley Ktorou Sep 04 '15 at 15:20
  • thanks, removed that semicolon. normally (in a controller) you can pass params to it, with customise.. i don't know if customise can accept closures? would be worth a try... – wmk Sep 04 '15 at 15:56
  • if you really need to pass params to it, you might extend Form with an Extension class and plug the functionality to it. Extending is most of the time better than subclass form and use your form class instead. – wmk Sep 04 '15 at 15:57
1

PHP uses an arrow instead of dot syntax like other programming languages. If you are trying to access a property or function from an instance of a php class then you use the arrow -> like so:

$tmp = new Connectors_Controller();
echo $tmp->TestFunction();

Now if you haven't initialized an instance of your class, you can Scope Resolution Operator like so:

echo Connectors_Controller::TestFunction();

This will call the function directly instead of calling it on an object.

CaldwellYSR
  • 3,056
  • 5
  • 33
  • 50
  • 1
    Hi thanks for your answer but I'm trying to access the function from within the Silverstripe theme so I can perform more complex form logic. – Ashley Ktorou Sep 02 '15 at 14:20
  • Yeah, after I posted the answer I had a feeling it wasn't oging to help. I don't know much about SS, it seems to use a pythony style of code. Sorry I can't be of more help. – CaldwellYSR Sep 02 '15 at 14:26