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
This commit is contained in:
Ingo Schommer 2009-03-12 16:42:31 +00:00
parent 5b0cf968f0
commit 71c64c1968
2 changed files with 47 additions and 14 deletions

View File

@ -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())){

View File

@ -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