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 ?
<?php 'tytul' => new sfValidatorString( ) )); ?>
<?php 'file_path' => 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') ) ?>
<?php class sfFileImageValidator extends sfValidatorFile { { $this->addOption('mime_types'); //a jesli chcesz dodac opcje wymagana, to: $this->addRequiredOption('mime_types'); } } ?>
<?php class sfFileImageValidator extends sfValidatorFile { /** * Executes this validator. * * @param mixed A file or parameter value/array * @param error An error message reference * * @return bool true, if this validator executes successfully, otherwise false */ { parent::configure($options, $messages); $this->addOption('max_width'); $this->addMessage('max_width', 'tttt'); } public function execute(&$value, &$error) { if (parent::execute($value, $error)) { // File is not a square $is_square = $this->getParameter('is_square'); if ($is_square && $width != $height) { $error = $this->getParameter('is_square_error'); return false; } // File height too large $max_height = $this->getParameter('max_height'); if ($max_height !== null && $max_height < $height) { $error = $this->getParameter('max_height_error'); return false; } // File width too large $max_width = $this->getParameter('max_width'); if ($max_width !== null && $max_width < $width) { $error = $this->getParameter('max_width_error'); return false; } // File height too small $min_height = $this->getParameter('min_height'); if ($min_height !== null && $min_height > $height) { $error = $this->getParameter('min_height_error'); return false; } // File width too small $min_width = $this->getParameter('min_width'); if ($min_width !== null && $min_width > $width) { $error = $this->getParameter('min_width_error'); return false; } return true; } } /** * Initializes this validator. * * @param sfContext The current application context * @param array An associative array of initialization parameters * * @return bool true, if initialization completes successfully, otherwise false */ public function initialize($context, $parameters = null) { // initialize parent parent::initialize($context, $parameters); // set defaults $this->getParameterHolder()->set('max_height', null); $this->getParameterHolder()->set('max_height_error', 'The file height is too large'); $this->getParameterHolder()->set('max_width', null); $this->getParameterHolder()->set('max_width_error', 'The file width is too large'); $this->getParameterHolder()->set('min_height', null); $this->getParameterHolder()->set('min_height_error', 'The file height is too small'); $this->getParameterHolder()->set('min_width', null); $this->getParameterHolder()->set('min_width_error', 'The file width is too small'); $this->getParameterHolder()->set('is_square', false); $this->getParameterHolder()->set('is_square_error', 'The file is not a square'); $this->getParameterHolder()->add($parameters); return true; } } ?>
<?php 'photo_id' => new sfValidatorPropelChoice(array('model' => 'Photo', 'column' => 'photo_id', 'required' => false)), 'gallery_id' => new sfValidatorChoice(array('choices' => array_keys(Gallery::getValuesToSelectGallery1()))), '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') ), )); ?>
<?php // Utworzenie formularza i... $this->adminForm = new AdminForm(); // ...przekazanie go do widoku. $this->setVar('adminForm', $this->adminForm); // Jeśli żądania jest typu POST i przetwarzanie formularza powiodło się... if ($request->getMethod() === sfRequest::POST && $this->processForm($request, $this->adminForm)) { // ...to dokonywana jest próba zapisu try { $this->adminForm->save(); /* @var $admin Admin */ $admin = $this->adminForm->getObject(); // Ustawienie komunikatu $this->getUser()->setFlash('create', "Utworzono nowego administratora ($admin)"); // Przekierowanie na widok główny modułu zarządzania administratorami $this->redirect('@admins'); } catch (Exception $e) { // Logowanie błędu sfContext::getInstance()->getLogger()->err($e->getMessage()); // Ustawienie komunikatu o niepowodzeniu } } ?>
<?php // Utworzenie formularza i... $this->adminForm = new AdminForm(); // ...przekazanie go do widoku. $this->setVar('adminForm', $this->adminForm); // Jeśli żądania jest typu POST if ($request->getMethod() === sfRequest::POST) { // ...to dokonywana jest próba zapisu try { $this->processForm($request, $this->adminForm); /* @var $admin Admin */ $admin = $this->adminForm->getObject(); // Ustawienie komunikatu $this->getUser()->setFlash('create', "Utworzono nowego administratora ($admin)"); // Przekierowanie na widok główny modułu zarządzania administratorami $this->redirect('@admins'); } catch (Exception $e) { // Logowanie błędu sfContext::getInstance()->getLogger()->err($e->getMessage()); // Ustawienie komunikatu o niepowodzeniu $this->adminForm->getErrorSchema()->addError( ); } } ?>
<?php /** * @author Michał Mech */ class sfFormProcessable extends sfForm { public function addProcessError(sfFormProcessError $error) { $this->getErrorSchema()->addError($error); } } /** * @author Michał Mech <michal.mech@k2.pl> */ abstract class sfFormPropelProcessable extends sfFormPropel { public function addProcessError(sfFormProcessError $error) { $this->getErrorSchema()->addError($error); } } /** * @author Michał Mech <michal.mech@k2.pl> */ class sfFormProcessError extends sfValidatorError { /** * @var sfValidatorBase */ protected $validator; public function __construct($message = null) { $this->validator = new sfValidatorPass(); $this->validator->setMessage('invalid', $message); parent::__construct($this->validator, 'invalid'); } } ?>
<?php $form = SampleForm(); try { $form->save() } catch (Exception $e) { $form->addProcessError( new sfFormProcessError($e->getMessage()); ); } ?>
<?php class sfFileImageValidator extends sfValidatorFile { { parent::configure($options, $messages); $this->addOption('max_width'); $this->addMessage('max_width', 'tttt'); } protected function doClean($value) { if ($width > 100) { throw new sfException('My exception message'); } return $value; } } ?>
<?php class sfFileImageValidator extends sfValidatorFile { { parent::configure($options, $messages); $this->addOption('max_width'); $this->addMessage('max_width', 'tttt'); } protected function doClean($value) { if ($this->hasOption('max_width') && $width > $this->getOption('max_width')) { throw new sfValidatorError($this, 'max_width', array('value' => $value, 'max_width' => $this->getOption('max_width'))); } return $value; } } ?>
<?php $class = $this->getOption('validated_file_class'); return new $class($value['name'], $mimeType, $value['tmp_name'], $value['size'], $this->getOption('path')); ?>