Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Symfony]setError
Lonas
post
Post #1





Grupa: Zarejestrowani
Postów: 576
Pomógł: 14
Dołączył: 9.11.2005

Ostrzeżenie: (20%)
X----


Jak mogę w symfony 1.2 ustawić setError ?

Jak mogę zrobić walidacje wysokosci i szerokosci obrazka ? dopisac do sfFileValidator kod odpowiedzialny za to czy w jakis inny sposób ?

Ten post edytował Lonas 20.03.2009, 13:17:59
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Lonas
post
Post #2





Grupa: Zarejestrowani
Postów: 576
Pomógł: 14
Dołączył: 9.11.2005

Ostrzeżenie: (20%)
X----


Zrobiłem ale też nie działo :/ walidator wygląda tak :

  1. <?php
  2. class sfFileImageValidator extends sfValidatorFile
  3. {
  4.  /**
  5.    * Executes this validator.
  6.    *
  7.    * @param mixed A file or parameter value/array
  8.    * @param error An error message reference
  9.    *
  10.    * @return bool true, if this validator executes successfully, otherwise false
  11.    */
  12.  protected function configure($options = array(), $messages = array())
  13.  {
  14.    parent::configure($options, $messages);
  15.  
  16.    $this->addOption('max_width');
  17.    $this->addMessage('max_width', 'tttt');
  18.  }
  19.  
  20.  
  21.  public function execute(&$value, &$error)
  22.  {
  23.    if (parent::execute($value, $error))
  24.    {
  25.      list($width, $height) = @getimagesize($value['tmp_name']);
  26.  
  27.      // File is not a square
  28.      $is_square = $this->getParameter('is_square');
  29.      if ($is_square && $width != $height)
  30.      {
  31.        $error = $this->getParameter('is_square_error');
  32.  
  33.        return false;
  34.      }
  35.  
  36.      // File height too large
  37.      $max_height = $this->getParameter('max_height');
  38.      if ($max_height !== null && $max_height < $height)
  39.      {
  40.        $error = $this->getParameter('max_height_error');
  41.  
  42.        return false;
  43.      }
  44.  
  45.      // File width too large
  46.      $max_width = $this->getParameter('max_width');
  47.      if ($max_width !== null && $max_width < $width)
  48.      {
  49.        $error = $this->getParameter('max_width_error');
  50.  
  51.        return false;
  52.      }
  53.  
  54.      // File height too small
  55.      $min_height = $this->getParameter('min_height');
  56.      if ($min_height !== null && $min_height > $height)
  57.      {
  58.        $error = $this->getParameter('min_height_error');
  59.  
  60.        return false;
  61.      }
  62.  
  63.      // File width too small
  64.      $min_width = $this->getParameter('min_width');
  65.      if ($min_width !== null && $min_width > $width)
  66.      {
  67.        $error = $this->getParameter('min_width_error');
  68.  
  69.        return false;
  70.      }
  71.  
  72.      return true;
  73.    }
  74.  }
  75.  
  76.  /**
  77.    * Initializes this validator.
  78.    *
  79.    * @param sfContext The current application context
  80.    * @param array   An associative array of initialization parameters
  81.    *
  82.    * @return bool true, if initialization completes successfully, otherwise false
  83.    */
  84.  public function initialize($context, $parameters = null)
  85.  {
  86.    // initialize parent
  87.    parent::initialize($context, $parameters);
  88.  
  89.    // set defaults
  90.    $this->getParameterHolder()->set('max_height',        null);
  91.    $this->getParameterHolder()->set('max_height_error',  'The file height is too large');
  92.    $this->getParameterHolder()->set('max_width',         null);
  93.    $this->getParameterHolder()->set('max_width_error',   'The file width is too large');
  94.    $this->getParameterHolder()->set('min_height',        null);
  95.    $this->getParameterHolder()->set('min_height_error',  'The file height is too small');
  96.    $this->getParameterHolder()->set('min_width',         null);
  97.    $this->getParameterHolder()->set('min_width_error',   'The file width is too small');
  98.    $this->getParameterHolder()->set('is_square',         false);
  99.    $this->getParameterHolder()->set('is_square_error',   'The file is not a square');
  100.  
  101.    $this->getParameterHolder()->add($parameters);
  102.  
  103.    return true;
  104.  }
  105. }
  106. ?>


