@madpilot makes

Update: Improved validation in CakePHP for version 1.x

I recently started a new project in CakePHP and thought it time to use version 1. But it seems the way that the validator code works has changed, so here is the updated code:

The following goes in app_model.php

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <span class="br0">{</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="kw1">if</span> <span class="br0">(</span>[empty</span>](http://www.php.net/empty)<span class="br0">(</span><span class="re0">$data</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="re0">$data</span> = <span class="re0">$this</span>-><span class="me1">data</span>;
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="br0">}</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="kw1">if</span> <span class="br0">(</span>!<span class="re0">$this</span>-><span class="me1">beforeValidate</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="kw1">return</span> <span class="kw2">false</span>;
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="br0">}</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="kw1">if</span> <span class="br0">(</span>![isset</span>](http://www.php.net/isset)<span class="br0">(</span><span class="re0">$this</span>-><span class="me1">validate</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="kw1">return</span> <span class="kw2">true</span>;
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="br0">}</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="kw1">if</span> <span class="br0">(</span>![empty</span>](http://www.php.net/empty)<span class="br0">(</span><span class="re0">$data</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="re0">$data</span> = <span class="re0">$data</span>;
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="br0">}</span> <span class="kw1">elseif</span> <span class="br0">(</span>[isset</span>](http://www.php.net/isset)<span class="br0">(</span><span class="re0">$this</span>-><span class="me1">data</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="re0">$data</span> = <span class="re0">$this</span>-><span class="me1">data</span>;
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="br0">}</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="kw1">if</span> <span class="br0">(</span>[isset</span>](http://www.php.net/isset)<span class="br0">(</span><span class="re0">$data</span><span class="br0">[</span><span class="re0">$this</span>-><span class="me1">name</span><span class="br0">]</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="re0">$data</span> = <span class="re0">$data</span><span class="br0">[</span><span class="re0">$this</span>-><span class="me1">name</span><span class="br0">]</span>;
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="br0">}</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="re0">$errors</span> = [array</span>](http://www.php.net/array)<span class="br0">(</span><span class="br0">)</span>;
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="kw1">foreach</span><span class="br0">(</span><span class="re0">$this</span>-><span class="me1">validate</span> <span class="kw1">as</span> <span class="re0">$field_name</span> => <span class="re0">$validators</span><span class="br0">)</span> <span class="br0">{</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="kw1">foreach</span><span class="br0">(</span><span class="re0">$validators</span> <span class="kw1">as</span> <span class="re0">$validator</span><span class="br0">)</span> <span class="br0">{</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="kw1">if</span> <span class="br0">(</span>[preg_match</span>](http://www.php.net/isset)<span class="br0">(</span><span class="re0">$validator</span><span class="br0">[</span><span class="st0">&#8216;expression&#8217;</span><span class="br0">]</span>, <span class="re0">$data</span><span class="br0">[</span><span class="re0">$field_name</span><span class="br0">]</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="re0">$errors</span><span class="br0">[</span><span class="re0">$field_name</span><span class="br0">]</span> = <span class="re0">$validator</span><span class="br0">[</span><span class="st0">&#8216;message&#8217;</span><span class="br0">]</span>;
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="br0">}</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="br0">}</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="br0">}</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="re0">$this</span>-><span class="me1">validationErrors</span> = <span class="re0">$errors</span>;
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="kw1">return</span> <span class="re0">$errors</span>;
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <span class="br0">}</span>
  </div>
</li>

The validation helper method (See my previous post) looks like this:

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="re0">$messages</span> = <span class="st0">&#8220;&#8221;</span>;
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="kw1">if</span><span class="br0">(</span>[isset</span>](http://www.php.net/isset)<span class="br0">(</span><span class="re0">$this</span>-><span class="me1">validationErrors</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="kw1">foreach</span><span class="br0">(</span><span class="re0">$this</span>-><span class="me1">validationErrors</span> <span class="kw1">as</span> <span class="re0">$tag</span><span class="br0">)</span> <span class="br0">{</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="kw1">foreach</span><span class="br0">(</span><span class="re0">$tag</span> <span class="kw1">as</span> <span class="re0">$element</span> => <span class="re0">$message</span><span class="br0">)</span> <span class="br0">{</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="re0">$messages</span> .= [empty</span>](http://www.php.net/sprintf)<span class="br0">(</span><span class="re0">$message</span><span class="br0">)</span> ? <span class="st0">&#8216;Error in field: &#8216;</span> . <span class="re0">$element</span> : <span class="re0">$message</span><span class="br0">)</span>;
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="br0">}</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="br0">}</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="br0">}</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <ins class="in"> </ins> <span class="kw1">return</span> <span class="re0">$messages</span>;
  </div>
</li>

<li class="li1">
  <div class="de1">
    <ins class="in"> </ins> <ins class="in"> </ins> <span class="br0">}</span>
  </div>
</li>