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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

custom form posting to its own name!?


Reply


2 Posts   655 Views

Avatar
lozhowlett

Community Member, 146 Posts

3 December 2011 at 3:28am

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 &amp; SEND" title="UPLOAD &amp; 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!

Avatar
martimiz

Forum Moderator, 1132 Posts

3 December 2011 at 5:13am

Edited: 03/12/2011 5:15am

There are two things I can see at a glance:

The url /clients/FrmAnything means SilverStripe is looking for a function FrmAnything() in the page called 'clients'. As this function doesn't exist, it will then try to find an actual page called 'FrmAnything' - thus the 404. To avoid this, change your form instantiation to:

public function FormUpload() {
return new MyCvUploadForm($this,'FormUpload');
}

A bit of a snake biting its own tail, but it works. The other thing: you defined the action as 'sendContactForm':

new FormAction('SendContactForm', 'UPLOAD & SEND')

SilverStripe will go look for a function SendContactForm() to handle submission. But you've named that function submit() instead, so it won't be found...

I hope this will help somewhat. Looking at the date/time of your post, I see its already saturday for you, while I'm still suffering my friday - yet another mystery...

[EDIT] ok - I also seem to have posted this in the direct future - it must be saturday for some...