walidacja

  1. <?php
  2. $this->setValidators(array(
  3.        'photo_id'    => new sfValidatorPropelChoice(array('model' => 'Photo', 'column' => 'photo_id', 'required' => false)),
  4.        'gallery_id'  => new sfValidatorChoice(array('choices' => array_keys(Gallery::getValuesToSelectGallery1()))),
  5.        'title'       => new sfValidatorString(array('min_length' => 1), array('required' => 'Wpisz tytuł zdjęcia')),
  6.        'file_patch'  => new sfFileImageValidator(array('path' => sfConfig::get('sf_upload_dir').'/gallery/temp','required' => true,'mime_types' => array('image/jpeg','image/pjpeg'),'max_size' => '261120','max_width' =>'200'), array('mime_types' =>'Nieprawidłowy format pliku','max_size'=>'Za duży rozmiar pliku. Limit to 255 Kb','required'=>'Wybierz zdjęcie')  ),
  7.        'description' => new sfValidatorString(array('required' => false)),
  8.        'tags'        => new sfValidatorString(array('required' => false)),
  9.      ));
  10. ?>


Wrzucajac zdjecie na serwer w ogole nie ma walidacji szerokosci tego pliku
Go to the top of the page
+Quote Post

Posty w temacie
- Lonas   [Symfony]setError   20.03.2009, 11:36:12
- - AxZx   [PHP] pobierz, plaintext <?php$this->setVali...   20.03.2009, 13:47:04
- - Lonas   Znalazlem gotowa klase ale nie dla symfony 1.2 moz...   20.03.2009, 13:50:27
- - AxZx   właśnie, tak jak napisałem;) [PHP] pobierz, plaint...   20.03.2009, 13:53:38
- - Lonas   Nie wiem czemu jak próbuje tego użyc to wywala błą...   20.03.2009, 13:59:23
- - destroyerr   To dlatego, że walidator podczas konfigurowan...   20.03.2009, 14:27:27
- - Lonas   Faktycznie dzięki - zrobiłem jak piszesz tyle że w...   20.03.2009, 14:42:22
- - destroyerr   Po części mój błąd, bo domyślam się, że tego nie z...   20.03.2009, 15:40:29
- - Lonas   Zrobiłem ale też nie działo :/ walidator wygląda t...   23.03.2009, 10:32:40
- - mike   Jako, że tytuł wątku i pośrednio tematyka bardzo m...   23.03.2009, 11:23:54
- - Lonas   No własnie w symfony 1.0, 1.1 było prosto setError...   23.03.2009, 11:46:04
|- - mike   Cytat(Lonas @ 23.03.2009, 11:46:04 ) ...   23.03.2009, 11:52:49
- - destroyerr   @Lonas, walidator nie działa z prostej przyczyny. ...   23.03.2009, 13:05:14
|- - mike   Cytat(destroyerr @ 23.03.2009, 13:05...   23.03.2009, 13:15:45
- - Lonas   To ja klikne za Ciebie ;-) destroyerr : Na stroni...   23.03.2009, 13:34:42
|- - mike   Cytat(Lonas @ 23.03.2009, 13:34:42 ) ...   23.03.2009, 13:50:48
- - Lonas   Czyli dokumentacja niby do 1.2 też nieaktualna ? h...   23.03.2009, 13:52:06
|- - mike   Cytat(Lonas @ 23.03.2009, 13:52:06 ) ...   23.03.2009, 13:59:09
- - Lonas   Czy to powinno być coś w ten deseń ? [PHP] pobie...   23.03.2009, 14:15:39
- - destroyerr   Mniej więcej coś takiego, tylko klasa wyjątku to s...   23.03.2009, 14:37:41
- - Lonas   Walidator działa - wyglada to narazie tak : [PHP]...   23.03.2009, 15:15:43


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 3.10.2025 - 18:31