Skip to content

Instantly share code, notes, and snippets.

@bummzack
Forked from phil-quinn/gist:0a4f21f0e7a303166979
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bummzack/d4bbe6d38a1879ae028b to your computer and use it in GitHub Desktop.
Save bummzack/d4bbe6d38a1879ae028b to your computer and use it in GitHub Desktop.
// Data Record Class : DataRecord.php
<?php
class DataRecord extends DataObject
{
private static $db = array(
'Firstname' => 'Varchar(255)',
'Lastname' => 'Varchar(255)'
);
private static $many_many = array(
'Images' => 'Image'
);
private static $many_many_extraFields = array(
'Images' => array('SortOrder' => 'Int')
);
}
// Controller class : MyController.php
<?php
class MyController extends Controller
{
private static $allowed_actions = array(
'submitForm' => true,
'UserForm' => true
);
public function index()
{
return $this->renderWith(array('MyTemplate','Page'));
}
public function UserForm()
{
$uploadField = new SortableUploadField('Images');
$uploadField->setEnablePermissionCheck(false);
$fields = new FieldList(
new HiddenField('ID'),
new TextField('Firstname'),
new TextField('Lastname'),
$uploadField
);
// Create actions
$actions = new FieldList(
new FormAction('submitForm', 'Submit')
);
$validator = new RequiredFields('Firstname', 'Lastname');
$form = new Form($this, 'UserForm', $fields, $actions, $validator);
// get and load up the first record if it exists
$record = DataRecord::get()->First();
if ($record) {
$form->loadDataFrom($record);
}
return $form;
}
public function submitForm($data, $form)
{
// if we have an ID, then use that record, otherwise this is new
if (isset($data['ID']) && $data['ID']) {
$record = DataRecord::get()->byId($data['ID']);
}
else {
$record = new DataRecord();
}
$form->saveInto($record);
$record->write();
$this->redirectBack();
}
}
// Template : Layouts/MyTemplate.ss
<% include SideBar %>
<div class="content-container unit size3of4 lastUnit">
<article>
<h1>Frontend Form Test</h1>
$UserForm
</article>
</div>
// ... and add a route to the controller to _config.yml
Director:
rules:
'': 'MyController'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment