PageSourceSearch

https://spkinger.github.io/2016/07/28/symfony-form/

html spkinger.github.io collected 2026-10-03 09:58:49 UTC 6,956 bytes, 262 lines download raw bytes

1<!DOCTYPE html>
2<html>
3<head>
4    <meta charset="utf-8">
5    <meta http-equiv="X-UA-Compatible" content="chrome=1">
6
7    
8
9    <title>
10      Symfony 笔记3--表单 | spkinger&#39;s blog 
11    </title>
12
13    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
14    
15      <meta name="author" content="spkinger">
16    
17    
18
19    <meta name="description" content="Symfony的表单部分">
20<meta property="og:type" content="article">
21<meta property="og:title" content="Symfony 笔记3--表单 | spkinger's blog">
22<meta property="og:url" content="http://spkinger.github.io/2016/07/28/symfony-form/index.html">
23<meta property="og:site_name" content="spkinger's blog">
24<meta property="og:description" content="Symfony的表单部分">
25<meta property="og:updated_time" content="2016-07-28T09:08:45.286Z">
26<meta name="twitter:card" content="summary">
27<meta name="twitter:title" content="Symfony 笔记3--表单 | spkinger's blog">
28<meta name="twitter:description" content="Symfony的表单部分">
29    
30    
31    
32      <link rel="icon" type="image/x-icon" href="/favicon.png">
33    
34    <link rel="stylesheet" href="/css/uno.css">
35    <link rel="stylesheet" href="/css/highlight.css">
36    <link rel="stylesheet" href="/css/archive.css">
37    <link rel="stylesheet" href="/css/china-social-icon.css">
38
39</head>
40<body>
41
42    <span class="mobile btn-mobile-menu">
43        <i class="icon icon-list btn-mobile-menu__icon"></i>
44        <i class="icon icon-x-circle btn-mobile-close__icon hidden"></i>
45    </span>
46
47    
48
49<header class="panel-cover panel-cover--collapsed">
50
51
52  <div class="panel-main">
53
54  
55    <div class="panel-main__inner panel-inverted">
56    <div class="panel-main__content">
57
58        
59
60        <h1 class="panel-cover__title panel-title"><a href="/" title="link to homepage">spkinger&#39;s blog</a></h1>
61        <hr class="panel-cover__divider" />
62
63        
64
65        <div class="navigation-wrapper">
66
67          <nav class="cover-navigation cover-navigation--primary">
68            <ul class="navigation">
69
70              
71                
72                <li class="navigation__item"><a href="/#blog" title="" class="blog-button">首页</a></li>
73              
74                
75                <li class="navigation__item"><a href="/about" title="" class="">关于</a></li>
76              
77                
78                <li class="navigation__item"><a href="/archive" title="" class="">归档</a></li>
79              
80
81            </ul>
82          </nav>
83
84          <!-- ----------------------------
85To add a new social icon simply duplicate one of the list items from below
86and change the class in the <i> tag to match the desired social network
87and then add your link to the <a>. Here is a full list of social network
88classes that you can use:
89
90    icon-social-500px
91    icon-social-behance
92    icon-social-delicious
93    icon-social-designer-news
94    icon-social-deviant-art
95    icon-social-digg
96    icon-social-dribbble
97    icon-social-facebook
98    icon-social-flickr
99    icon-social-forrst
100    icon-social-foursquare
101    icon-social-github
102    icon-social-google-plus
103    icon-social-hi5
104    icon-social-instagram
105    icon-social-lastfm
106    icon-social-linkedin
107    icon-social-medium
108    icon-social-myspace
109    icon-social-path
110    icon-social-pinterest
111    icon-social-rdio
112    icon-social-reddit
113    icon-social-skype
114    icon-social-spotify
115    icon-social-stack-overflow
116    icon-social-steam
117    icon-social-stumbleupon
118    icon-social-treehouse
119    icon-social-tumblr
120    icon-social-twitter
121    icon-social-vimeo
122    icon-social-xbox
123    icon-social-yelp
124    icon-social-youtube
125    icon-social-zerply
126    icon-mail
127
128-------------------------------->
129
130<!-- add social info here -->
131
132
133
134        </div>
135
136      </div>
137
138    </div>
139
140    <div class="panel-cover--overlay"></div>
141  </div>
142</header>
143
144    <div class="content-wrapper">
145        <div class="content-wrapper__inner entry">
146            
147
148<article class="post-container post-container--single">
149
150  <header class="post-header">
151    
152    <h1 class="post-title">Symfony 笔记3--表单</h1>
153
154    
155
156  </header>
157
158  <section id="post-content" class="article-content post">
159    <p>#9.表单生成</p>
160<p>####(1).基本的生成</p>
161<pre><code>#控制器中
162public function indexAction($name, Request $request)
163{
164    $user = new User();
165    $form = $this-&gt;createFormBuilder($user)
166        -&gt;add(&apos;email&apos;,&apos;email&apos;,array(&apos;label&apos;=&gt;&apos;邮箱&apos;))
167        -&gt;add(&apos;password&apos;)
168        -&gt;add(&apos;age&apos;)
169        -&gt;add(&apos;提交&apos;,&apos;submit&apos;)
170        -&gt;getForm();
171
172    $form-&gt;handleRequest($request);
173
174    if($form-&gt;isValid()){
175        $em = $this-&gt;getDoctrine()-&gt;getManager();
176        $em-&gt;persist($user);
177        $em-&gt;flush();
178    }
179
180    return array(&apos;content&apos;=&gt;$name, &apos;form&apos;=&gt;$form-&gt;createView());
181}
182#视图中
183{{ form(form) }}
184</code></pre><p>####(2).FORM模板的修改</p>
185<pre><code>{ { form(form) } }可扩展为
186
187{ { form_start(form) } }
188{ { form_widget(form) } }
189{ { form_end(form) } }
190
191{ { form_start(form) } }
192&lt;!--这里有多个--&gt;
193{ { form_label(form.email) } }
194{ { form_errors(form.email) } }
195{ { form_widget(form.email) } }
196
197{ { form_end(form) } }
198</code></pre><p>####(3).选项模板的修改</p>
199<pre><code>#可修改下面这种每个单元
200{ % block email_widget -% }
201    { % set type = type|default(&apos;email&apos;) -% }
202    { { block(&apos;form_widget_simple&apos;) } }
203{ % endblock email_widget % }
204
205#对模板的修改可以放到view/form/my_form_widget.html.twig然后通过下面代码引入
206{ % form_theme from [&apos;xxxBundle:Form:my_form_widget.html.twig&apos;] % }
207</code></pre>
208  </section>
209
210  
211  
212</article>
213
214
215            <footer class="footer">
216
217    <span class="footer__copyright">&copy; 2014-2015. | 由<a href="https://hexo.io/">Hexo</a>强力驱动 | 主题<a href="https://github.com/someus/huno">Huno</a></span>
218    
219</footer>
220        </div>
221    </div>
222
223    <!-- js files -->
224    
224<script src="/js/jquery.min.js"></script>
224
225    
225<script src="/js/main.js"></script>
225
226    
226<script src="/js/scale.fix.js"></script>
226
227    
228
229    
230
231    
231<script type="text/javascript" src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
231
232    
232<script type="text/javascript"> 
233        $(document).ready(function(){
234            MathJax.Hub.Config({ 
235                tex2jax: {inlineMath: [['[latex]','[/latex]'], ['\\(','\\)']]} 
236            });
237        });
238    </script>
238
239
240
241    
242
243    
243<script src="/js/awesome-toc.min.js"></script>
243
244    
244<script>
245        $(document).ready(function(){
246            $.awesome_toc({
247                overlay: true,
248                contentId: "post-content",
249            });
250        });
251    </script>
251
252
253
254    
255    
256    <!--kill ie6 -->
257<!--[if IE 6]>
258  
258<script src="//letskillie6.googlecode.com/svn/trunk/2/zh_CN.js"></script>
258
259<![endif]-->
260
261</body>
262</html>

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.