enter image description here変数 "url_resources"は存在しません。ここで私はちょうど上記の入力フォームの値を表示しようとしています。それは常に私に同じエラーを引き出す。別のページに移動せずに同じページに表示したい。私はあなたが小枝のコードで見ることができるように適切なfielsを表示するための3つのテキストボックスを作成しました。もちろん変数 "url_resources"が存在しません。ここで私はちょうど上記の入力フォームの値を表示しようとしています
コントローラ
<?php
namespace UrlResourceBundle\Controller;
use UrlResourceBundle\Entity\url_resources;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class DefaultController extends Controller
{
/**
* @Route("/", name="todo_list")
*/
public function listAction()
{
$urls_resources = $this->getDoctrine()
->getRepository('UrlResourceBundle:url_resources')
->findAll();
return $this->render('todo/index.html.twig', array(
'urls_resources' => $urls_resources
));
}
/**
* @Route("/todo/create", name="todo_create")
*/
public function createAction(Request $request)
{
$url_resources = new url_resources;
$form = $this->createFormBuilder($url_resources)
->add('title', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('url', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('description', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('save', SubmitType::class, array('label' => "Create",'attr' => array('class' => 'btn btn-primary', 'style' => 'margin-bottom:15px')))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
//Get Data
$title = $form['title']->getData();
$url = $form['url']->getData();
$description = $form['description']->getData();
$url_resources->setTitle($title);
$url_resources->setUrl($url);
$url_resources->setDescription($description);
$em = $this->getDoctrine()->getManager();
$em->persist($url_resources);
$em->flush();
$this->addFlash(
'notice',
'Entry Created'
);
return $this->redirectToRoute('todo_create');
return $this->redirect($request->getUri());
}
return $this->render('todo/create.html.twig', array(
'form' => $form->createView()
));
}
/**
* @Route("/todo/edit/{id}", name="todo_edit")
*/
public function editAction($id, Request $request)
{
$url_resources = $this->getDoctrine()
->getRepository('UrlResourceBundle:url_resources')
->find($id);
$url_resources->setTitle($url_resources->getTitle());
$url_resources->setUrl($url_resources->getUrl());
$url_resources->setDescription($url_resources->getDescription());
$form = $this->createFormBuilder($url_resources)
->add('title', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('url', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('description', TextareaType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('save', SubmitType::class, array('label' => "Update",'attr' => array('class' => 'btn btn-primary', 'style' => 'margin-bottom:15px')))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
//Get Data
$title = $form['title']->getData();
$url = $form['url']->getData();
$description = $form['description']->getData();
$em = $this->getDoctrine()->getManager();
$url_resources = $em->getRepository('UrlResourceBundle:url_resources')->find($id);
$url_resources->setTitle($title);
$url_resources->setUrl($url);
$url_resources->setDescription($description);
$em->flush();
$this->addFlash(
'notice',
'Entry Updated'
);
return $this->redirectToRoute('todo_list');
}
return $this->render('todo/edit.html.twig', array(
'url_resources' => $url_resources,
'form' => $form->createView()
));
}
/**
* @Route("/todo/details/{id}", name="todo_details")
*/
public function detailsAction($id)
{
$url_resources = $this->getDoctrine()
->getRepository('UrlResourceBundle:url_resources')
->find($id);
return $this->render('todo/details.html.twig', array(
'url_resources' => $url_resources
));
}
/**
* @Route("/todo/delete/{id}", name="todo_delete")
*/
public function deleteAction($id)
{
$em = $this->getDoctrine()->getManager();
$url_resources = $em->getRepository('UrlResourceBundle:url_resources')->find($id);
$em->remove($url_resources);
$em->flush();
$this->addFlash(
'notice',
'Entry deleted');
return $this->redirectToRoute('todo_list');
}
}
小枝
{% extends 'base.html.twig' %}
{% block body %}
<h2 class="page-header">Create</h2>
{{form_start(form)}}
{{form_widget(form)}}
<ul class="list-group">
<li class="list-group-item">Title: {{url_resources.title}}</li>
<li class="list-group-item">URL: {{url_resources.url}}</li>
<li class="list-group-item">Description: {{url_resources.description}}</li>
</ul>
{{form_end(form)}}
{% endblock %}
あなたは正確なエラーメッセージを伝えることができますか?私たちはどこでそれが起こるか知りません –
私はエラーのスクリーンショットを追加しました... pls質問のトップを参照してください..おはようございます... – jcoder