Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Symfony]Nie uwzględnia walidacji z validate/*.yml
Forum PHP.pl > Forum > PHP > Frameworki
kaniagandzowski
Mam problem z walidowanie formularza w symfony 1.2 a dokładnie nie uwzględnia reguł zamieszczonych w folderze validate/*.yml

Mam standardowy zrobiony formularz

  1. //apps/frontend/modules/sfGuardAuth/lib/form/rejestracjaForm.class.php
  2. <?php
  3.  
  4. class rejestracjaForm extends sfForm
  5. {
  6.  public function configure()
  7.  {
  8.      $this->setWidgets(array(
  9.            'username'     => new sfWidgetFormInput(),
  10.          'password'     => new sfWidgetFormInput( array('type' => 'password') ),
  11.          'password2'    => new sfWidgetFormInput( array('type' => 'password') ),
  12.          
  13.        ));
  14.  
  15.        
  16.        $this->setValidators(array(
  17.          'username'     => new sfValidatorString(),
  18.          'password'     => new sfValidatorString(array('required' => true ) ),
  19.          'password2'    => new sfValidatorAnd( array(
  20.                                new sfValidatorString(array('required' => true ) ),
  21.                                
  22.                            )),        
  23.          
  24.        ));
  25.  
  26.    $this->validatorSchema->setPostValidator(
  27.            new sfValidatorAnd(array(  
  28.                //new sfGuardValidatorUser(),
  29.                new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL, 'password2',
  30.                                    array(),
  31.                                    array('invalid' => 'hasĹ‚o nie jest takie same ')
  32.                                    )
  33.            ))
  34.        );
  35.        
  36.    $this->widgetSchema->setNameFormat('rejestracja[%s]');
  37.  }
  38.  
  39.  public function getModelName()
  40.  {
  41.    return 'Rejestracja';
  42.  }
  43. }
  44. ?>


  1. <?php
  2. ////apps/frontend/modules/sfGuardAuth/actions/action.class.php
  3. class sfGuardAuthActions extends BasesfGuardAuthActions {
  4.  
  5.  
  6.    /**
  7.      * Tworzy nowe konto użytkownikowi
  8.      *
  9.      * Metoda pozwala na zakladanie konta
  10.      *
  11.      * @return unknown
  12.      */
  13.    public function executeRejestracja( sfWebRequest $request ) {
  14.        $this->form = new rejestracjaForm();
  15.    }
  16.    
  17.    public function executeUpdate_rejestracja( sfWebRequest $request ){
  18.        $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
  19.        $this->form = new rejestracjaForm();
  20.        $this->processForm( $request, $this->form );
  21.        $this->setTemplate('rejestracja');
  22.    }
  23.  
  24.    protected function processForm(sfWebRequest $request, sfForm $form)
  25.    {
  26.        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
  27.        
  28.        if ($form->isValid())
  29.        {
  30.            $this->redirect( $this->generateUrl('homepage') );
  31.        }
  32.        return false;
  33.    }
  34.  
  35.    public function handleError() {
  36.        return sfView::ERROR;
  37.    }
  38.    
  39.    
  40.    public function handleErrorUpdate_rejestracja() {
  41.        return sfView::SUCCESS;
  42.    }
  43. ?>


  1. ////apps/frontend/modules/sfGuardAuth/templates/rejestracjaSuccess.php
  2. <?php use_helper('Validation') ?>
  3. <h2>Rejestracja</h2>
  4. <form action="<?php echo url_for('sfGuardAuth/update_rejestracja') ?>" method="post">
  5.  <table>
  6.    <?php echo $form ?>
  7.  </table>
  8.  <?php
  9.      echo submit_tag(__('Zarejestruj się'));
  10.  ?>
  11. </form>


  1. <?php
  2. ////apps/frontend/modules/sfGuardAuth/validate/update_rejestracja.yml
  3. methods: [post]
  4.  
  5. validators:
  6.  nicknameValidator:  
  7.    class:        sfStringValidator
  8.    param:
  9.      min:        3
  10.      min_error:  login musi miec minimum 5 znakow
  11.  
  12.  passwordValidator:
  13.    class:        sfStringValidator
  14.    param:
  15.      min:        5
  16.      min_error:  haslo musi miec minimum 5 znakow
  17.  
  18.  newAccountValidator:
  19.    class: myNewAccountValidator
  20.    param:
  21.      newaccount_error:  login o tej nazwie już istnieje
  22.  
  23.  
  24.  
  25. fields:
  26.  names:
  27.    username:
  28.      required:
  29.        msg: twoj login jest wymagany
  30.      nicknameValidator:
  31.      #newAccountValidator:
  32.  
  33.    password:
  34.      required:
  35.        msg:  twoje haslo jest wymagane
  36.      passwordValidator:
  37.  
  38.    password2:
  39.      required:
  40.        msg: twoje haslo jest wymagane
  41.      passwordValidator:
  42. ?>


