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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Create e-mail from in base page


Go to End


1337 Views

Avatar
PeterR

Community Member, 15 Posts

29 July 2013 at 6:51am

Hello,

I´m stucked with creating custom e-mail form. I tried to follow tutorials & topics but something gone wrong :)

My target is to create an custom e-mail form which will be available in the footer on all site pages.

So I modify mysite/code/Page.php
----------------------------------------------------------------------
<?php
class Page extends SiteTree {

public static $db = array(
);

public static $has_one = array(
);

}
class Page_Controller extends ContentController {

public function init() {
parent::init();
}

static $allowed_actions = array('HelloForm');

public function HelloForm()
{
return new MyForm($this, 'HelloForm');
}
}

class MyForm extends Form
{
public function __contruct($controller, $name)
{
$fields = new FieldList(
TextField::create('Name'),
EmailField::create('Email'),
TextField::create('City'),
TextareaField::create('Text')
);

$actions = new FieldList(
FormAction::create('submit', 'Submit')->addExtraClass('button-send')
);

parent::__contruct($controller, $name, $fields, $actions);
}

public function submit($data, $form)
{
// sending e-mail ....
}

public function forTemplate()
{
return $this->renderWith(array($this->class, 'Form'));
}
}

-------------------------------
in footer.ss I have $HelloForm
-------------------------------
---------------------------------------------------------------------
and here is my templates/simple/includes/MyForm.ss
---------------------------------------------------------------------

<form $FormAttributes>
<% if $Message %>
<p id="{$FormName}_error" class="message $MessageType">$Message</p>
<% else %>
<p id="{$FormName}_error" class="message $MessageType" style="display: none"></p>
<% end_if %>

<h2>Contact</h2>
<div class="contact-left">
$Fields.dataFieldByName(Name)
$Fields.dataFieldByName(City)
$Fields.dataFieldByName(Email)
</div>
<div class="contact-right">
$Fields.dataFieldByName(Text)

<% if $Actions %>
<div class="Actions">
<% loop $Actions %>$Field<% end_loop %>
</div>
<% end_if %>
</div>
</form>

---------------------------------------------
the problem is that page is generated with error

[User Error] Uncaught InvalidArgumentException: $fields must be a valid FieldList instance

in \framework\forms\Form.php

so the problem is with calling parent method. But I already sent $fields which is declared in MyForm contructor method, so... ?

I do not know how to fix it...I am using Silverstripe 3.0.5.