ibrows / xeditable-bundle
bootstrap x-editable symfony2 表单集成
1.2.0
2016-10-04 08:58 UTC
Requires
- symfony/symfony: ~3.0
README
x-editable ( http://vitalets.github.io/x-editable/ ) symfony2 表单集成
安装和设置包
-
在您的 composer.json 中添加 IbrowsXeditableBundle
{ "require": { "ibrows/xeditable-bundle": "~1.0", } }
-
现在运行以下命令让 composer 下载包
$ php composer.phar update ibrows/xeditable-bundle
Composer 将包安装到您的项目
ibrows/xeditable-bundle
目录中。(PSR-4) -
将包添加到您的
AppKernel
类中// app/AppKernerl.php public function registerBundles() { $bundles = array( // ... new Ibrows\XeditableBundle\IbrowsXeditableBundle(), // ... ); // ... }
-
包含 JS 库和 CSS 文件
{% javascripts '@IbrowsXeditableBundle/Resources/public/javascript/bootstrap.editable-1.5.1.js' '@IbrowsXeditableBundle/Resources/public/javascript/xeditable.js' %} <script type="text/javascript" src="{{ asset_url }}"></script> {% endjavascripts %}
{% stylesheets 'bundles/ibrowsxeditable/css/bootstrap-editable.css' %} <link rel="stylesheet" type="text/css" media="screen" href="{{ asset_url }}" /> {% endstylesheets %}
基本用法
获取工厂并将表单包裹在 xeditableFormMapper 中
$factory = $this->get('ibrows_xeditable.mapper.factory'); $xeditableFormMapper = $factory->createFormFromRequest( 'user_xedit', //target route where data would be sent after submit array('user' => $user->getId()), // parameters for the target route $request, // request to get information about the current view, to find forward paramters new UserEditType(), // a form type with a name and a firstName field $user, // form data for the form type array('validation_groups' => arrya('edit_user')) // form options for the form type );
然后在 twig 中使用 xedit_inline_render 函数渲染 xeditableFormMapper
XeditableMapperInterface $mapper, $formPath = null, array $attributes = array(), array $options = array()
{{ xedit_inline_render(xeditableFormMapper, 'name', {'data-emptytext': 'userName'|trans}) }}
{{ xedit_inline_render(xeditableFormMapper, 'firstName', {'data-emptytext': 'firstName'|trans}) }}
在一个动作中保存来自一个表单的所有 xedit 请求
/** * @Route("/xedit/{user}", name="user_xedit") * @param User $user * @param Request $request * @return Response|\Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response|void */ public function xeditAction(User $user, Request $request) { $factory = $this->get('ibrows_xeditable.mapper.factory'); $xeditableFormMapper = $factory->createFormFromRequest( 'user_xedit', //target route where data would be sent after submit array('user' => $user->getId()), // parameters for the target route $request, // request to get information about the current view, to find forward paramters new UserEditType(), // a form type with some fields $user, // form data for the form type array('validation_groups' => array('edit_user')) // form options for the form type ); if ($request->isMethod('POST')) { if (($response = $xeditableFormMapper->handleRequest($request)) instanceof Response) { return $response; } $em = $this->getManagerForClass($user); $em->persist($user); $em->flush(); // after success redirect to view, so frontend can be refreshed return $this->redirectToForwardRoute($request, 'GET'); } // get back view of the handled form, to display error messages return new Response($xeditableFormMapper->renderXeditable($request->get('path'))); }