<?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>Bertrand Mansion &#187; php</title>
	<atom:link href="http://mansion.im/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://mansion.im</link>
	<description>Blog</description>
	<lastBuildDate>Wed, 19 Dec 2012 17:38:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Problems compiling PHP 5.3.19 on Mountain Lion</title>
		<link>http://mansion.im/2012/problems-compiling-php-5-3-19-on-mountain-lion/</link>
		<comments>http://mansion.im/2012/problems-compiling-php-5-3-19-on-mountain-lion/#comments</comments>
		<pubDate>Wed, 19 Dec 2012 17:36:20 +0000</pubDate>
		<dc:creator>Bertrand Mansion</dc:creator>
				<category><![CDATA[mac]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://mansion.im/?p=226</guid>
		<description><![CDATA[I had to add EXTRA_CFLAGS = -lstdc++ -lresolv to Makefile when trying to compile PHP 5.3.19 with: ./configure --prefix=/usr/local --mandir=/usr/share/man --infodir=/usr/share/info --sysconfdir=/private/etc --enable-cli --with-config-file-path=/usr/local/php/etc --with-libxml-dir=/usr --enable-xml --enable-exif --enable-ftp --with-gd --with-jpeg-dir=/usr/local/lib --with-png-dir=/usr/local/lib --enable-gd-native-ttf --with-imap=/usr/local --with-imap-ssl --enable-magic-quotes --enable-mbstring --enable-mbregex --enable-json --with-mysql=mysqlnd --with-mysqli=mysqlnd &#8230; <a href="http://mansion.im/2012/problems-compiling-php-5-3-19-on-mountain-lion/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I had to add <strong>EXTRA_CFLAGS = -lstdc++ -lresolv</strong> to Makefile when trying to compile PHP 5.3.19 with:</p>
<p><code>./configure --prefix=/usr/local --mandir=/usr/share/man --infodir=/usr/share/info --sysconfdir=/private/etc --enable-cli --with-config-file-path=/usr/local/php/etc --with-libxml-dir=/usr --enable-xml --enable-exif --enable-ftp --with-gd --with-jpeg-dir=/usr/local/lib --with-png-dir=/usr/local/lib --enable-gd-native-ttf --with-imap=/usr/local --with-imap-ssl --enable-magic-quotes --enable-mbstring --enable-mbregex --enable-json --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock=/tmp/mysql.sock --with-iodbc=/usr --enable-shmop --enable-sockets --enable-fpm --with-mhash --with-mcrypt --with-xmlrpc --enable-xmlwriter --enable-xmlreader --with-xsl=/usr --enable-zend-multibyte --enable-zip --with-pcre-regex=/usr/local --with-pdo-sqlite --enable-pdo --with-freetype-dir=/usr/local --enable-dom --enable-fileinfo --with-kerberos --with-pgsql=/Applications/Postgres.app/Contents/MacOS --with-pdo-pgsql=/Applications/Postgres.app/Contents/MacOS --with-curl --with-gettext --with-zlib --with-tidy --enable-pcntl --enable-hash --enable-mbregex --with-regex=php --enable-bcmath --enable-sqlite-utf8 --enable-intl<br />
</code><br />
Otherwise I had errors with Intl (ICU) and cUrl together, something like :</p>
<p><code>Undefined symbols for architecture x86_64<br />
_res_9_dn_expand etc...</code></p>
<p>This was on OSX Mountain Lion.<br />
Coworker reports it also works on old Lion :p</p>
]]></content:encoded>
			<wfw:commentRss>http://mansion.im/2012/problems-compiling-php-5-3-19-on-mountain-lion/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Twitter Bootstrap and the QuickForm2 Callback Renderer</title>
		<link>http://mansion.im/2011/twitter-bootstrap-and-the-quickform2-callback-renderer/</link>
		<comments>http://mansion.im/2011/twitter-bootstrap-and-the-quickform2-callback-renderer/#comments</comments>
		<pubDate>Fri, 23 Sep 2011 18:37:31 +0000</pubDate>
		<dc:creator>Bertrand Mansion</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[bootstrap]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[qf2]]></category>
		<category><![CDATA[quickform2]]></category>

		<guid isPermaLink="false">http://mansion.im/?p=172</guid>
		<description><![CDATA[I don&#8217;t know about you, but for me building HTML Forms and styling HTML Forms are maybe the most boring things in web development. It&#8217;s repetitive and takes a lot of time to do things correctly. That&#8217;s why tools like &#8230; <a href="http://mansion.im/2011/twitter-bootstrap-and-the-quickform2-callback-renderer/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t know about you, but for me building HTML Forms and styling HTML Forms are maybe the most boring things in web development. It&#8217;s repetitive and takes a lot of time to do things correctly.</p>
<p>That&#8217;s why tools like Twitter&#8217;s Bootstrap and PEAR&#8217;s HTML_QuickForm2 can help with this part of our job.</p>
<p>Wouldn&#8217;t it be nice to <strong>have QuickForm2 generate a markup compatible with Bootstrap CSS</strong>, so that you could get a nice looking form without to much efforts? Well, that&#8217;s what I plan to do here.<br />
<span id="more-172"></span><br />
<a href="http://twitter.github.com/bootstrap/">Bootstrap</a> is a toolkit from Twitter designed to kickstart development of webapps and sites. It includes base CSS and HTML for typography, forms, buttons, tables, grids, navigation, and more. In this post, I will concentrate on the form stuff.</p>
<p><a href="http://pear.php.net/package/HTML_QuickForm2">QuickForm2</a> on the other hand is a PHP library dedicated to make building HTML forms easier, faster and less error-prone. It is hosted in <a href="http://pear.php.net/">PEAR</a> and has been extensively tested.</p>
<p>First, I will try to build the same form as shown on Bootstrap&#8217;s website, with QuickForm2. Except for the three elements with appended and prepended things, it was easy. I will come back to these elements later, just know it is not difficult to render them neither.</p>
<p>So here is the result :<br />
<strong>Step 1 : </strong><a href="/tests/quickform2-bootstrap.php?step=1">Bootstrap with QuickForm2 and no renderer</a></p>
<p>The most obvious problem is that QuickForm2 doesn&#8217;t use the same markup to build forms as what Bootstrap expects. To solve this, we can use the Callback renderer that comes with QuickForm2 and define our own callback to render our elements the way Bootstrap likes them.</p>
<p>The Callback renderer associates PHP callbacks with form elements using either their type (text, textarea, &#8230;) or their name if you need a more precise control. This means that we can first define how our form object should be rendered, as well as our Checkboxes, Textareas, etc. Adding a Callback renderer and specifying a function to render an element is as simple as that :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span class="re0">$r</span> <span class="sy0">=</span> HTML_QuickForm2_Renderer<span class="sy0">::</span><span class="me2">factory</span><span class="br0">&#40;</span><span class="st_h">'callback'</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="re0">$r</span><span class="sy0">-&gt;</span><span class="me1">setCallbackForClass</span><span class="br0">&#40;</span><span class="st_h">'HTML_QuickForm2_Element'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$renderer</span><span class="sy0">,</span> <span class="re0">$element</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$error</span> <span class="sy0">=</span> <span class="re0">$element</span><span class="sy0">-&gt;</span><span class="me1">getError</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$error</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="re0">$html</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'&lt;div class=&quot;clearfix error&quot;&gt;'</span><span class="sy0">;</span>
        <span class="re0">$element</span><span class="sy0">-&gt;</span><span class="me1">addClass</span><span class="br0">&#40;</span><span class="st_h">'error'</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
        <span class="re0">$html</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'&lt;div class=&quot;clearfix&quot;&gt;'</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
    <span class="re0">$html</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="re0">$renderer</span><span class="sy0">-&gt;</span><span class="me1">renderLabel</span><span class="br0">&#40;</span><span class="re0">$element</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="re0">$html</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'&lt;div class=&quot;input&quot;&gt;'</span><span class="sy0">.</span><span class="re0">$element</span><span class="sy0">;</span>
    <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$error</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="re0">$html</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'&lt;span class=&quot;help-inline&quot;&gt;'</span><span class="sy0">.</span><span class="re0">$error</span><span class="sy0">.</span><span class="st_h">'&lt;/span&gt;'</span><span class="sy0">;</span>
    <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
        <span class="re0">$label</span> <span class="sy0">=</span> <span class="re0">$element</span><span class="sy0">-&gt;</span><span class="me1">getLabel</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw3">is_array</span><span class="br0">&#40;</span><span class="re0">$label</span><span class="br0">&#41;</span> <span class="sy0">&amp;&amp;</span> <span class="sy0">!</span><span class="kw3">empty</span><span class="br0">&#40;</span><span class="re0">$label</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="re0">$html</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'&lt;span class=&quot;help-block&quot;&gt;'</span><span class="sy0">.</span><span class="re0">$label</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="sy0">.</span><span class="st_h">'&lt;/span&gt;'</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
    <span class="br0">&#125;</span>
    <span class="re0">$html</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'&lt;/div&gt;&lt;/div&gt;'</span><span class="sy0">;</span>
    <span class="kw1">return</span> <span class="kw3">implode</span><span class="br0">&#40;</span><span class="st_h">''</span><span class="sy0">,</span> <span class="re0">$html</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw1">echo</span> <span class="re0">$form</span><span class="sy0">-&gt;</span><span class="me1">render</span><span class="br0">&#40;</span><span class="re0">$r</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>Just adding the above to our form will already make it look a lot better with Bootstrap. There are some refinements we can add for the rest of our elements, this is shown here :<br />
<strong>Step 2 :</strong> <a href="/tests/quickform2-bootstrap.php?step=2">Defining callbacks to render elements</a></p>
<p>As you may notice, when I have to deal with special elements or group of elements, as is the case with Bootstrap and its action buttons for example, I usually just create a dumb subclass and define a specific callback to render it. For example, to render the form buttons:</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span class="co1">// Callback for action inputs</span>
&nbsp;
<span class="re0">$r</span><span class="sy0">-&gt;</span><span class="me1">setCallbackForClass</span><span class="br0">&#40;</span><span class="st_h">'ActionInputs'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$renderer</span><span class="sy0">,</span> <span class="re0">$group</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$html</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'&lt;div class=&quot;actions&quot;&gt;'</span><span class="sy0">;</span>
    <span class="re0">$elements</span> <span class="sy0">=</span> <span class="kw3">array_pop</span><span class="br0">&#40;</span><span class="re0">$renderer</span><span class="sy0">-&gt;</span><span class="me1">html</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="re0">$html</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="kw3">implode</span><span class="br0">&#40;</span><span class="st_h">' '</span><span class="sy0">,</span> <span class="re0">$elements</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="re0">$html</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'&lt;/div&gt;'</span><span class="sy0">;</span>
    <span class="kw1">return</span> <span class="kw3">implode</span><span class="br0">&#40;</span><span class="st_h">''</span><span class="sy0">,</span> <span class="re0">$html</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Callback for elements inside an action inputs group</span>
&nbsp;
<span class="re0">$r</span><span class="sy0">-&gt;</span><span class="me1">setElementCallbackForGroupClass</span><span class="br0">&#40;</span><span class="st_h">'ActionInputs'</span><span class="sy0">,</span>
    <span class="st_h">'HTML_QuickForm2_Element'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$renderer</span><span class="sy0">,</span> <span class="re0">$element</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$html</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="br0">&#40;</span>string<span class="br0">&#41;</span><span class="re0">$element</span><span class="sy0">;</span>
    <span class="kw1">return</span> <span class="kw3">implode</span><span class="br0">&#40;</span><span class="st_h">''</span><span class="sy0">,</span> <span class="re0">$html</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>This is a very simple example, you can do a lot more in these callbacks, for example add a contextual error message, a label or an help text, join elements with a separator, etc.</p>
<p>There are, of course, many ways to improve this renderer and expand it to make it even more reusable. I personally find the Bootstrap CSS a little limited, for example it&#8217;s missing the following features I often use :</p>
<ul>
<li>Differentiate required elements</li>
<li>Allow elements in inline inputs groups to have labels</li>
<li>Allow checkboxes and radios to be displayed inline</li>
<li>Use a standard text link instead of a button for cancel (better for redirects)</li>
<li>Allow elements in inline inputs groups to have contextual errors</li>
</ul>
<p>For the above, I have made my own <a href="http://pear.php.net/package/HTML_QuickForm2">QuickForm2</a> renderer and custom CSS, but if you know of any other good looking and interesting <strong>CSS framework</strong> for styling forms, please let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://mansion.im/2011/twitter-bootstrap-and-the-quickform2-callback-renderer/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>PHP with Intl and Gettext on OSX Lion</title>
		<link>http://mansion.im/2011/php-with-intl-and-gettext-on-osx-lion/</link>
		<comments>http://mansion.im/2011/php-with-intl-and-gettext-on-osx-lion/#comments</comments>
		<pubDate>Sat, 06 Aug 2011 19:23:11 +0000</pubDate>
		<dc:creator>Bertrand Mansion</dc:creator>
				<category><![CDATA[mac]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://mansion.im/?p=154</guid>
		<description><![CDATA[Mac OSX Lion ships with PHP 5.3.6. There are also other ways to get PHP working on Lion, for example: Liip distributes a full featured PHP 5.3 binary for Lion Zend has its Zend Server Community Edition MacPorts Homebrew These &#8230; <a href="http://mansion.im/2011/php-with-intl-and-gettext-on-osx-lion/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Mac OSX Lion ships with PHP 5.3.6. There are also other ways to get PHP working on Lion, for example:</p>
<ul>
<li><a href="http://php-osx.liip.ch/">Liip</a> distributes a full featured PHP 5.3 binary for Lion</li>
<li><a href="http://www.zend.com/en/products/server-ce/">Zend</a> has its Zend Server Community Edition</li>
<li><a href="http://www.macports.org">MacPorts</a></li>
<li><a href="https://github.com/mxcl/homebrew">Homebrew</a></li>
</ul>
<p>These are all very good solutions, but since I prefer to travel light and the version in Lion already comes with a lot of useful extensions, I preferred to go with it.<br />
<span id="more-154"></span><br />
Here is the list of extensions installed with Lion:</p>
<blockquote><p>bcmath, bz2, calendar, Core, ctype, curl, date, dba, dom, ereg, exif, fileinfo, filter, ftp, gd, hash, iconv, json, ldap, libxml, mbstring, mysql, mysqli, mysqlnd, odbc, openssl, pcre, PDO, pdo_mysql, pdo_pgsql, pdo_sqlite, pgsql, Phar, posix, readline, Reflection, session, shmop, SimpleXML, snmp, soap, sockets, SPL, SQLite, sqlite3, standard, sysvmsg, sysvsem, sysvshm, tidy, tokenizer, wddx, xdebug, xml, xmlreader, xmlrpc, xmlwriter, xsl, zip, zlib
</p></blockquote>
<p>In /etc, there is a <code>php.ini.default</code> file that is not activated, so you will have to modify it to suit your preferences and rename it to /etc/php.ini. For instance, Lion also ships with <a href="http://xdebug.org">Xdebug</a>, but it is not activated by default. To activate it, you&#8217;ll have to remove the semi-colon on the line that says:</p>

<div class="wp_syntax"><div class="code"><pre class="ini"><span class="re1">zend_extension</span><span class="sy0">=</span><span class="st0">&quot;/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so&quot;</span></pre></div></div>

<p>Lion doesn&#8217;t provide neither <a href="http://pear.php.net">PEAR</a> nor <a href="http://pecl.php.net">PECL</a> by default but it is easy to activate them because Apple provide the required php archive file (phar) for that:</p>

<div class="wp_syntax"><div class="code"><pre class="shell">cd /usr/lib/php
sudo php install-pear-nozlib.phar</pre></div></div>

<p>Then edit the php.ini file and add:</p>

<div class="wp_syntax"><div class="code"><pre class="ini"><span class="re1">include_path</span> <span class="sy0">=</span> <span class="st0">&quot;.:/usr/lib/php/pear&quot;</span>
<span class="re1">extension_dir</span> <span class="sy0">=</span> <span class="st0">&quot;/usr/lib/php/extensions/no-debug-non-zts-20090626&quot;</span></pre></div></div>

<p>Then you can update your PEAR/PECL channels:</p>

<div class="wp_syntax"><div class="code"><pre class="shell">sudo pear channel-update pear.php.net
sudo pecl channel-update pecl.php.net
sudo pear upgrade-all</pre></div></div>

<p>If like me you are missing the <a href="http://www.php.net/manual/fr/book.intl.php">Intl extension</a>, here is how I installed it (warning : <strong>requires Xcode</strong>):</p>
<ol>
<li>Download and install <a href="http://site.icu-project.org/download/48#ICU4C-Download">ICU</a>

<div class="wp_syntax"><div class="code"><pre class="shell">tar xzvf icu4c-4_8_1-src.tgz
cd icu/source
./runConfigureICU MacOSX
make
sudo make install</pre></div></div>

</li>
<li>Download <a href="http://www.php.net/get/php-5.3.6.tar.gz/from/a/mirror">PHP 5.3.6 sources</a> (update: I tried with 5.3.8, I got a compilation error, so use 5.3.6)</li>
<li>Unpack</li>
<li>Change directory to <code>ext/intl</code></li>
<li>Use the following commands in Terminal:

<div class="wp_syntax"><div class="code"><pre class="shell">cd ext/intl
phpize
./configure --enable-intl
make
sudo cp modules/intl.so /usr/lib/php/extensions/no-debug-non-zts-20090626/</pre></div></div>

</li>
<li>Activate intl in your php.ini file with

<div class="wp_syntax"><div class="code"><pre class="ini"><span class="re1">extension</span><span class="sy0">=</span><span class="re2">intl.so</span></pre></div></div>

</li>
</ol>
<p>Since I mostly work on multilingual sites, I also needed to install <a href="http://ftp.gnu.org/gnu/gettext/">Gettext</a> (it&#8217;s unfortunate that Apple will not ship <em>gettext</em> and <em>intl</em> by default, a lot of PHP developers use more than English, but that might be another licence issue&#8230;). The problem is that Gettext apparently defines <code>stpncpy</code> function, as does something in Lion&#8230;</p>
<p>As suggested by <a href="http://stephenyeargin.com/">Stephen Yeargin</a> in the comments, you can apply a <a href="http://code.google.com/p/clementine-player/source/browse/macosx/patches/gettext-01-stpncpy.patch?repo=dependencies&#038;r=16cfe841ae227624eb1c3d195310c2a828203ac1" target="_blank" title="Patch for Gettext to compile on OSX Lion">small patch on Gettext</a> that will modify the <em>configure</em> script to make sure function <code>stpncpy</code> is not already defined.</p>

<div class="wp_syntax"><div class="code"><pre class="shell">tar xzvf gettext-0.18.1.1.tar.gz
cd gettext-0.18.1.1
# apply patch here, then :
./configure
make
sudo make install</pre></div></div>

<p>Then, back in the PHP sources directory:</p>

<div class="wp_syntax"><div class="code"><pre class="shell">cd ext/gettext
phpize
./configure --with-gettext
make
sudo cp modules/gettext.so /usr/lib/php/extensions/no-debug-non-zts-20090626/</pre></div></div>

<p>And add this to the php.ini file:</p>

<div class="wp_syntax"><div class="code"><pre class="ini"><span class="re1">extension</span><span class="sy0">=</span><span class="re2">gettext.so</span></pre></div></div>

</li>
<p>Finally I just restarted Apache from the Sharing Preference pane and I&#8217;m ready to start coding.</p>
<p><b>Updates 02/24/2012:</b> Don&#8217;t use 5.3.8. Use the patch suggested in the comments for Gettext to compile.</p>
]]></content:encoded>
			<wfw:commentRss>http://mansion.im/2011/php-with-intl-and-gettext-on-osx-lion/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Getting started with HTML_QuickForm2</title>
		<link>http://mansion.im/2011/getting-started-with-html_quickform2/</link>
		<comments>http://mansion.im/2011/getting-started-with-html_quickform2/#comments</comments>
		<pubDate>Sun, 05 Jun 2011 17:49:40 +0000</pubDate>
		<dc:creator>Bertrand Mansion</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[qf2]]></category>
		<category><![CDATA[quickform2]]></category>

		<guid isPermaLink="false">http://mansion.im/?p=58</guid>
		<description><![CDATA[QuickForm 2 is a PHP library which makes the development of complex and modern HTML forms easier and faster. It is the natural successor of QuickForm with a new revamped API, PHP5 support, new features and less bloat. Its development &#8230; <a href="http://mansion.im/2011/getting-started-with-html_quickform2/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://pear.php.net/package/HTML_QuickForm2">QuickForm 2</a> is a PHP library which makes the development of complex and modern HTML forms easier and faster. It is the natural successor of <a href="http://pear.php.net/package/HTML_QuickForm">QuickForm</a> with a new revamped API, PHP5 support, new features and less bloat. Its development was started in 2007 by <strong>Alexey Borzov</strong> and <strong>Bertrand Mansion</strong>, and we are very close to a first beta release. I already use QuickForm 2 in production on a dozen of sites and since its code is almost fully unit tested, it is very stable.<br />
<span id="more-58"></span><br />
In this post, I propose to create a <strong>simple contact form</strong> using QuickForm 2. It will have to contain some of the most useful form elements like <a href="http://pear.php.net/package/HTML_QuickForm2/docs/latest/HTML_QuickForm2/HTML_QuickForm2_Element_InputText.html">text fields</a>, <a href="http://pear.php.net/package/HTML_QuickForm2/docs/latest/HTML_QuickForm2/HTML_QuickForm2_Element_InputSubmit.html">submit buttons</a>, etc. There won&#8217;t be any complex <a href="http://pear.php.net/manual/en/package.html.html-quickform2.rules.php">validation rules</a>, since this is not our goal yet (although it might become the topic of another tutorial). We will use a filter to automatically <em>trim</em> our element values after they are submitted. And then, we will process our form.</p>
<h2>Installing HTML_QuickForm2</h2>
<p>QuickForm 2 is a <a href="http://pear.php.net">PEAR</a> package, so it can be easily installed using the following command from your command line:</p>
<pre>
pear install -f html_quickform2
</pre>
<p>This will also install the HTML_Common2 which is required by HTML_QuickForm2.</p>
<h2>Form components</h2>
<p><img src="http://mansion.im/wp-content/uploads/2011/06/qf2-rocks.jpg" alt="" title="qf2-rocks" width="650" height="330" class="alignnone size-full wp-image-121" /></p>
<p>Before we get started with the code, there are some concepts you should know that will make your work with QuickForm 2 easier. More specifically, it is important to understand what makes a form :</p>
<ul class="items">
<li><strong>Elements :</strong> Elements are the form widgets that receive input from the form users. In QuickForm 2, elements are text fields, textareas, select boxes, radio buttons, checkboxes, buttons, etc. In most cases, the elements are descendant of the <code><a href="http://pear.php.net/package/HTML_QuickForm2/docs/latest/HTML_QuickForm2/HTML_QuickForm2_Element.html">HTML_QuickForm2_Element</a></code> class. To build a form, basically, you just have to add elements to it.</li>
<li><strong>Labels :</strong> Labels are what helps the users know what the input is expecting. Labels are usually placed in <code>&lt;label&gt;</code> tags although this can be changed by your form renderer. Checkable elements like radio buttons or checkboxes are also treated differently, their label is the text that is displayed next to each one of them. In QuickForm 2, an element can have multiple labels, this makes it easy to add descriptions and help text.</li>
<li><strong>Required elements :</strong> Required elements are checked for contents when they are submitted. They are also marked as such, so the form renderer knows they should be treated differently because the user needs to know he has to fill them.</li>
<li><strong>Groups and fieldsets :</strong> Elements can be grouped in generic containers. This helps organize your form logic and rendering. It is a very powerful feature of QuickForm 2 and is what makes working with it so pleasant (in my opinion). It is also a feature other form packages usually don&#8217;t have, last time I checked. Groups are descendant of the <code><a href="http://pear.php.net/package/HTML_QuickForm2/docs/latest/HTML_QuickForm2/HTML_QuickForm2_Container.html">HTML_QuickForm2_Container</a></code> class. By default, QuickForm 2 provides two containers : <a href="http://pear.php.net/package/HTML_QuickForm2/docs/latest/HTML_QuickForm2/HTML_QuickForm2_Container_Group.html">group</a> and <a href="http://pear.php.net/package/HTML_QuickForm2/docs/latest/HTML_QuickForm2/HTML_QuickForm2_Container_Fieldset.html">fieldset</a>.
</li>
<li><strong>Submit elements :</strong> Submit elements need to be treated differently since they are usually display at the bottom of the form and they don&#8217;t need a label. They can be either right or left aligned. They trigger the submit event of the form which leads to the form validation and processing. By default, QuickForm 2 has a way to identify which form has been submitted, even if you have more than one form on a page. Submit elements can also be buttons and images.</li>
<li><strong>Error messages :</strong> Errors are displayed once the form has be submitted and didn&#8217;t validate. QuickForm 2, thanks to its renderers flexibility, allows to group error messages together or display them next to the elements. Client side validation with javascript was added in version 0.5 and it works like HTML5 validation (but without the need for HTML5).</li>
</ul>
<p>These are the basic parts that will usually compose your forms. QuickForm 2 has methods to deal with all of them in a flexible way as we will see now and in future tutorials.</p>
<h2>Build the contact form</h2>
<p>First thing, we&#8217;ll have to define our form object and specify how and where it is supposed to send its values when submitted.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span class="re0">$form</span> <span class="sy0">=</span> <span class="kw2">new</span> HTML_QuickForm2<span class="br0">&#40;</span><span class="st_h">'ContactForm'</span><span class="sy0">,</span> <span class="st_h">'post'</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span>
    <span class="st_h">'action'</span> <span class="sy0">=&gt;</span> <span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st_h">'PHP_SELF'</span><span class="br0">&#93;</span>
<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>Notice that we used <code>PHP_SELF</code> as url for processing our form. It is usually a good habit to use an url that will process our form and then redirect to somewhere else once everything is done, using a <code>header('Location: /other-url')</code>. This technique is called <a href="http://en.wikipedia.org/wiki/Post/Redirect/Get">Post/Redirect/Get</a> and is recommended with QuickForm 2.</p>
<p>Using <code>PHP_SELF</code> is fine because QuickForm 2 needs first to build the form in order to know what the elements and rules are. Most of the time, the form is built in <code>PHP_SELF</code>. The good news is that QuickForm 2 uses <code>PHP_SELF</code> by default so you actually don&#8217;t have to specify the third parameter if that&#8217;s all you need.</p>
<p>Now, let&#8217;s add some form elements.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span class="co1">// Let's start with a fieldset</span>
<span class="re0">$fs</span> <span class="sy0">=</span> <span class="re0">$form</span><span class="sy0">-&gt;</span><span class="me1">addFieldset</span><span class="br0">&#40;</span><span class="st_h">'contact_us'</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">setLabel</span><span class="br0">&#40;</span><span class="st0">&quot;Contact us&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Add a title select box</span>
<span class="re0">$title</span> <span class="sy0">=</span> <span class="re0">$fs</span><span class="sy0">-&gt;</span><span class="me1">addSelect</span><span class="br0">&#40;</span><span class="st_h">'title'</span><span class="br0">&#41;</span>
             <span class="sy0">-&gt;</span><span class="me1">setLabel</span><span class="br0">&#40;</span><span class="st0">&quot;Title : &quot;</span><span class="br0">&#41;</span>
             <span class="sy0">-&gt;</span><span class="me1">loadOptions</span><span class="br0">&#40;</span><span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">''</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;Select your title...&quot;</span><span class="sy0">,</span> <span class="st0">&quot;Mr.&quot;</span><span class="sy0">,</span> <span class="st0">&quot;Ms&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Add a name text field</span>
<span class="re0">$name</span> <span class="sy0">=</span> <span class="re0">$fs</span><span class="sy0">-&gt;</span><span class="me1">addText</span><span class="br0">&#40;</span><span class="st_h">'name'</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">setLabel</span><span class="br0">&#40;</span><span class="st0">&quot;Name : &quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Add a comment textarea with 6 rows</span>
<span class="re0">$comment</span> <span class="sy0">=</span> <span class="re0">$fs</span><span class="sy0">-&gt;</span><span class="me1">addTextarea</span><span class="br0">&#40;</span><span class="st_h">'comment'</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'rows'</span> <span class="sy0">=&gt;</span> <span class="nu0">6</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
                      <span class="sy0">-&gt;</span><span class="me1">setLabel</span><span class="br0">&#40;</span><span class="st0">&quot;Comments : &quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Add the submit button to our form (outside of our fieldset)</span>
<span class="re0">$form</span><span class="sy0">-&gt;</span><span class="me1">addSubmit</span><span class="br0">&#40;</span><span class="st_h">'submit'</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'value'</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;Send us your comments&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>This is a lot like playing with HTML directly. You specify element attributes in the second parameter and they get added to your element tag. Shall we later enforce a <code>maxlength</code> for our text field, it would be as easy as :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span class="re0">$name</span><span class="sy0">-&gt;</span><span class="me1">setAttribute</span><span class="br0">&#40;</span><span class="st_h">'maxlength'</span><span class="sy0">,</span> <span class="nu0">64</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="re0">$title</span><span class="sy0">-&gt;</span><span class="me1">setAttribute</span><span class="br0">&#40;</span><span class="st_h">'style'</span><span class="sy0">,</span> <span class="st_h">'width:200px'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>That&#8217;s because all our elements inherit from <a href="http://pear.php.net/package/HTML_Common2/">HTML_Common2</a> which does that for us.</p>
<h2>Validation</h2>
<p>All our fields should be required so we have to specify it.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span class="re0">$title</span><span class="sy0">-&gt;</span><span class="me1">addRule</span><span class="br0">&#40;</span><span class="st0">&quot;required&quot;</span><span class="sy0">,</span> <span class="st0">&quot;Please specify your title&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="re0">$name</span><span class="sy0">-&gt;</span><span class="me1">addRule</span><span class="br0">&#40;</span><span class="st0">&quot;required&quot;</span><span class="sy0">,</span> <span class="st0">&quot;Please specify your name&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="re0">$comment</span><span class="sy0">-&gt;</span><span class="me1">addRule</span><span class="br0">&#40;</span><span class="st0">&quot;required&quot;</span><span class="sy0">,</span> <span class="st0">&quot;Please enter some comments&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>Upon user submission, QuickForm 2 will check that these fields have been filled. But before we actually call the <code>validate()</code> method, let&#8217;s make sure that the submitted values are not only spaces or newlines. In order to achieve this, we&#8217;ll trim all our form values :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span class="co1">// Apply the trim() function recursively on all form children elements</span>
<span class="re0">$form</span><span class="sy0">-&gt;</span><span class="me1">addRecursiveFilter</span><span class="br0">&#40;</span><span class="st_h">'trim'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>By default, QuickForm 2 uses what we call <em>intrinsic validation</em> on the server-side. It just means that user cannot submit non-existent values from your select box (it can be turned off). Let&#8217;s validate the form and output it to the browser :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$form</span><span class="sy0">-&gt;</span><span class="me1">validate</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span class="kw1">echo</span> <span class="st0">&quot;Success&quot;</span><span class="sy0">;</span> <span class="co1">// Use a redirect here...</span>
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
  <span class="kw1">echo</span> <span class="re0">$form</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>Thanks to a hidden element, QuickForm 2 knows when the form needs to be validated and will not try to validate it if unnecessary.</p>
<p>The form can be output with a simple <code>echo</code> because like all other QuickForm 2 elements, the <code>__toString()</code> method is implemented.</p>
<h2>Conclusion</h2>
<p>If you just look at the code, you will see that we made a full featured contact form that has validation for required elements and select boxes, recursive trimming of values and that is reusable, in just a few lines. Note that we could even have added client-side validation with javascript without having to add a single line, but this will be talked about in another post&#8230;</p>
<p>In the future, I plan to talk more about rendering and styling forms using CSS and eventually a custom renderer. I also plan to post some nice tricks to make working with QuickForm 2 as efficient as possible and how to make your own elements with javascript. So, stay tuned <img src='http://mansion.im/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://mansion.im/2011/getting-started-with-html_quickform2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
