Created
August 1, 2013 16:14
-
-
Save intellix/6132863 to your computer and use it in GitHub Desktop.
Add data-ajax="1" and on submit, it will post the form to a ZF2 ControllerAction via AJAX and push the errors into your form. HTML is what is generated via DluTwBootstrap. You'll need jQuery UI Widget Factory and Twitter Bootstrap
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
/** | |
* @author Dominic Watson | |
* | |
* Functionality for posting ZF2 forms with AJAX and displaying errors etc. | |
* Just add data-ajax="1" to the <form> | |
*/ | |
(function( $ ) { | |
$.widget( "app.ajaxZendForms", { | |
options: { | |
}, | |
_create: function () | |
{ | |
var self = this; | |
self.setupEvents(); | |
}, | |
setupEvents: function () | |
{ | |
var self = this; | |
self.element.on('submit', 'form[data-ajax]', function () { | |
var form = $(this), | |
button = form.find('button[type="submit"], input[type="submit"]'); | |
button.button('loading'); | |
// Remove old errors as they're re-added again later anyway | |
form.find('.errors').remove(); | |
form.find('.control-group').removeClass('error'); | |
$.ajax({ | |
url: form.attr('action'), | |
type: form.attr('method'), | |
data: form.serializeObject() | |
}).always(function (msg) { | |
button.button('reset'); | |
}).done(function (msg) { | |
if (msg.success === false) { | |
self.fail(form, msg); | |
return false; | |
} | |
/** | |
* For now let's just set the button to green and disabled, | |
* we could eventually return success messages or flashMessages and invoke it here | |
*/ | |
button.button('complete').prop('disabled', 'disabled'); | |
}).fail(function (msg) { | |
self.fail(form, msg); | |
}) | |
return false; | |
}); | |
}, | |
fail: function (form, msg) | |
{ | |
var self = this; | |
// Loop through each of the fields | |
$.each(msg.errors, function (fieldName, errors) { | |
var controlGroup = $('#cgroup-' + fieldName), | |
controls = controlGroup.find('.controls'), | |
errorList = $('<ul class="help-block errors"></ul>'); | |
controlGroup.addClass('error'); | |
controls.append(errorList); | |
// Loop through each of the errors and append it to the UL list of errors | |
$.each(errors, function(errorKey, errorText) { | |
errorList.append('<li>' + errorText + '</li>'); | |
}); | |
}); | |
}, | |
}); | |
}( jQuery ) ); |
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
<form action="/index/do-something" method="post" name="do-something-form" data-ajax="1"> | |
<div class="control-group" id="cgroup-name"> | |
<div class="controls" id="controls-name"> | |
<textarea name="name" id="name"></textarea> | |
</div> | |
</div> | |
<div class="control-group" id="cgroup-submit"> | |
<div class="controls" id="controls-submit"> | |
<button type="submit" name="submit" class="btn-primary btn" data-loading-text="Checking..." data-complete-text="Success!" value="">Search</button> | |
</div> | |
</div> | |
</form> |
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
<?php | |
namespace Application\Controller; | |
use Zend\Mvc\Controller\AbstractActionController; | |
use Zend\Mvc\MvcEvent; | |
use Zend\View\Model\JsonModel; | |
class IndexController extends AbstractActionController | |
{ | |
public function doSomethingAction() | |
{ | |
$request = $this->getRequest(); | |
$form = $this->getServiceLocator()->get('formelementmanager')->get('Application\Form\form'); | |
if ($request->isPost()) { | |
$form->setData($request->getPost()); | |
if ($form->isValid()) { | |
$formData = $form->getData(); | |
// Call some service | |
return new JsonModel(array( | |
'success' => true, | |
)); | |
} else { | |
return new JsonModel(array( | |
'success' => false, | |
'errors' => $form->getMessages() | |
)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment