View file application/modules/Fields/Form/Element/Birthdate.php

File size: 1.72Kb
<?php
/**
 * SocialEngine
 *
 * @category   Application_Core
 * @package    Fields
 * @copyright  Copyright 2006-2020 Webligo Developments
 * @license    http://www.socialengine.com/license/
 * @version    $Id: Birthdate.php 10162 2014-04-11 19:49:53Z andres $
 * @author     John
 */

/**
 * @category   Application_Core
 * @package    Fields
 * @copyright  Copyright 2006-2020 Webligo Developments
 * @license    http://www.socialengine.com/license/
 * @author     John
 */
class Fields_Form_Element_Birthdate extends Engine_Form_Element_Birthdate
{
  protected $_metaObject;

  public function init()
  {
    parent::init();

    // Set min age
    if( !is_null($this->min_age) ) {

      // Set max year
      $date = new Zend_Date();
      $this->_yearMax = (int) $date->get(Zend_Date::YEAR) - (int) $this->min_age;

      // Add validator
      $validator = new Engine_Validate_Callback(array($this, 'validateAge'));
      $validator->setMessage('The minimum age is ' . $this->min_age . '.', 'invalid');
      $this->addValidator($validator);
    }
  }

  public function setMetaObject($meta)
  {
    $this->_metaObject = $meta;
    return $this;
  }

  public function validateAge($value)
  {
    $parts = @explode('-', $value);

    // Error if not filled out
    if( engine_count($parts) < 3 || engine_count(array_filter($parts)) < 3 ) {
      //$this->addError('Please fill in your birthday.');
      return false;
    }

    $value = mktime(0, 0, 0, $parts[1], $parts[2], $parts[0]);

    // Error if too low
    $date = new Zend_Date($value);
    $date->add((int) $this->min_age, Zend_Date::YEAR);

    if ($date->compare(new Zend_Date()) > 0) {
      //$this->addError('You are not old enough.');
      return false;
    }
    
    return true;
  }
}