Thursday, September 6, 2012

How to Save Empty Form Fields as NULL in Symfony

If you leave a field empty in your form, Symfony will save it as a blank value. However sometimes you want to save it as NULL...

Create a method for the field in the form class:
  public function updateManufacturerColumn($value)
  {
    return (!$value) ? null : $value;
  }
Manufacturer is the name of the field now.

Post Validator in Symfony

  public function configure()
  {
    $this->getValidator('day')->setOption('required', 'true');
    
    $this->validatorSchema->setPostValidator(new sfValidatorAnd(array(
      new sfValidatorCallback(array('callback' => array($this, 'checkFields'))),
    )));
  }
  
  public function checkFields($validator, $values)
  {
    if(empty($values['manufacturer']) && empty($values['category_apollo_kod']) && empty($values['product_apollo_kod']))
    {
      throw new sfValidatorErrorSchema($validator, array('manufacturer' => new sfValidatorError($validator, 'custom error')));
    }
     
    return $values;
  }

Wednesday, August 22, 2012

Debian: Install NTPD To Synchronism Clock With Internet Standard Time Servers

date
apt-get install ntp
(wait....)
date
(works like a charm)

Wednesday, July 11, 2012

Using Validators in Actions in Symfony

Check whether an email address is valid or not with sfValidatorEmail:
$v = new sfValidatorEmail();
try {
  $v->clean(trim($email));
} catch (sfValidatorError $e) {
  // not valid
}

Tuesday, July 10, 2012

phpMyAdmin: No activity within 1440 seconds

At the end of /etc/phpmyadmin/config.inc.php:
$cfg['LoginCookieValidity'] = 60 * 60 * 8;

/etc/php5/apache2/php.ini:
session.gc_maxlifetime = 30000

Thursday, July 5, 2012

Log to Custom Log File in Symfony

$logger = new sfFileLogger($this->getContext()->getEventDispatcher(), array('file' => sfConfig::get('sf_log_dir').'/CronSendOrderEmail.log'));

Wednesday, July 4, 2012

Symfony Email Validator Accepts Accents

Use this validator to ignore accents:

  1. Put this after sfCoreAutoload::register(); in config/ProjectConfiguration.class.php:
    require_once dirname(__FILE__).'/../lib/validator/sfValidatorEmail.class.php';
  2. Create lib/validator/sfValidatorEmail.class.php with this content:
class sfValidatorEmail extends sfValidatorRegex
{
   // const REGEX_EMAIL = '/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i';
  // const REGEX_EMAIL = '/^([^@öüóőúéáűíÖÜÓŐÚÉÁŰÍ\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i';
  
  const REGEX_EMAIL = '/^([A-Z0-9._%+-]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i';
  /**
   * @see sfValidatorRegex
   */
  protected function configure($options = array(), $messages = array())
  {
    parent::configure($options, $messages);
    $this->setOption('pattern', self::REGEX_EMAIL);
  }
}