Example #1
0
File: asset.php Project: awd/assets
 /**
 * Before Validate runs before the model is validated.
 * Here all the model validations are set for Asset Behavior instance
 * 
 * @param object $model Model using this behavior
 * @access public
 */
 function beforeValidate(&$model)
 {
     if ($this->errors[$model->name] === true) {
         $this->_setError($model, 'Critical Error found while starting beforeValidate: ' . $model->name, 'error');
         return false;
     }
     foreach ($this->_settings($model) as $name => $setting) {
         $fieldName = Inflector::underscore($name);
         # determine when to validate
         $validatesOn = !empty($setting['on']) ? $setting['on'] : null;
         # shortcut to determine if we are creating, updating or neither
         $exists = !empty($model->data[$model->name][$model->primaryKey]) && is_numeric($model->data[$model->name][$model->primaryKey]) ? true : false;
         # prevent validations from being added when they are not required (always|create|update)
         if (empty($validatesOn) || $validatesOn == 'create' && !$exists || $validatesOn == 'update' && $exists) {
             # validate against the field itself
             $validations['uploadField'] = array('allowEmpty' => $setting['allowEmpty'], 'last' => true, 'rule' => array('validateUploadField', $fieldName), 'message' => 'AssetBehavior expects field named: "' . $model->name . '.' . $fieldName . '", please ensure this form can accept file-uploads.', 'on' => $validatesOn, 'required' => $setting['required']);
             # ensure the upload is valid and data exists
             $validations['uploadData'] = array('rule' => array('validateUploadData', $fieldName, $setting['allowEmpty']), 'message' => !empty($setting['message']) ? $setting['message'] : 'Please provide a file attachment.');
         } else {
             # always check if the required field is present - when required by config
             if (!empty($setting['required']) && $setting['required'] == true || $this->required == true) {
                 # validate against the field itself
                 $validations['uploadField'] = array('allowEmpty' => $setting['allowEmpty'], 'last' => true, 'rule' => array('validateUploadField', $fieldName), 'message' => 'AssetBehavior expects field named: "' . $model->name . '.' . $fieldName . '", please ensure this form can accept file-uploads.', 'required' => $setting['required']);
             }
         }
         #endif
         # the following validations will only occur when data exists (handled within the validation)
         # validate against the upload storage path
         if (!empty($model->data[$model->alias][$fieldName]['tmp_name'])) {
             $validations['uploadPath'] = array('allowEmpty' => false, 'last' => true, 'rule' => array('validateUploadPath'), 'message' => 'AssetBehavior cannot write to the "files" directory. Please contact an administrator.', 'required' => true);
         }
         # validate the file size
         $validations['uploadSize'] = array('last' => true, 'rule' => array('validateUploadSize', $fieldName, $setting['maxSize']), 'message' => 'The file size must not exceed: ' . assetHumanizeSize($setting['maxSize']));
         # validate the file extension
         if (!empty($setting['allowMimes']) && in_array('*', $setting['allowMimes'])) {
             $message = 'Please ensure the file is one of the following: ' . strtolower(assetStringify($setting['allowedExts'])) . '.';
         } else {
             $message = 'Please ensure the file extension is valid for [' . assetStringify($setting['allowedMimes']) . '] files.';
         }
         $validations['fileExtension'] = array('last' => true, 'rule' => array('validateFileExtension', $fieldName, $setting['allowedExts']), 'message' => $message);
         # validate the file mime-type
         $validations['fileMime'] = array('last' => true, 'rule' => array('validateFileMime', $fieldName, $setting['allowedMimes']), 'message' => 'Please ensure the mime-type is valid for [' . assetStringify($setting['allowedMimes']) . '] files.');
         # validate the file dimensions (if image or swf)
         if (!empty($setting['dimensions'])) {
             $validations['fileDimensions'] = array('last' => true, 'rule' => array('validateFileDimensions', $fieldName, $setting['dimensions']), 'message' => 'Please ensure the file dimensions match the requirements designated above.');
         }
         # merge new custom validations with existing validations
         $model->validate[$fieldName] = $validations;
     }
 }
Example #2
0
 /**
  * test stringify function
  * 
  * @return void
  */
 function testAssetStringify()
 {
     $result = assetStringify(array('first', 'second', 'third'));
     $expected = "First, Second, Third";
     $this->assertEqual($result, $expected);
     $result = assetStringify(array('first', 'second', 'third'));
     $expected = "first, second, third";
     $this->assertNotEqual($result, $expected);
     $result = assetStringify("random string here");
     $expected = "random string here";
     $this->assertEqual($result, $expected);
 }