Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Symfony]bindowanie formularza
Forum PHP.pl > Forum > PHP > Frameworki
Gribo
mam problem z zabindowaniem formularza oraz z jednym polem podczas walidacji mam takie formularze :

  1. <?php
  2.  
  3. /**
  4.  * Uzytkownicy form base class.
  5.  *
  6.  * @package    serwis
  7.  * @subpackage form
  8.  * @author     Your name here
  9.  * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 16976 2009-04-04 12:47:44Z fabien $
  10.  */
  11. class BaseUzytkownicyForm extends BaseFormPropel
  12. {
  13.  public function setup()
  14.  {
  15.    $this->setWidgets(array(
  16.      'usid'     => new sfWidgetFormInputHidden(),
  17.      'login'    => new sfWidgetFormInput(),
  18.      'haslo'    => new sfWidgetFormInputPassword(),
  19.      'imie'     => new sfWidgetFormInput(),
  20.      'nazwisko' => new sfWidgetFormInput(),
  21.      'miasto'   => new sfWidgetFormInput(),
  22.      'mail'     => new sfWidgetFormInput(),
  23.      'osobie'   => new sfWidgetFormInput(),
  24.      'data_2'   => new sfWidgetFormDate(),
  25.      'usuniety' => new sfWidgetFormInput(),
  26.    ));
  27.  
  28.    $this->setValidators(array(
  29.      'usid'     => new sfValidatorPropelChoice(array('model' => 'Uzytkownicy', 'column' => 'usid', 'required' => false)),
  30.      'login'    => new sfValidatorString(array('max_length' => 255, 'required' => true)),
  31.      'haslo'    => new sfValidatorString(array('max_length' => 45, 'required' => true)),
  32.      'imie'     => new sfValidatorString(array('max_length' => 255, 'required' => true)),
  33.      'nazwisko' => new sfValidatorString(array('max_length' => 255, 'required' => true)),
  34.      'miasto'   => new sfValidatorString(array('max_length' => 255, 'required' => true)),
  35.      'mail'     => new sfValidatorEmail(array('required' => true)),
  36.      'osobie'   => new sfValidatorString(array('max_length' => 255, 'required' => false)),
  37.      'data_2'   => new sfValidatorDate(array('required' => false)),
  38.      'usuniety' => new sfValidatorInteger(),
  39.    ));
  40.  
  41.    $this->validatorSchema->setPostValidator(
  42.      new sfValidatorPropelUnique(array('model' => 'Uzytkownicy', 'column' => array('login')))
  43.    );
  44.  
  45.  
  46.    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
  47.  
  48.    parent::setup();
  49.  }
  50.  
  51.  public function getModelName()
  52.  {
  53.    return 'Uzytkownicy';
  54.  }
  55.  
  56.  
  57. }
  58. ?>


  1. <?php
  2. class RejestracjaForm extends BaseUzytkownicyForm {
  3.    public function configure() {
  4.      
  5.        unset($this['usuniety'],$this['miasto'],$this['osobie'],$this['data_2'],$this['usid']);
  6.       $this->setWidget('powtorz',new sfWidgetFormInputPassword());
  7.       $this->setValidator('powtorz',new sfValidatorString(array('max_length' => 45, 'required' => true)));
  8.       $this->widgetSchema->moveField('powtorz',sfWidgetFormSchema::AFTER,'haslo');
  9.       [b]$this->widgetSchema->setNameFormat('register[%s]');[/b]
  10.       $this->widgetSchema->setFormFormatterName('list');
  11.       $this->validatorSchema->setPostValidator(
  12.            new sfValidatorSchemaCompare('haslo','==','powtorz'));
  13.       }
  14.    
  15.  
  16. }
  17. ?>


  1. <?php
  2.  
  3. /**
  4.  * rejestracja actions.
  5.  *
  6.  * @package    serwis
  7.  * @subpackage rejestracja
  8.  * @author     Your name here
  9.  * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
  10.  */
  11. class rejestracjaActions extends sfActions
  12. {
  13. /**
  14.   * Executes index action
  15.   *
  16.   * @param sfRequest $request A request object
  17.   */
  18.  public function executeIndex(sfWebRequest $request)
  19.  {
  20.      $this->form=new RejestracjaForm();
  21.      if ($request->isMethod('post')) {
  22. //           $this->form->bind(array('login'=>$this->getRequest()->getParameter('login'),
  23. //                         'haslo'=>$request->getParameter('haslo'),
  24. //                         'powtorz'=>$this->getRequest()->getParameter('powtorz'),
  25. //                         'imie'=>$this->getRequest()->getParameter('imie'),
  26. //                         'nazwisko'=>$this->getRequest()->getParameter('nazwisko'),
  27. //                         'mail'=>$this->getRequest()->getParameter('mail'),
  28. //                         ));
  29.          [b] $this->form->bind($request->getParameter('register'));[/b]
  30.           if ($this->form->isValid()) {
  31.               $this->form->save();
  32.               $this->redirect('rejestracja/dziekujemy');
  33.           }
  34.          
  35.          }
  36.  }
  37.  
  38.  
  39.  public function executeDziekujemy(sfWebRequest $request) {
  40.     $this->setTemplate('dziekuje');
  41. }
  42. }
  43. ?>


Otóz jeśli binduje formularz w ten sposób, który jest zakomentowany to wszytsko jest ok, ale czy nie ma jakiegoś szybszego sposobu takiego jak ta linijka wytłuszczona. Jak chce tak zrobic to niestety wyskakuje błąd : Catchable fatal error: Argument 1 passed to sfForm::bind() must be an array ... (co oczywiscie sygnalizuje ze wymaga jako arg. tablicy. Jeszcze z czym mam problem to
  1. <?php
  2. $this->validatorSchema->setPostValidator(
  3.        new sfValidatorPropelUnique(array('model' => 'Uzytkownicy', 'column' => array('login')))
  4.      );
  5. ?>
ten walidator nie działa nie mam pojęcia dlaczego.

Aha jescze jedno pytanko jak odnieść się do takiego pola podczas bindowania $this->widgetSchema->setNameFormat('register[%s]'); bo jak próbuję 'mail'=>$this->getRequest()->getParameter('register[mail]') to nie działa
Pr0100
  1. <?php
  2. $request->getParameter('register')
  3. ?>


1. Nie masz w routing.yml nigdzie zminnej register?
2. W po wyrenderowaniu formularza pola mają name="register[nazwa_pola]"?
3. co daje print_r($request->getParameter('register')) ?

  1. <?php
  2. $this->validatorSchema->setPostValidator
  3. ?>


nie łatwiej będzie

  1. <?php
  2. if ($this->isNew)
  3. {
  4.  $this->setValidator('login', new sfValidatorAnd(array(
  5.        new sfValidatorPropelUnique(array(
  6.          'model' => 'Uzytkownicy',
  7.          'column' => 'login'
  8.        )),
  9.        $this->getValidator('login')
  10.  )));
  11. }
  12. ?>
Gribo
-zmienna register[] jest deklarowana tu :
$this->widgetSchema->setNameFormat('register[%s]');
w class RejestracjaForm extends BaseUzytkownicyForm

-po wynderewowaniu pola formularza maja poprawna wartość tj. name=register[login] itd.

- niestety sfValidatorPropelUnique jest walidatorem post i nie moge go wywołac tak jak napisałeś

JUŻ SE PORADZIŁEM DZIEKI
tOm-i
Cytat(Gribo @ 5.07.2009, 15:43:54 ) *
JUŻ SE PORADZIŁEM DZIEKI


Warto by napisać w jaki sposób, dla potomnych ...
blackroger
Chyba się domyślam co było przyczyną problemu, bo przed chwila miałem podobny...Prawdopodobnie chodziło o nazwę inputa w formularzu, któremu przypisany został atrybut name="Register" co powodowało że zamiast tablicy ładowała się wartość register....
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.