Default symfony 1.4 file validator can validate only some basic file attributes. Sometimes we need more, for example we want to prevent small images from being uploaded. Code below presents my custom sfImageFileValidator which allows you to apply some dimensions constraints to image file validation, e.g. max_height or max_width.
</p><br />
<p>&lt;?php</p><br />
<p>/**<br /><br />
* sfImageFileValidator allows you to apply constraints to image file upload, it extend the sfFileValidator functions.<br /><br />
*<br /><br />
* @author Kamil Adryjanek &lt;kamil.adryjanek@gmail.com&gt;<br /><br />
*/<br /><br />
class sfImageFileValidator extends sfValidatorFile<br /><br />
{<br /><br />
/**<br /><br />
* Configures the current validator.<br /><br />
*<br /><br />
* Available options:<br /><br />
*<br /><br />
* * max_height: The maximum file height in pixels<br /><br />
* * min_height: The minimum file height in pixels<br /><br />
* * max_width: The maximum file width in pixels<br /><br />
* * min_width: The minimum file width in pixels<br /><br />
*<br /><br />
* Available error codes:<br /><br />
*<br /><br />
* * max_height<br /><br />
* * min_height<br /><br />
* * max_width<br /><br />
* * min_width<br /><br />
*<br /><br />
* @param array $options An array of options<br /><br />
* @param array $messages An array of error messages<br /><br />
*<br /><br />
* @see sfValidatorBase<br /><br />
*/<br /><br />
public function configure($options = array(), $messages = array())<br /><br />
{<br /><br />
parent::configure($options, $messages);</p><br />
<p> $this-&gt;addOption(&amp;#039;max_height&amp;#039;);<br /><br />
$this-&gt;addOption(&amp;#039;min_height&amp;#039;);<br /><br />
$this-&gt;addOption(&amp;#039;max_width&amp;#039;);<br /><br />
$this-&gt;addOption(&amp;#039;min_width&amp;#039;);</p><br />
<p> $this-&gt;addMessage(&amp;#039;max_height&amp;#039;, &amp;#039;File is too high (maximum is %max_height% pixels, %height% given).&amp;#039;);<br /><br />
$this-&gt;addMessage(&amp;#039;min_height&amp;#039;, &amp;#039;File is too short (minimum is %min_height% pixels, %height% given).&amp;#039;);<br /><br />
$this-&gt;addMessage(&amp;#039;max_width&amp;#039;, &amp;#039;File is too wide (maximum is %max_width% pixels, %width% given).&amp;#039;);<br /><br />
$this-&gt;addMessage(&amp;#039;min_width&amp;#039;, &amp;#039;File is too thin (minimum is %min_width% pixels, %width% given).&amp;#039;);<br /><br />
}</p><br />
<p> /**<br /><br />
* This validator always returns a sfValidatedFile object.<br /><br />
*<br /><br />
* The input value must be an array with the following keys:<br /><br />
*<br /><br />
* * tmp_name: The absolute temporary path to the file<br /><br />
* * name: <div style="display: none"><a href='http://viagralowestprice.com/'>buy viagra uk</a></div> The original file name (optional)<br /><br />
* * type: The file content type (optional)<br /><br />
* * error: The error code (optional)<br /><br />
* * size: The file size in bytes (optional)<br /><br />
*<br /><br />
* @see sfValidatorBase<br /><br />
*/<br /><br />
protected function doClean($value)<br /><br />
{<br /><br />
if (!is_array($value) || !isset($value[&amp;#039;tmp_name&amp;#039;])) {<br /><br />
throw new sfValidatorError($this, &amp;#039;invalid&amp;#039;, array(&amp;#039;value&amp;#039; =&gt; (string) $value));<br /><br />
}</p><br />
<p> // get image dimensions<br /><br />
list($wi&lt;div style=&quot;display: none&quot;&gt;&lt;a href=&quot;http://isidoreguest.devhub.com/blog/1440105-how-do-you-know-when-your-sons-father-wants-you-back/&quot;&gt;How To Make Ex Want You Back Leave The Girl&lt;/a&gt;&lt;/div&gt;dth, $height) = getimagesize($value[&amp;#039;tmp_name&amp;#039;]);</p><br />
<p> // check file height<br /><br />
if ($this-&gt;hasOption(&amp;#039;max_height&amp;#039;) &amp;&amp; $this-&gt;getOption(&amp;#039;max_height&amp;#039;) &lt; (int) $height) {<br /><br />
throw new sfValidatorError($this, &amp;#039;max_height&amp;#039;, array(&amp;#039;max_height&amp;#039; =&gt; $this-&gt;getOption(&amp;#039;max_height&amp;#039;), &amp;#039;height&amp;#039; =&gt; (int) $height));<br /><br />
}</p><br />
<p> if ($this-&gt;hasOption(&amp;#039;min_height&amp;#039;) &amp;&amp; $this-&gt;getOption(&amp;#039;min_height&amp;#039;) &gt; (int) $height) {<br /><br />
throw new sfValidatorError($this, &amp;#039;min_height&amp;#039;, array(&amp;#039;min_height&amp;#039; =&gt; $this-&gt;getOption(&amp;#039;min_height&amp;#039;), &amp;#039;height&amp;#039; =&gt; (int) $height));<br /><br />
}</p><br />
<p> <div style="display: none"><a href='http://online-cialis-buy.com/'>cialis vs levitra</a></div> // check file width<br /><br />
if ($this-&gt;hasOption(&amp;#039;max_width&amp;#039;) &amp;&amp; $this-&gt;getOption(&amp;#039;max_width&amp;#039;) &lt; (int) $width) {<br /><br />
throw new sfValidatorError($this, &amp;#039;max_width&amp;#039;, array(&amp;#039;max_width&amp;#039; =&gt; $this-&gt;getOption(&amp;#039;max_width&amp;#039;), &amp;#039;width&amp;#039; =&gt; (int) $width));<br /><br />
}</p><br />
<p> if ($this-&gt;hasOption(&amp;#039;min_width&amp;#039;) &amp;&amp; $this-&gt;getOption(&amp;#039;min_width&amp;#039;) &gt; (int) $width) {<br /><br />
throw new sfValidatorError($this, &amp;#039;min_width&amp;#039;, array(&amp;#039;min_width&amp;#039; =&gt; $this-&gt;getOption(&amp;#039;min_width&amp;#039;), &amp;#039;width&amp;#039; =&gt; (int) $width));<br /><br />
}</p><br />
<p> // check other options<br /><br />
return parent::doClean($value);<br /><br />
}<br /><br />
}<br /><br />
?&gt;</p><br />
<p>
How to use this code?
</p><br />
<p>&lt;?php</p><br />
<p>class PhotoForm extends sfForm<br /><br />
{<br /><br />
public function configure()<br /><br />
{</p><br />
<p> //...</p><br />
<p> $this-&gt;widgetSchema[&amp;#039;photo&amp;#039;] = new sfWidgetFormInputFileEditable(array(<br /><br />
&amp;#039;label&amp;#039; =&gt; &amp;#039;User photo&amp;#039;,<br /><br />
&amp;#039;file_src&amp;#039; =&gt; &amp;#039;/uploads/photos/&amp;#039;.$this-&gt;getObject()-&gt;getPhoto(),<br /><br />
&amp;#039;is_image&amp;#039; =&gt; true,<br /><br />
&amp;#039;edit_mode&amp;#039; =&gt; !$this-&gt;isNew(),<br /><br />
&amp;#039;template&amp;#039; =&gt; &amp;#039;&lt;div&gt;%file%&lt;br /&gt;%input%&lt;br /&gt;%delete% %delete_label%&lt;/div&gt;&amp;#039;,<br /><br />
));</p><br />
<p> $this-&gt;validatorSchema[&amp;#039;photo&amp;#039;] = new sfImageFileValidator(array(<br /><br />
&amp;#039;required&amp;#039; =&gt; true,<br /><br />
&amp;#039;mime_types&amp;#039; =&gt; array(&amp;#039;image/jpeg&amp;#039;, &amp;#039;image/png&amp;#039;, &amp;#039;image/gif&amp;#039;, &amp;#039;image/pjpeg&amp;#039;),<br /><br />
&amp;#039;max_size&amp;#039; =&gt; &amp;#039;1048576&amp;#039;,<br /><br />
&amp;#039;min_height&amp;#039; =&gt; &amp;#039;640&amp;#039;,<br /><br />
&amp;#039;min_width&amp;#039; =&gt; &amp;#039;480&amp;#039;,<br /><br />
&amp;#039;path&amp;#039; =&gt; sfConfig::get(&amp;#039;sf_upload_dir&amp;#039;).&amp;#039;/photos&amp;#039;,<br /><br />
), array(<br /><br />
&amp;#039;required&amp;#039; =&gt; &quot;Photo is required!&quot;,<br /><br />
&amp;#039;min_height&amp;#039; =&gt; &quot;Custom message for height vaidation.&quot;,<br /><br />
&amp;#039;min_width&amp;#039; =&gt; &quot;File is too thin (minimum is %min_width% pixels, %width% given).&quot;<br /><br />
));</p><br />
<p> $this-&gt;validatorSchema[&amp;#039;photo_delete&amp;#039;] = new sfValidatorPass();</p><br />
<p> //…</p><br />
<p> }<br /><br />
}</p><br />
<p>?&gt;<br /><br />
How To Make Ex Want You Back Leave The Girl
zp8497586rq