This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » plugins » General plug-ins » sfMediaLibrary file validation
sfMediaLibrary file validation [message #47945] Sun, 16 March 2008 16:36 Go to next message
jillelaine  is currently offline jillelaine
Messages: 228
Registered: January 2008
Location: West Coast
Faithful Member
I need some help with part of this (see at bottom). Rolling Eyes But first I will post the parts I have working, in case it helps someone else with this nice plugin.

I have installed sfMediaLibraryPlugin-0.9.0. Now to how to validate the uploaded file?

To not touch the plugin folder, make a sfMediaLibrary directory in apps/myapp/modules/

Inside this sfMediaLibrary directory, make 3 new directories: actions, templates, and validate.

Inside validate directory, make file upload.yml:
methods:
  post:               [file]

names:
  file:
    required:         Yes
    required_msg:     Please select a file to upload.
    validators:       myFileValidator
    file:             true

myFileValidator:
  class:              sfFileValidator
  param:
    # give size in bytes
    max_size:         512000
    max_size_error:   This file is too large. Maximum size is 4Mb or 500KB.
    mime_types:
      - 'image/jpeg'
      - 'image/png'
      - 'image/x-png'
      - 'image/pjpeg'
      - 'image/gif'
      - 'application/pdf'
      - 'application/vnd.ms-excel'
      - 'application/msword'
      - 'application/vnd.ms-powerpoint'
      - 'application/zip'
      - 'text/rtf'
      - 'text/plain'
      - 'text/html'
    mime_types_error: This file type is not allowed. Allowed types are .pdf, .doc, .xls, .rtf, .zip, .ppt, .txt, .htm, .jpg, .png and .gif.

NOTE: Internet Explorer may be picky about order of the list of allowed mime_types. The above list is not tested.

Inside your templates directory, make a copy of indexSuccess.php from the plugins/sfMediaLibraryPlugin/modules/sfMediaLibrary/template s, so that you can edit it. The whole file is below, but there are only 2 added lines: <?php use_helper('Validation') ?> and <div class="error"><?php echo form_error('file') ?></div>
<?php use_helper('Javascript', 'I18N') ?>
<?php use_helper('Validation') ?>

<div id="sf_asset_container">
<h1><?php echo __('Media library (%1%)', array('%1%' => $current_dir_slash), 'sfMediaLibrary') ?></h1>
  <div id="sf_asset_content">
    <div id="sf_asset_controls">
      <?php echo form_tag('sfMediaLibrary/upload', 'class=float-left id=sf_asset_upload_form name=sf_asset_upload_form multipart=true') ?>
      <?php echo input_hidden_tag('current_dir', $currentDir) ?>
      <fieldset>
        <div class="error"><?php echo form_error('file') ?></div>
        <div class="form-row">
          <?php echo label_for('file', __('Add a file:', array(), 'sfMediaLibrary'), '') ?>
	  <div class="content"><?php echo input_file_tag('file') ?></div>
        </div>
      </fieldset>
      <ul class="sf_asset_actions">
        <li><?php echo submit_tag(__('Add', array(), 'sfMediaLibrary'), array (
          'name'    => 'add',
          'class'   => 'sf_asset_action_add_file',
          'onclick' => "if($('file').value=='') { alert('".__('Please choose a file first', array(), 'sfMediaLibrary')."');return false; }",
        )) ?></li>
      </ul>
      </form>
      <?php echo form_tag('sfMediaLibrary/mkdir', 'class=float-left id=sf_asset_mkdir_form name=sf_asset_mkdir_form') ?>
      <?php echo input_hidden_tag('current_dir', $currentDir) ?>
      <fieldset>
        <div class="form-row">
          <?php echo label_for('dir', __('Create a dir:', array(), 'sfMediaLibrary'), '') ?>
          <div class="content"><?php echo input_tag('name', null, 'size=15 id=dir') ?></div>
        </div>
      </fieldset>
      <ul class="sf_asset_actions">
        <li><?php echo submit_tag(__('Create', array(), 'sfMediaLibrary'), array (
          'name'    => 'create',
          'class'   => 'sf_asset_action_add_folder',
          'onclick' => "if($('dir').value=='') { alert('".__('Please enter a directory name first', array(), 'sfMediaLibrary')."');return false; }",
        )) ?></li>
      </ul>
      </form>
    </div>
    <div id="sf_asset_assets">
      <?php include_partial('sfMediaLibrary/dirs', array('dirs' => $dirs, 'currentDir' => $currentDir, 'parentDir' => $parentDir, 'is_file' => (count($files) > 0))) ?>
      <?php include_partial('sfMediaLibrary/files', array('files' => $files, 'currentDir' => $currentDir, 'webAbsCurrentDir' => $webAbsCurrentDir, 'count' => count($dirs))) ?>
    </div>
  </div>
</div>

Inside your actions directory, create file actions.class.php. You must require_once the path to BasesfMediaLibraryActions.class.php in this file.
<?php
// TO ADD 1 new function to plugins/sfMediaLibraryPlugin/modules/sfMediaLibrary/lib/BasesfMediaLibraryActions

require_once(sfConfig::get('sf_plugins_dir').'/sfMediaLibraryPlugin/modules/sfMediaLibrary/lib/BasesfMediaLibraryActions.class.php');

class sfMediaLibraryActions extends BasesfMediaLibraryActions
{
  public function handleErrorUpload()
  { 
    //if the file fails validation, we redisplay the upload form with the errors
    $this->forward('sfMediaLibrary', 'index');
  }
}

(I appreciate any comments & suggestions on this validation process! I am just a persistent n00b: there is much I do not understand. Very Happy )

Okay, now time to test. You should receive an error if you try to upload a 'not allowed' mime type, or if your file is too large.

My tests with Firefox works perfectly. Internet Explorer works but so ugly: who writes this buggy piece of crap??

Now to ask for help. Confused
Default sfMediaLibrary behavior is to silently overwrite a file if you upload a file with the same name as an existing file. I have added to sfMediaLibrary a related 'file' table, so that each time a file is uploaded, the filename, filetype, created_at, etc is automatically recorded in this table. I can check this table to see if the filename already exists.

When users upload a file, if the filename already exists, I want to popup a 'confirm' message to explain that old file will be overwritten by new file. Then users can choose to overwrite old file...or cancel and rename old or new file so they do not collide.

I can't figure out how to make a confirm message popup if the filename already exists. Can someone please help?

[Updated on: Sun, 20 April 2008 22:57]

Re: sfMediaLibrary file validation [message #83631 is a reply to message #47945 ] Wed, 12 August 2009 12:58 Go to previous messageGo to next message
kluch  is currently offline kluch
Messages: 6
Registered: July 2009
Junior Member
Soz hey

has anyone figured this out? I also need that popup for file overwrite...
Re: sfMediaLibrary file validation [message #102527 is a reply to message #47945 ] Thu, 15 July 2010 13:45 Go to previous message
apulmca2k4  is currently offline apulmca2k4
Messages: 123
Registered: August 2008
Location: New Delhi, India
Senior Member
I too need it. if you got the solution, please let me know.


Apul Gupta
http://in.linkedin.com/in/apulgupta
Previous Topic:Moving plugin from alpha status
Next Topic:[SOLVED] prestaForumConnectorPlugin synch error
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software