Skip to content

Instantly share code, notes, and snippets.

@bummzack
Created May 27, 2015 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bummzack/5d9507c88398508b87d2 to your computer and use it in GitHub Desktop.
Save bummzack/5d9507c88398508b87d2 to your computer and use it in GitHub Desktop.
Simple frontend form implementation with SortableUploadField
// 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('MyTemplate');
}
public function UserForm()
{
$uploadField = new SortableUploadField('Images');
$fields = new FieldList(
new TextField('Firstname'),
new TextField('Lastname'),
$uploadField
);
// Create actions
$actions = new FieldList(
new FormAction('submitForm', 'Submit')
);
$validator = new RequiredFields('Firstname', 'Lastname');
return new Form($this, 'UserForm', $fields, $actions, $validator);
}
public function submitForm($data, $form)
{
$record = new DataRecord();
$form->saveInto($record);
$record->write();
}
}
// Template : MyTemplate.ss
<!DOCTYPE html>
<html>
<head>
<% base_tag %>
<title>Frontend Form Test</title>
</head>
<body >
<div id="Container">
<h1>Frontend Form Test</h1>
$UserForm
</div>
</body>
</html>
// ... 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