Hi
I have been following this http://doc.silverstripe.org/sapphire/en/topics/forms "Using a custom template", which I finally got to work (SS Admin, you really need to update the docs, as the form needs putting within a control and you need to post it to the page from you php as well), anyway... It posts to itself (so to speak) and doesnt actually run the submit command, any ideas?
MyCvUploadForm.php
<?php
class MyCvUploadForm extends Form {
function __construct($controller, $name) {
$fields = new FieldSet(
new TextField('Name', 'Name'),
new EmailField('Email', 'Email'),
new TextField('Telephone','Telephone / Mobile'),
new FileField('Attchment', 'CV Attachment')
);
// Create action
$actions = new FieldSet(
new FormAction('SendContactForm', 'UPLOAD & SEND')
);
// Create Validators
$validator = new RequiredFields('Name', 'Email', 'Telephone');
parent::__construct($controller, $name, $fields, $actions);
}
function forTemplate() {
return $this->renderWith(array(
$this->class,
'Form'
));
}
function submit($data, $form) {
//Set data
$From = $data['Email'];
$To = "lawrence@newedge.co.uk";
$Subject = "CV Upload Submission";
$email = new Email($From, $To, $Subject);
//set template
$email->setTemplate('CvUploadEmail');
//populate template
$email->populateTemplate($data);
//attach upload
if (isset($_FILES["Attchment"]) && is_uploaded_file($_FILES["Attchment"]["tmp_name"])) {
$email->attachFile($_FILES["Attchment"]["tmp_name"], $_FILES["Attchment"]["name"]);
}
//send mail
$email->send();
//return to submitted message
Director::redirect($this->Link("?success=1"));
}
public function Success()
{
return isset($_REQUEST['success']) && $_REQUEST['success'] == "1";
}
}
MyCvUploadForm.ss (ps I know this is hacky, but its the way I need it for the design)
<% control FormUpload %>
<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 %>
$dataFieldByName(SecurityID)
<div class="form">
<h3>Upload your CV</h3>
<input id="MyCvUploadForm_FrmAnything_Name" name="" type="text" class="tb1" title="Name" value="Name" onfocus="if(value=='Name'){value=''}" onblur="if(value==''){value='Name'}" />
<input id="MyCvUploadForm_FrmAnything_Email" name="" type="text" class="tb1" title="Name" value="Email" onfocus="if(value=='Email'){value=''}" onblur="if(value==''){value='Email'}" />
<input id="MyCvUploadForm_FrmAnything_Telephone" name="" type="text" class="tb1" title="Name" value="Telephone / Mobile" onfocus="if(value=='Telephone / Mobile'){value=''}" onblur="if(value==''){value='Telephone / Mobile'}" />
<input id="MyCvUploadForm_FrmAnything_Attchment" name="" type="file" class="files" title="Name" value="CV Attachment" />
<% if Actions %>
<div class="Actions">
<% control Actions %>$Field<% end_control %>
</div>
<% end_if %>
</div>
</form>
<% end_control %>
In Page.php I also have...
public function FormUpload() {
return new MyCvUploadForm($this,'FrmAnything');
}
Which in the end results in this output
<form id="MyCvUploadForm_FrmAnything" action="/clients/FrmAnything" method="post" enctype="multipart/form-data">
<p id="MyCvUploadForm_FrmAnything_error" class="message " style="display: none"></p>
<input class="hidden" type="hidden" id="MyCvUploadForm_FrmAnything_SecurityID" name="SecurityID" value="52610e70ca124bb97fb49cb9fe09d4fa3b17c835" />
<div class="form">
<h3>Upload your CV</h3>
<input id="MyCvUploadForm_FrmAnything_Name" name="" type="text" class="tb1" title="Name" value="Name" onfocus="if(value=='Name'){value=''}" onblur="if(value==''){value='Name'}" />
<input id="MyCvUploadForm_FrmAnything_Email" name="" type="text" class="tb1" title="Name" value="Email" onfocus="if(value=='Email'){value=''}" onblur="if(value==''){value='Email'}" />
<input id="MyCvUploadForm_FrmAnything_Telephone" name="" type="text" class="tb1" title="Name" value="Telephone / Mobile" onfocus="if(value=='Telephone / Mobile'){value=''}" onblur="if(value==''){value='Telephone / Mobile'}" />
<input id="MyCvUploadForm_FrmAnything_Attchment" name="" type="file" class="files" title="Name" value="CV Attachment" />
<div class="Actions">
<input class="action " id="MyCvUploadForm_FrmAnything_action_SendContactForm" type="submit" name="action_SendContactForm" value="UPLOAD & SEND" title="UPLOAD & SEND" />
</div>
</div>
</form>
As you can see it posts to "/clients/FrmAnything" which results in a 404.
Any ideas how I get this to like up to the submit function?
Thanks - this has killed my day, 5 hours spent on it already!