<?php

class LoginForm extends sfForm {
    public function validConnection($validator, $value) {
        $request = sfContext::getInstance()->getRequest();
        $login = $request->getParameter('login[login]');
        $password = $request->getParameter('login[password]');
        if (!empty($login) && !empty($password)) {
            if (null == $katao_user = KataoUserPeer::getUserByLoginAndPassword($login, $password)) {
                throw new sfValidatorError($validator, 'invalid');
            } else {
                $credentials = $katao_user->getCredentials();
                if (1 == count($credentials) && in_array('member', $credentials)) {
                    throw new sfValidatorError($validator, 'invalid');
                }
            }
        }
        return $value;
    }
    public function configure() {
        $this->setWidgets(array(
                'login' => new sfWidgetFormInput(),
                'password' => new sfWidgetFormInputPassword(),
                ));
        $this->widgetSchema->setNameFormat('login[%s]');
        $this->widgetSchema->setLabels(array(
                'login' => 'Identifiant',
                'password' => 'Mot de passe',
                ));
        $this->setValidators(array(
                'login' => new sfValidatorString(array('required' => true), array('required' => 'Merci de préciser votre identifiant.')),
                'password' => new sfValidatorString(array('required' => true), array('required' => 'Merci de préciser votre mot de passe.')),
                ));
        $this->validatorSchema->setPostValidator(new sfValidatorCallback(
                array('callback' => array($this, 'validConnection')), array('invalid' => 'Utilisateur inconnu.'))
            );
    }
}

?>