<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Amicalement Web - Astuces et Bons plans dans le développement web &#187; formatter</title>
	<atom:link href="http://www.amicalement-web.net/tag/formatter/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.amicalement-web.net</link>
	<description>Astuces et bons plans d&#039;un web developpeur</description>
	<lastBuildDate>Wed, 04 Jan 2012 14:54:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Personnaliser le code HTML d&#8217;un widget symfony</title>
		<link>http://www.amicalement-web.net/personnaliser-le-code-html-dun-widget-symfony/2009/06/02/</link>
		<comments>http://www.amicalement-web.net/personnaliser-le-code-html-dun-widget-symfony/2009/06/02/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 11:55:49 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Astuces]]></category>
		<category><![CDATA[astuce]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[formatter]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=303</guid>
		<description><![CDATA[Comme dit le proverbe, billet du mardi, billet symfony&#8230; Bon on va dire que c&#8217;est un nouveau proverbe mais pour le coup, on va continuer cet adage avec au programme aujourd&#8217;hui les possibilités offertes par symfony pour modifier le code HTML construit par ses fameux widgets de son système de formulaire. Plus mon nombre de [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/system-software-update.png" alt="system-software-update" title="system-software-update" width="48" height="48" class="alignleft size-full wp-image-40" /> Comme dit le proverbe, billet du mardi, billet symfony&#8230; Bon on va dire que c&#8217;est un nouveau proverbe mais pour le coup, on va continuer cet adage avec au programme aujourd&#8217;hui les possibilités offertes par symfony pour modifier le code HTML construit par ses fameux widgets de <a  href="http://www.symfony-project.org/forms/1_2/en/" class="extern">son système de formulaire</a>.</p>
<p>Plus mon nombre de projets défile sous Symfony et plus j&#8217;essaye d&#8217;éviter de faire des choses hors framework, mais plutôt de voir comment on pourrait le régler via le framework. Avant, quand un problème se présentait, j&#8217;optais souvent pour une solution alternative quitte à mettre de côté pour une petite fonction la philosophie Symfony. Mais voilà, je l&#8217;aime moi, cette philosophie et au fur et à mesure, j&#8217;ai décidé de mettre de plus en plus les mains dans la cambouis et les yeux dans le code source, pour résoudre mes problèmes.<br />
<span id="more-303"></span><br />
Récemment, j&#8217;étais sur un petit système de sondage très basique qui se présentait donc sous cette forme grâce au complément sfWidgetFormChoice et sfWidgetFormSelectCheckbox:<br />
<img src="http://www.amicalement-web.net/wp-content/uploads/image-49.png" alt="image-49" title="image-49" width="337" height="155" class="alignnone size-full wp-image-304" /></p>
<p>Grâce à un code du genre:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">widgetSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'reponses'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormChoice<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'multiple'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
      	<span style="color: #0000ff;">'expanded'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'choices'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$reponses</span>
      <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>qui me produisait quelque chose comme ça:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;ul class=&quot;radio_list&quot;&gt;
&lt;li&gt;&lt;input type=&quot;checkbox&quot; id=&quot;etude_question_2_reponses_7&quot; value=&quot;7&quot; name=&quot;etude[question_2][reponses][]&quot;/&gt; &lt;label for=&quot;etude_question_2_reponses_7&quot;&gt;Oula&lt;/label&gt;&lt;/li&gt;
&lt;li&gt;&lt;input type=&quot;checkbox&quot; id=&quot;etude_question_2_reponses_8&quot; value=&quot;8&quot; name=&quot;etude[question_2][reponses][]&quot;/&gt; &lt;label for=&quot;etude_question_2_reponses_8&quot;&gt;Ola&lt;/label&gt;&lt;/li&gt;
&lt;li&gt;&lt;input type=&quot;checkbox&quot; id=&quot;etude_question_2_reponses_9&quot; value=&quot;9&quot; name=&quot;etude[question_2][reponses][]&quot;/&gt; &lt;label for=&quot;etude_question_2_reponses_9&quot;&gt;Woot&lt;/label&gt;&lt;/li&gt;
&lt;li&gt;&lt;input type=&quot;checkbox&quot; id=&quot;etude_question_2_reponses_10&quot; value=&quot;10&quot; name=&quot;etude[question_2][reponses][]&quot;/&gt; &lt;label for=&quot;etude_question_2_reponses_10&quot;&gt;Wagli&lt;/label&gt;&lt;/li&gt;
&lt;li&gt;&lt;input type=&quot;checkbox&quot; id=&quot;etude_question_2_reponses_11&quot; value=&quot;11&quot; name=&quot;etude[question_2][reponses][]&quot;/&gt; &lt;label for=&quot;etude_question_2_reponses_11&quot;&gt;Plop&lt;/label&gt;&lt;/li&gt;
&lt;/ul&gt;</pre></div></div>

<p>Ca fonctionne parfaitement, mais voilà à l&#8217;intégration, j&#8217;avais 2 checkbox par ligne&#8230; Et pour réaliser ça, j&#8217;avais besoin de modifier le code HTML que Symfony m&#8217;avait gentiment généré.</p>
<p>Et donc là, je me suis lancé dans une session de formatting grâce au sfFormatter. En effet, Symfony intègre en règle générale un code HTML par défaut, pour un peu tout, mais nous laisse parfaitement la possibilité de le modifier. Ici, je veux juste pouvoir rajouter une class CSS différente pour les éléments pair et impair. On va donc réécrire la fonction de formatting du widget comme ceci:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">widgetSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'reponses'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormChoice<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'multiple'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
      	<span style="color: #0000ff;">'expanded'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'choices'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$reponses</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'renderer_options'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'formatter'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'formatter'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Ici on passe l'option formatter avec comme valeur la fonction formatter que l'on va ajouter à notre formulaire</span>
      <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Puis maintenant, on ajoute la fameuse fonction. J&#8217;ai choisi ici de la laisser dans mon formulaire, on peut très bien la mettre où on veut:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> formatter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$widget</span><span style="color: #339933;">,</span> <span style="color: #000088;">$inputs</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$rows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$inputs</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$widget</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">renderContentTag</span><span style="color: #009900;">&#40;</span>
        	  <span style="color: #0000ff;">'li'</span><span style="color: #339933;">,</span> 
                  <span style="color: #000088;">$input</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'input'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$widget</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'label_separator'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'label'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
                  <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">%</span><span style="color:#800080;">2</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span>?<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pair'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'impair'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//Seule réelle modification par rapport à la fonction définie de base dans la classe sfWidgetFormSelectCheckbox</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$widget</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">renderContentTag</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ul'</span><span style="color: #339933;">,</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$widget</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'separator'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rows</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'radio_list'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Et voilà mon code HTML ressemble maintenant à:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;ul class=&quot;radio_list&quot;&gt;
&lt;li class=&quot;impair&quot;&gt;&lt;input type=&quot;checkbox&quot; id=&quot;etude_question_2_reponses_7&quot; value=&quot;7&quot; name=&quot;etude[question_2][reponses][]&quot;/&gt; &lt;label for=&quot;etude_question_2_reponses_7&quot;&gt;Oula&lt;/label&gt;&lt;/li&gt;
&lt;li class=&quot;pair&quot;&gt;&lt;input type=&quot;checkbox&quot; id=&quot;etude_question_2_reponses_8&quot; value=&quot;8&quot; name=&quot;etude[question_2][reponses][]&quot;/&gt; &lt;label for=&quot;etude_question_2_reponses_8&quot;&gt;Ola&lt;/label&gt;&lt;/li&gt;
&lt;li class=&quot;impair&quot;&gt;&lt;input type=&quot;checkbox&quot; id=&quot;etude_question_2_reponses_9&quot; value=&quot;9&quot; name=&quot;etude[question_2][reponses][]&quot;/&gt; &lt;label for=&quot;etude_question_2_reponses_9&quot;&gt;Woot&lt;/label&gt;&lt;/li&gt;
&lt;li class=&quot;pair&quot;&gt;&lt;input type=&quot;checkbox&quot; id=&quot;etude_question_2_reponses_10&quot; value=&quot;10&quot; name=&quot;etude[question_2][reponses][]&quot;/&gt; &lt;label for=&quot;etude_question_2_reponses_10&quot;&gt;Wagli&lt;/label&gt;&lt;/li&gt;
&lt;li class=&quot;impair&quot;&gt;&lt;input type=&quot;checkbox&quot; id=&quot;etude_question_2_reponses_11&quot; value=&quot;11&quot; name=&quot;etude[question_2][reponses][]&quot;/&gt; &lt;label for=&quot;etude_question_2_reponses_11&quot;&gt;Plop&lt;/label&gt;&lt;/li&gt;
&lt;/ul&gt;</pre></div></div>

<p>Je peux maintenant appliquer mon CSS pour faire la modification demandée à l&#8217;origine! Cette astuce fonctionne pour tous les widgets qui embarque du html autre que le simple tag input.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/personnaliser-le-code-html-dun-widget-symfony/2009/06/02/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
