Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sherry Ann Hernandez 320 posts 344 karma points
    Feb 28, 2011 @ 12:17
    Sherry Ann Hernandez
    0

    How to raise a custom validator when saving a media

    Hi,

    Is is possible to raise a custom validator when uploading a media?

    I have this image resizer datatype that I got from daniel. I'm customizing it to raise an error when the image size exceeds 120kb. How can I do that?

    Here's my code

     

     

    internal bool validateFileSizePath(string origFile)

    {

     

     

    try

    {

     

     

    HttpServerUtility map = HttpContext.Current.Server;

     

     

    string path = map.MapPath(origFile);

    System.IO.

     

    FileInfo info = new System.IO.FileInfo(path);

     

     

    if (info.Length <= 120000)

    {

     

     

    return true;

    }

     

     

    else

    {

    _customValidator.ControlToValidate = _imageResizerImage.ImageUrl;

    _customValidator.Validate();

    _customValidator.ErrorMessage =

     

    "Invalid image size. Maximum image size should be 120kb";

     

     

    return false;

    }

    }

     

     

    catch (Exception ex)

    {

    _customValidator.ControlToValidate = _imageResizerImage.ImageUrl;

    _customValidator.Validate();

    _customValidator.ErrorMessage =

     

    "Invalid image size. Maximum image size should be 120kb. " + ex;

     

     

    return false;

    }

     

    }

    and then I call it in 

    private

     

     

    void DataEditorControl_OnSave(EventArgs e)

    {

     

     

    var prevalueEditor = (ImageResizerPrevalueEditor)PrevalueEditor;

     

     

     

    if (_imageResizerDataEditor.validateFileSizePath(ImageResizerHelper.GetOriginalUrl(ImageResizerHelper.CurrentNodeId, prevalueEditor)))

    {

     

     

    var imageUrl = ImageResizerHelper.ResizeImage(ImageResizerHelper.GetOriginalUrl(ImageResizerHelper.CurrentNodeId, prevalueEditor), prevalueEditor.MaxHeight, prevalueEditor.MaxHeight);

    _imageResizerDataEditor.ImageUrl = imageUrl;

     

     

    base.Data.Value = _imageResizerDataEditor.ImageUrl;

    }

    }

    Am I getting this right?
  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Mar 01, 2011 @ 13:35
    Tim
    0

    Hi Sherry,

    I think the problem may be that the custom validator needs to be assigned to the same group as the other page validation controls.

    Are you getting an error, or is the validator just not firing?

  • Sherry Ann Hernandez 320 posts 344 karma points
    Mar 01, 2011 @ 14:05
    Sherry Ann Hernandez
    0

    Sometimes it is generating an error and it is not firing.

    I tried to create a BeforeSaveEventHandler class but i'm not sure where to call it in the dataeditor class.

    How do you add the custom validator to the page validation controls?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies