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.

General Questions /

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

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

Form behavior in Custom Template


Go to End


2 Posts   2054 Views

Avatar
TDNP

Community Member, 19 Posts

12 January 2012 at 3:02pm

Edited: 12/01/2012 3:05pm

I have been working hard on a form and had it working before applying a custom template. The form takes donor information and writes it to an XML file, then sends the user along to their destination to pay. It was working in it's default layout but now execute the XMLWrite function I created.

DonationForm.php -

<?php
// Donation Form for PNWU
class DonationForm extends Form {

//Define our form function as allowed
static $allowed_actions = array(
'DonationForm',
'XMLWrite',
'HttpSubmission'
);

function DonationForm($controller, $name) {

//Create Fields
$fields = new FieldSet(
new OptionsetField('Prefix', null, array('Mrs' => 'Mr.', 'Mrs.' => 'Mrs.', 'Ms.' => 'Ms.', 'Dr.' => 'Dr.')),
new TextField('FirstName', 'First Name*'),
new TextField('LastName', 'Last Name*'),
new TextField('MI', 'Initial'),
new TextField('SpouseFirst', 'Spouse, First Name*'),
new TextField('SpouseLast', 'Last Name'),
new TextField('SpouseMI', 'Initial'),
new OptionsetField('Type', null, array('Home' => 'Home', 'Business' => 'Business')),
new TextField('Occupation', 'Occupation'),
new TextField('Suffix', 'Suffix'),
new TextField('Business', 'Business Name'),
new TextField('Address', 'Full Address*'),
new TextField('City', 'City'),
new TextField('State', 'State'),
new TextField('Zip', 'Zipcode'),
new NumericField('Phone', 'Home Phone* (No Dashes)', null, 10),
new NumericField('BusinessPh', 'Business Phone', null, 10),
new NumericField('MobilePh', 'Mobile Phone', null, 10),
new EmailField('Email', 'Email Address*'),
//Amount Information
new OptionsetField('GiftType', null, array('OneTime' => null, 'Recurring' => null)),
new CurrencyField('BaseAmount', 'Amount of Donation'),
new CurrencyField('RecurringAmount', 'Recurring Amount'),
new NumericField('Years', 'years', null, 5),
new CurrencyField('MonthlyAmount', null),
new TextField('Month', null),
new TextField('Year', null),
new CurrencyField('InstallAmount', null),
new CheckboxField('RemindMe', null),
new TextField('DisplayAs', null),
new DropdownField('PaymentType', 'Select Your Payment Method', array('CC' => 'Credit Card', 'ACH' => 'Check'))
);
if(isset($_GET['confirm'])) {
echo $xmlfile;
}

// Create action
$actions = new FieldSet(
new FormAction('XMLWrite', 'Submit')
);

// Create action
$requiredFields = new RequiredFields('FirstName', 'LastName', 'Email', 'Address', 'HomePhone', 'PaymentType', 'CardNumber', 'Month', 'Year', 'CVV2');

parent::__construct($controller, $name, $fields, $actions, $requiredFields);

}

function XMLWrite() {
//Write to Donation.xml
//Convert Form data to variables
$name = $_POST['FirstName'] . $_POST['LastName'];
$business = $_POST['Business'];
$address = $_POST['Address'];
$address2 = $_POST['Address2'];
$city = $_POST['City'];
$state = $_POST['State'];
$zip = $_POST['Zip'];
$homePhone = $_POST['HomePhone'];
$cellPhone = $_POST['CellPhone'];
$email = $_POST['Email'];
$giftType = $_POST['GiftType'];
$paymentType = 'CC';
$cardNumber = $_POST['CardNumber'];
$expiration = $_POST['Month'] . '/' . $_POST['Year'];
$CVV2 = $_POST['CVV2'];
$securityID = $_POST['SecurityID'];
// Amount Information

$xmlfile = "../assets/OfficialPayments/" . $name . $paymentType . ".xml";
$file = fopen($xmlfile,"w");

$xml_document = '<?xml version="1.0" encoding="utf-8"?>
<Payment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ApplicationID>3968</ApplicationID>
<SkinID>3700</SkinID>
<UniqueID />
<BaseAmount>' . $amount . '</BaseAmount>
<ConvenienceFee>0.00</ConvenienceFee>
<ErrorURL>http://website/departments/advancement/support-pnwu?error=1</ErrorURL>
<PostbackURL>http://website/departments/advancement/postback</PostbackURL>
<Processor>OPC</Processor>
<ReturnURL>http://website/departments/advancement/support-pnwu?success=1</ReturnURL>
<DisplayTerms />
<ItemID />
<TimeoutURL>http://website/departments/advancement/support-pnwu?timeout=1</TimeoutURL>
<PaymentMethod>'. $paymentType .'</PaymentMethod>
<PaymentTypes>AE|DS|MC|VI</PaymentTypes>
<ShowCVV2>true</ShowCVV2>
</Payment>';

fwrite($file, $xml_document);
fclose($file);
}

/*public function FormAttributes() {

if(!isset($_GET['confirm'])) {
$attr = parent::FormAttributes();
// Do your manipulations
$attr = 'id="DonationForm_DonationForm" action="http://localhost/pnwu/home/new-testpage/?confirm=1" method="post" enctype="application/x-www-form-urlencoded"';
return $attr;
} else if(isset($_GET['confirm']) && $_GET['confirm'] == '1') {
$attr = parent::FormAttributes();
// Do your manipulations
$attr = 'id="DonationForm_DonationForm" action="https://remoteWebsite/payselect/payselect_epayprocessor.asmx" method="post" enctype="application/x-www-form-urlencoded"';
return $attr;
}
}*/

function forTemplate() {

return $this->renderWith(array(
$this->class,
'Form'
));

}

}

DonationForm.ss -

<% require javascript(http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js ) %>
<div id="text2">
<form $FormAttributes>
<fieldset>
<h3 align="center">Donor Information</h3>
<table cellpadding="4">
<tr>
<td colspan="4">$dataFieldByName(Prefix)</td>
</tr>
<tr>
<td><label>First Name: </label>$dataFieldByName(FirstName)</td>
<td colspan="2"><label>Last Name: </label>$dataFieldByName(LastName)</td>
<td align="right"><label>Initial: </label>$dataFieldByName(MI)</td>
</tr>
<tr>
<td><label>Spouse, First Name: </label>$dataFieldByName(SpouseFirst)</td>
<td colspan="2"><label>Last Name: </label>$dataFieldByName(SpouseLast)</td>
<td align="right"><label>Initial: </label>$dataFieldByName(SpouseMI)</td>
</tr>
<tr>
<td>Is this $dataFieldByName(Type) information?</td>
<td colspan="3" align="right"><label>Name of Business: $dataFieldByName(Business)</td>
</tr>
<tr>
<td><label>Occupation: </label>$dataFieldByName(Occupation)</td>
<td><label>Title/Suffix: </label>$dataFieldByName(Suffix)</td>
<td colspan="2" align="right"><label>Email: </label>$dataFieldByName(Email)</td>
</tr>
<tr>
<td><label>Address: </label>$dataFieldByName(Address)</td>
<td><label>City: </label>$dataFieldByName(City)</td>
<td><label>State: </label>$dataFieldByName(State)</td>
<td align="right"><label>Zip: </label>$dataFieldByName(Zip)</td>
</tr>
<tr>
<td><label>Phone: </label>$dataFieldByName(Phone)</td>
<td colspan="2"><label>(Business) </label>$dataFieldByName(BusinessPh)</td>
<td align="right"><label>(Mobile) </label>$dataFieldByName(MobilePh)</td>
</tr>
</table>
<h3 align="center">Donation Details</h3>
<table background="#a0a0a0">
<tr>
<td rowspan="2">$dataFieldByName(GiftType)</td>
<td>$dataFieldByName(BaseAmount)</td>
<td>One-time <i>cash</i> gift.</td>
</tr>
<tr>
<td>$dataFieldByName(RecurringAmount)</td>
<td>Pledge, to be paid over a period of $dataFieldByName(Years) years.</td>
</tr>
<tr>
<td align="right" colspan="2">First Payment of $dataFieldByName(MonthlyAmount)</td>
<td> will be made (month) $dataFieldByName(Month) of (year) $dataFieldByName(Year)</td>
</tr>
<tr>
<td align="left" colspan="2">In equal installments of $dataFieldByName(InstallAmount)</td>
<td>$dataFieldByName(RemindMe) Please send me pledge reminders.
</tr>
<tr>
<td colspan="3">&nbsp;</td>
</tr>
<tr>
<td colspan="3">Please indicate how your name(s) should appear on our published list of donors: <br />$dataFieldByName(DisplayAs)</td>
</tr>
<tr>
<td colspan="3">&nbsp;</td>
</tr>
<tr>
<td colspan="2" align="right">$dataFieldByName(PaymentType)</td>
<td><% control actions %>$Field<% end_control %></td>
</tr>
</table>
</fieldset>
</form>
</div>

Sorry for the length but I couldn't attach the files.

Avatar
TDNP

Community Member, 19 Posts

13 January 2012 at 2:04pm

A little more on the subject. It appears that by creating the form template and adding the fields this way, the action that is applied via the "submit" button is not being called. In this case, the XMLWrite function is not being run. I tested by removing the rest of the code and just used:
Director::redirect(Director::baseURL() . $this->URLSegment . "/?confirm=1");

The redirect did not happen. Anyone know why the $action is not working?