<?php

/**
 * DiscountPhone form.
 *
 * @package    sf
 * @subpackage form
 * @author     Atma
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class DiscountPhoneForm extends BaseDiscountPhoneForm
{
	public function configure()
	{
		$this->useFields(array('phone', 'discount_id', 'order_id'));
	}

	public function isValid()
	{
	    $is_valid = parent::isValid();
	    if ($is_valid) {
	        $dp = Doctrine_Query::create()
                ->from("DiscountPhone dp")
                ->where("dp.phone = ?", $this->getValue('phone'))
                ->andWhere("dp.id != ?", $this->getValue('id'))
                ->andWhere("dp.order_id is null")
                ->count();
	        if($dp > 0) {
	        	$is_valid = false;
	        	$this->getErrorSchema()->addError(new sfValidatorError($this->getValidator('phone'), 'Пользователь уже добавил скидку'), 'phone');
	        }
	    }
	    return $is_valid;
	}
}
