<?xml version="1.0" encoding="UTF-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
  <title>Latest snippets tagged validator</title>
  <link rel="alternate" href="http://snippets.prendreuncafe.com/snippets/tagged/validator/order_by/date"></link>
  <id>http://snippets.prendreuncafe.com/snippets/tagged/validator/order_by/date</id>
  <updated>2008-03-16T08:14:55Z</updated>
  <author>
    <name>Symfony</name>
    <author_email>noreply@symfony-project.com</author_email>
  </author>
<entry>
  <title>[Symfony] Un validateur de date qu'il est bien</title>
  <link href="http://snippets.prendreuncafe.com/snippet/76"></link>
  <updated>2008-03-16T08:14:55Z</updated>
  <id>76</id>
  <summary type="html">Quelques fonctionnalités en plus par rapport à sfDateValidator (qu'il étend), notamment l'interdiction de date futures ou passées et l'évitement du traitement de la langue courante utilisateur (utile pour les formats de date figés).

[code php]
/**
 * Custom date validator
 *
 */
class myDateValidator extends sfDateValidator
{
  /**
   * Execute this validator.
   *
   * @param mixed A file or parameter value/array
   * @param error An error message reference
   *
   * @return bool true, if this validator executes successfully, otherwise false
   */
  public function execute(&amp;$value, &amp;$error)
  {
    $culture = $this-&gt;getContext()-&gt;getUser()-&gt;getCulture();

    // Validate the given date
    if ($this-&gt;getParameter('with_culture'))
    {
      $value1 = $this-&gt;getValidDate($value, $culture);
    }
    else
    {
      $value1 = strtotime($value);
    }

    if (!$value1 || -1 == $value1) // Before php 5.1, strtotime() returns -1 on fail
    {
      $error = $this-&gt;getParameter('date_error');
      return false;
    }

    // Is there a compare to do?
    $compareDateParam = $this-&gt;getParameter('compare');
    $compareDate = $this-&gt;getContext()-&gt;getRequest()-&gt;getParameter($compareDateParam);

    // If the compare date is given
    if ($compareDate)
    {
      $operator = trim($this-&gt;getParameter('operator', '=='), '\'&quot; ');
      $value2 = $this-&gt;getValidDate($compareDate, $culture);

      // If the check date is valid, compare it. Otherwise ignore the comparison
      if ($value2)
      {
        $valid = false;
        switch ($operator)
        {
          case '&gt;':
            $valid = $value1 &gt;  $value2;
            break;
          case '&gt;=':
            $valid = $value1 &gt;= $value2;
            break;
          case '==':
            $valid = $value1 == $value2;
            break;
          case '&lt;=':
            $valid = $value1 &lt;= $value2;
            break;
          case '&lt;':
            $valid = $value1 &lt;  $value2;
            break;

          default:
            throw new sfValidatorException(sprintf('Invalid date comparison operator &quot;%s&quot;', $operator));
        }

        if (!$valid)
        {
          $error = $this-&gt;getParameter('compare_error');

          return false;
        }
      }
    }

    if (!$this-&gt;getParameter('allow_future') &amp;&amp; $value1 &gt; time())
    {
      $error = $this-&gt;getParameter('future_error');
      return false;
    }

    if (!$this-&gt;getParameter('allow_past') &amp;&amp; $value1 &lt; time())
    {
      $error = $this-&gt;getParameter('past_error');
      return false;
    }

    return true;
  }

  /**
   * Initializes the validator.
   *
   * @param sfContext The current application context
   * @param array   An associative array of initialization parameters
   *
   * @return bool true, if initialization completes successfully, otherwise false
   */
  public function initialize($context, $parameters = null)
  {
    // Initialize parent
    parent::initialize($context, $parameters);

    // Set defaults
    $this-&gt;getParameterHolder()-&gt;set('with_culture', true);
    $this-&gt;getParameterHolder()-&gt;set('allow_future', true);
    $this-&gt;getParameterHolder()-&gt;set('future_error', 'Future dates not allowed');
    $this-&gt;getParameterHolder()-&gt;set('allow_past', true);
    $this-&gt;getParameterHolder()-&gt;set('past_error', 'Past dates not allowed');
    $this-&gt;getParameterHolder()-&gt;add($parameters);

    return true;
  }
}

[code]</summary>
</entry>
</feed>
