-
-
Save phil-quinn/0a4f21f0e7a303166979 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); | |
$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 new Form($this, 'UserForm', $fields, $actions, $validator); | |
} | |
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