Last active
August 29, 2015 13:57
-
-
Save anyt/9537900 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// примеры команд, которые выполняются при успешном сабмите формы | |
$.ajaxFormCommands = { | |
redirect: function (target) { | |
window.location.href = target; | |
}, | |
reload: function () { | |
location.reload(); | |
}, | |
replace: function (newContent, target) { | |
var $target = $(target); | |
$target.replaceWith(newContend); | |
}, | |
prepend: function (newContent, target) { | |
var $target = $(target); | |
$target.prepend(newContent); | |
} | |
}; |
This file contains hidden or 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
/* action для сабмита формы, у каждой формы свой */ | |
<?php | |
public function createAction(Request $request) | |
{ | |
$post = new Post(); | |
$form = $this->createCreateForm($post); | |
$form->handleRequest($request); | |
if ($form->isValid()) { | |
$em = $this->getDoctrine()->getManager(); | |
$em->persist($post); | |
$em->flush(); | |
$url = $this->generateUrl('thankyou_page'); | |
// если форма валидна возвращаем массив "команд", которые выполняться на фронтенде | |
// Примеры: | |
// редирект на новую страницу | |
return new JsonResponse([ | |
['redirect', $url] | |
]); | |
// перезагрузка текущей страницы | |
return new JsonResponse([ | |
['reload'] | |
]); | |
// jQuery команды (replace, prepend, append и т.д.) | |
// в массиве первый элемент название команды, остальные - аргументы | |
return new JsonResponse([ | |
['replace', '<p>Thank You!</p>', 'this'], // заменить форму на произвольный html | |
['prepend', '<p>new post</p>', '#posts'] // добавить в начале div#posts новый Post | |
]); | |
// также легко добавить любые свои доп команды. | |
} | |
// если форма не валидна, возвращаем саму форму с ошибками. | |
return new JsonResponse([ | |
'error', | |
$this->renderView( | |
'DemoBundle:Post:create_form.html.twig', | |
array('form' => $form->createView()) | |
), | |
]); | |
} |
This file contains hidden or 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_start(form, {'attr': {'novalidate': 'novalidate' }}) }} | |
{{ form_widget(form) }} | |
{# для того чтобы форма сабмитилась ajax, нужно к кнопке добавить класс .ajax-submit #} | |
<button class="btn btn-success ajax-submit">Create</button> | |
{{ form_end(form) }} | |
{# также на странице с ajax формой нужно подключить один js файл. #} |
This file contains hidden or 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_start(form, {style: 'horizontal', action: path('video_update', {id: video.id}) }) }} | |
<div class="modal-header"> | |
<a class="close" data-dismiss="modal">×</a> | |
<h4 class="modal-title">Edit Video</h4> | |
</div> | |
<div class="modal-body"> | |
{{ form_widget(form) }} | |
</div> | |
<div class="modal-footer form-actions"> | |
<button class="btn btn-primary btn-long ajax-submit" type="submit">Save</button> | |
<a class="btn" data-dismiss="modal">Cancel</a> | |
</div> | |
{{ form_end(form) }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment