コード例 #1
0
 /**
  * returns an validator for a single category
  *
  * @return Zend_Filter_Input validator
  */
 protected function getValidator()
 {
     // define filter
     $filterTrim = new Zend_Filter_StringTrim();
     $filterUtf8 = new application_filter_utf8();
     $filter = array('id' => $filterTrim, 'name' => array($filterTrim, $filterUtf8), 'position' => $filterTrim);
     // define validators
     $validatorId = new application_validate_categoryid();
     $validatorId->setMessage(Zend_Registry::get('language')->translate("category doesn't exists"), application_validate_categoryid::NOT_EXISTS);
     $validatorNotEmpty = new Zend_Validate_NotEmpty();
     $validatorNotEmpty->setMessage(Zend_Registry::get('language')->translate("Value is required and can't be empty"), Zend_Validate_NotEmpty::IS_EMPTY);
     $validatorAlnum = new Zend_Validate_Alnum(true);
     $validatorAlnum->setMessage(Zend_Registry::get('language')->translate('Only alphanummeric values allowed'), Zend_Validate_Alnum::NOT_ALNUM);
     $validatorAlnum->setMessage(Zend_Registry::get('language')->translate("Value is required and can't be empty"), Zend_Validate_Alnum::STRING_EMPTY);
     $validatorNum = new Zend_Validate_Digits(false);
     $validatorNum->setMessage(Zend_Registry::get('language')->translate('Only digits allowed'), Zend_Validate_Digits::NOT_DIGITS);
     $validatorNum->setMessage(Zend_Registry::get('language')->translate("Value is required and can't be empty"), Zend_Validate_Digits::STRING_EMPTY);
     $validators = array('id' => array($validatorId, Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_OPTIONAL), 'name' => array($validatorNotEmpty, Zend_Filter_Input::ALLOW_EMPTY => false, Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED), 'position' => array($validatorNum, Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_OPTIONAL));
     // create validation main object
     $validator = new Zend_Filter_Input($filter, $validators, array(), array(Zend_Filter_Input::NOT_EMPTY_MESSAGE => Zend_Registry::get('language')->translate("Value is required and can't be empty"), Zend_Filter_Input::BREAK_CHAIN => false));
     // return filter input object
     return $validator;
 }
コード例 #2
0
 /**
  * validates feed input
  *
  * @return Zend_Filter_Input|array validator or error message array
  * @param array $data for validating
  * @param int $validateid (optional) indicates whether id has to be validated
  */
 protected function validate($data, $validateId = false)
 {
     // define filter
     $filterTrim = new Zend_Filter_StringTrim();
     $filter = array('name' => $filterTrim, 'url' => $filterTrim, 'category' => $filterTrim, 'priority' => $filterTrim, 'favicon' => $filterTrim, 'filter' => $filterTrim, 'source' => $filterTrim);
     if (!isset($data['source'])) {
         $data['source'] = '';
     }
     // define validators
     $validatorNotEmpty = new Zend_Validate_NotEmpty();
     $validatorNotEmpty->setMessage(Zend_Registry::get('language')->translate("Value is required and can't be empty"), Zend_Validate_NotEmpty::IS_EMPTY);
     $validatorCategoryId = new application_validate_categoryid();
     $validatorCategoryId->setMessage(Zend_Registry::get('language')->translate("category doesn't exists"), application_validate_categoryid::NOT_EXISTS);
     $validatorSource = new application_validate_source();
     $validatorSource->setMessage(Zend_Registry::get('language')->translate("source doesn't exists"), application_validate_source::NOT_EXISTS);
     $validatorNum = new Zend_Validate_Int(Zend_Registry::get('session')->language);
     $validatorNum->setLocale(Zend_Registry::get('session')->language);
     $validatorNum->setMessage(Zend_Registry::get('language')->translate('Only digits allowed'), Zend_Validate_Int::NOT_INT);
     $validatorNum->setMessage(Zend_Registry::get('language')->translate('Only digits allowed'), Zend_Validate_Int::INVALID);
     $validatorDuplicateFeed = new application_validate_duplicatefeed($data['source'], $validateId ? $data['id'] : false);
     $validatorDuplicateFeed->setMessage(Zend_Registry::get('language')->translate("feed already exists"), application_validate_duplicatefeed::ALREADY_EXISTS);
     $validators = array('name' => array($validatorNotEmpty, Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED), 'url' => array($validatorDuplicateFeed, Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_OPTIONAL), 'category' => array($validatorNum, $validatorCategoryId, Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_OPTIONAL), 'priority' => array($validatorNum, Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED), 'favicon' => array(Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_OPTIONAL, Zend_Filter_Input::ALLOW_EMPTY => true), 'filter' => array(Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_OPTIONAL, Zend_Filter_Input::ALLOW_EMPTY => true), 'source' => array($validatorNotEmpty, $validatorSource, Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED));
     // read from source whether url is optional or not
     if ($validatorSource->isValid($data['source'])) {
         $plugin = Zend_Controller_Action_HelperBroker::getStaticHelper('pluginloader')->getPlugin($data['source']);
         if (!$plugin->sourceOptional && $plugin->source !== false) {
             $validators['url'] = array($validatorNotEmpty, $validatorDuplicateFeed, Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED);
         }
     }
     // insert filter and validate rules for id
     if ($validateId !== false) {
         $validatorFeedId = new application_validate_feedid();
         $validatorFeedId->setMessage(Zend_Registry::get('language')->translate("feed doesn't exists"), application_validate_feedid::NOT_EXISTS);
         $filter['id'] = $filterTrim;
         $validators['id'] = array($validatorNum, $validatorFeedId, Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED);
     }
     // create validation main object
     $validator = new Zend_Filter_Input($filter, $validators, $data, array(Zend_Filter_Input::NOT_EMPTY_MESSAGE => Zend_Registry::get('language')->translate("Value is required and can't be empty"), Zend_Filter_Input::BREAK_CHAIN => false));
     // return filter input object
     return parent::validate($validator);
 }