From 71c64c19684afa4142dc8beb612f410378f45cc4 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 12 Mar 2009 16:42:31 +0000 Subject: [PATCH] ENHANCEMENT Using ajax for the add/create action in ModelAdmin ENHANCEMENT Adding global ajax error handlers for ModelAdmin save/delete requests MINOR Added $recordID parameter to ModelAdmin_RecordController so it can be instanciated manually (necessary for post-create actions in ModelAdmin) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@72978 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- code/ModelAdmin.php | 39 ++++++++++++++++++++++++++++++++------- javascript/ModelAdmin.js | 22 +++++++++++++++------- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/code/ModelAdmin.php b/code/ModelAdmin.php index 85be6540..37d48890 100644 --- a/code/ModelAdmin.php +++ b/code/ModelAdmin.php @@ -323,13 +323,16 @@ class ModelAdmin_CollectionController extends Controller { $fields = $context->getSearchFields(); $columnSelectionField = $this->ColumnSelectionField(); $fields->push($columnSelectionField); + $validator = new RequiredFields(); + $validator->setJavascriptValidationHandler('none'); $form = new Form($this, "SearchForm", $fields, new FieldSet( new FormAction('search', _t('MemberTableField.SEARCH')), $clearAction = new ResetFormAction('clearsearch', _t('ModelAdmin.CLEAR_SEARCH','Clear Search')) - ) + ), + $validator ); //$form->setFormAction(Controller::join_links($this->Link(), "search")); $form->setFormMethod('get'); @@ -361,7 +364,10 @@ class ModelAdmin_CollectionController extends Controller { $createButton->dontEscape = true; - return new Form($this, "CreateForm", new FieldSet(), $actions); + $validator = new RequiredFields(); + $validator->setJavascriptValidationHandler('none'); + + return new Form($this, "CreateForm", new FieldSet(), $actions, $validator); } /** @@ -403,11 +409,15 @@ class ModelAdmin_CollectionController extends Controller { new FormAction('import', _t('ModelAdmin.IMPORT', 'Import from CSV')) ); + $validator = new RequiredFields(); + $validator->setJavascriptValidationHandler('none'); + $form = new Form( $this, "ImportForm", $fields, - $actions + $actions, + $validator ); return $form; } @@ -690,6 +700,8 @@ class ModelAdmin_CollectionController extends Controller { } $validator = ($newRecord->hasMethod('getCMSValidator')) ? $newRecord->getCMSValidator() : null; + if(!$validator) $validator = new RequiredFields(); + $validator->setJavascriptValidationHandler('none'); $actions = new FieldSet ( new FormAction("doCreate", _t('ModelAdmin.ADDBUTTON', "Add")) @@ -710,7 +722,19 @@ class ModelAdmin_CollectionController extends Controller { $form->saveInto($model); $model->write(); - Director::redirect(Controller::join_links($this->Link(), $model->ID , 'edit')); + if(Director::is_ajax()) { + $recordController = new ModelAdmin_RecordController($this, $request, $model->ID); + return new HTTPResponse( + $recordController->EditForm()->forAjaxTemplate(), + 200, + sprintf( + _t('ModelAdmin.LOADEDFOREDITING', "Loaded '%s' for editing."), + $model->Title + ) + ); + } else { + Director::redirect(Controller::join_links($this->Link(), $model->ID , 'edit')); + } } } @@ -725,10 +749,10 @@ class ModelAdmin_RecordController extends Controller { static $allowed_actions = array('edit', 'view', 'EditForm', 'ViewForm'); - function __construct($parentController, $request) { + function __construct($parentController, $request, $recordID = null) { $this->parentController = $parentController; $modelName = $parentController->getModelClass(); - $recordID = $request->param('Action'); + $recordID = ($recordID) ? $recordID : $request->param('Action'); $this->currentRecord = DataObject::get_by_id($modelName, $recordID); parent::__construct(); @@ -779,7 +803,8 @@ class ModelAdmin_RecordController extends Controller { $fields = $this->currentRecord->getCMSFields(); $fields->push(new HiddenField("ID")); - $validator = ($this->currentRecord->hasMethod('getCMSValidator')) ? $this->currentRecord->getCMSValidator() : null; + $validator = ($this->currentRecord->hasMethod('getCMSValidator')) ? $this->currentRecord->getCMSValidator() : new RequiredFields(); + $validator->setJavascriptValidationHandler('none'); $actions = $this->currentRecord->getCMSActions(); if($this->currentRecord->canEdit(Member::currentUser())){ diff --git a/javascript/ModelAdmin.js b/javascript/ModelAdmin.js index 0c64e25a..d45e566f 100644 --- a/javascript/ModelAdmin.js +++ b/javascript/ModelAdmin.js @@ -10,6 +10,14 @@ */ (function($) { $(document).ready(function() { + /** + * Generic ajax error handler + */ + $('form').livequery('ajaxError', function (XMLHttpRequest, textStatus, errorThrown) { + $('input', this).removeClass('loading'); + statusMessage(ss.i18n._t('ModelAdmin.ERROR', 'Error'), 'bad'); + }); + /** * Add class ajaxActions class to the parent of Add button of AddForm * so it float to the right @@ -186,20 +194,20 @@ $(document).ready(function() { /** * RHS panel Save button */ - $('#right #form_actions_right input[name=action_doSave]').livequery('click', function(){ + $('#right input[name=action_doSave],#right input[name=action_doCreate]').livequery('click', function(){ var form = $('#right form'); var formAction = form.attr('action') + '?' + $(this).fieldSerialize(); // Post the data to save $.post(formAction, form.formToArray(), function(result){ $('#right #ModelAdminPanel').html(result); - - statusMessage(ss.i18n._t('ModelAdmin.SAVED')); + + statusMessage(ss.i18n._t('ModelAdmin.SAVED', 'Saved')); // TODO/SAM: It seems a bit of a hack to have to list all the little updaters here. // Is livequery a solution? Behaviour.apply(); // refreshes ComplexTableField - }); + }, 'html'); return false; }); @@ -207,8 +215,8 @@ $(document).ready(function() { /** * RHS panel Delete button */ - $('#right #form_actions_right input[name=action_doDelete]').livequery('click', function(){ - var confirmed = confirm(ss.i18n._t('ModelAdmin.REALLYDELETE')); + $('#right input[name=action_doDelete]').livequery('click', function(){ + var confirmed = confirm(ss.i18n._t('ModelAdmin.REALLYDELETE', 'Really delete?')); if(!confirmed) { $(this).removeClass('loading') return false; @@ -222,7 +230,7 @@ $(document).ready(function() { // On success, the panel is refreshed and a status message shown. $('#right #ModelAdminPanel').html(result); - statusMessage(ss.i18n._t('ModelAdmin.DELETED')); + statusMessage(ss.i18n._t('ModelAdmin.DELETED', 'Successfully deleted')); $('#form_actions_right').remove(); // To do - convert everything to jQuery so that this isn't needed