我需要通过ajax存储来自symfony表单的数据,而不是更新浏览器.此外,我需要你,如果字段中的错误可以某种方式让他们响应该调用Ajax并显示我的表单错误,所有这些都没有刷新页面.
我有一个带有symfony资产的表单来验证字段,如果执行ajax调用,将所有内容都完美,存储数据或更新显示错误的页面,但我需要相同而不刷新页面.
然后我把一些我正在使用的代码:
控制器:
public function createAction(Request $request)
{
$entity = new Student();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('student_show', array('id' => $entity->getId())));
}
return $this->render('BackendBundle:Student:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
Run Code Online (Sandbox Code Playgroud)
ajax调用:( 我不明白如何处理错误部分)
$('.form_student').submit(function(event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: Routing.generate('student_create'),
data: $(this).serialize(),
success: function(data) {
//clean form
cleanForm($(this));
//show success message
$('#result').html("<div id='message'></div>");
$('#message').html("<h2> student created</h2>").hide();
$('#message').fadeIn('slow').delay(5000).fadeOut('slow');
event.stopPropagation();
},
error: function (xhr, desc, err)
{
alert("error");
}
}) …Run Code Online (Sandbox Code Playgroud) 我有一个Symfony2应用程序,其中包含一个文件类型字段的表单.我需要在那里上传一个学生的图像,所以我帮助了这个文档:如何上传文件
这是我的代码:
控制器:
public function createAction(Request $request)
{
if ($request->isXmlHttpRequest() && !$request->isMethod('POST')) {
throw new HttpException('XMLHttpRequests/AJAX calls must be POSTed');
}
$entity = new Student();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$file = $entity->getPhoto();
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$photoDir = $this->container->getParameter('kernel.root_dir').'/../web/uploads/images';
$file->move($photoDir, $fileName);
$entity->setPhoto($fileName);
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
if ($request->isXmlHttpRequest()) {
return new JsonResponse(array('message' => 'Success!','success' => true), 200);
}
if ($request->isMethod('POST')) {
return new JsonResponse(array('message' => 'Invalid form','success' => false), 400);
}
return $this->redirect($this->generateUrl('student_show', array('id' => $entity->getId()))); …Run Code Online (Sandbox Code Playgroud) 我有一个Symfony应用程序,其中包含一个创建睫毛的菜单,并使用.js文件加载到div中,使用load()方法将ulr指向每个菜单选项.此.js是一个变量,它与引用每个创建的选项卡相反.在这个例子中可以看到这个想法:
http://jsfiddle.net/JMMS_85/o7610t24/
$("#tabs-"+tabCounter).load($(this).attr('href'));
Run Code Online (Sandbox Code Playgroud)
加载到div中的URL有其他链接,所以我创建了另一个.js文件来阻止原始链接"preventDefault()",我必须在它之前重新加载这些链接div,即显示新的url链接在同一个div中,所以我必须使用变量计数器上的另一个.js文件来知道选择的实际div是什么.
file1.js
$(document).ready(function () {
$("#tabs").tabs();
var tabCounter = 1;
var cont =1;
//Code creating the tabs omitted
$(".container div a").on("click",function(event) {
event.preventDefault();
tabTitle = $(this).attr("title"),
addTab();
$("#tabs-"+tabCounter).load($(this).attr('href'));
tabCounter++;
$("#tabs").tabs({active: $('.ui-tabs-nav li:last').index()});
});
});
Run Code Online (Sandbox Code Playgroud)
file2.js
$(document).ready(function () {
$("a").click( function(event)
{
event.preventDefault();
$("#tabs-"+tabCounter).load(this.href);
});
Run Code Online (Sandbox Code Playgroud)
我的问题是如何将计数器变量第一个.js文件用于另一个文件.
我在 Symfony 中有一个带有选项卡的应用程序,并使用 Ajax 异步加载,这样我就可以在应用程序中一次使用两个表单。我有一个用于注册用户的表格和另一个用于更新用户数据的表格。在更新表单中,Symfony2通过生成表单来创建用户(UserType),因此表单元素的id是相同的。
另一方面,我有一个验证表单元素的函数,但是当它验证其中一个表单时,也会验证另一个表单是否具有相同的 id 元素,因此没有正确验证每个表单。
例如,使用 Twig
{{form_row (edit_form.name)}}生成一个输入id = "user_name",我需要将其修改为更新形式 as id = "update_user_name"。
那有什么办法可以改变id属性吗?