Reguły walidacji działają jedynie z klasy rejestracjaForm a nie są brane też pod uwage wpisy z /validate/*.yml

Szukałem na forum symfony lecz tam nawet przykładu podobnego do mojego nie znalazłem.
Ja wykorzystuje do generowania formularza klase sfForm i być może dlatego nie działają reguły walidacji z /validate/update_rejestracja.yml bo reguly walidacji pobiera z klasy rejestracjaForm,

Mi zależy aby można było wykorzystać własne walidatory np.

  1. <?php
  2.  
  3. /**
  4.  * new account validator.
  5.  *
  6.  * @package    askeet
  7.  * @subpackage user
  8.  * @author     Your name here
  9.  * @version    SVN: $Id$
  10.  */
  11. class myNewAccountValidator extends sfValidator
  12. {
  13.  /**
  14.    * Execute this validator.
  15.    *
  16.    * @param mixed A file or parameter value/array.
  17.    * @param error An error message reference.
  18.    *
  19.    * @return bool true, if this validator executes successfully, otherwise
  20.    *              false.
  21.    */
  22.  public function execute (&$value, &$error)
  23.  {
  24.    $this->getContext()->getRequest()->setAttribute('newaccount', true);
  25.  
  26.    $login = $value;
  27.  
  28.    $c = new Criteria();
  29.    $c->add(sfGuardUserPeer::USERNAME, $login);
  30.    $user = sfGuardUserPeer::doSelectOne($c);
  31.  
  32.    // nickname exists?
  33.    if ($user)
  34.    {
  35.      $error = $this->getParameterHolder()->get('newaccount_error');
  36.      return false;
  37.    }
  38.  
  39.    return true;
  40.  }
  41.  
  42.  public function initialize ($context, $parameters = null)
  43.  {
  44.    // initialize parent
  45.    parent::initialize($context);
  46.  
  47.    // set defaults
  48.    $this->getParameterHolder()->set('newaccount_error', 'Invalid input');
  49.  
  50.    $this->getParameterHolder()->add($parameters);
  51.  
  52.    return true;
  53.  }
  54. }
  55.  
  56. ?>


Nie wiem w czym jest problem.
axi
Walidacja z validate/*.yml była tylko w Symfony 1.0, od 1.1 jest niezalecana: "The features described in this section are deprecated in symfony 1.1 and only work if you enable the sfCompat10 plugin." (źródło).

Aby utworzyć własny validator dla Form Framework'a z którego korzystasz musisz dziedziczyć po klasie sfValidatorBase, a nie sfValidator. Niestety The symfony Forms Book nie dotarł do rozdziału o tworzeniu własnych walidatorów. Przyglądając się jednak jak wyglądają dostarczone, np. sfValidatorString dla pojedynczych pól, czy sfValidatorSchemaCompare dla post-validatorów da się napisać własny.

Parametry dla validatorów np. dla passwordValidator umieszczasz w konstruktorze (ustawiając setValidators):
Kod
'password'     => new sfValidatorString(
          array('required' => true, 'min_length'=>5 ),
          array('required'=>'twoje haslo jest wymagane', 'min_length' => 'haslo musi miec minimum %min_length% znakow') );
(opis tego również w The Symfony Forms Book).

Albo więc musisz zrezygnować z Form Framework'a i pisać formularze tak jak to było w Symfony 1.0 i mieć walidację w plikach *.yml, albo wpisać walidację w klasę formularza za pomocą właściwych klas.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.