Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » Custom upload validator

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 778 Views
  • lku
    Avatar
    Community Member
    8 Posts

    Custom upload validator Link to this post

    Hello,

    in my data object i need validate image dimensions before uploading.
    So i created custom upload validator and it works fine, but i really don't know, how can i propagate my error messages back to upload form.

    I'm using ImageField in data object popup form, here is simplified code of image field definition:

    $imgField = new ImageField('Image');
    $imgField->setValidator(new Custom_ImageValidator());

    If i'm uploading image, that is not valid, form (upload iframe) is redirected back because:
    https://github.com/silverstripe/sapphire/blob/2.4/forms/FileIFrameField.php#L181
    but without error message, how can i show these messages?

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: Custom upload validator Link to this post

    Don't know if this will work in the iframe field but...

    FormResponse::status_message(sprintf('Invalid Size'),'bad');
    return FormResponse::respond();


    .. works for other custom CMS actions http://api.silverstripe.org/2.4/forms/core/FormResponse.html

  • lx
    Avatar
    Community Member
    82 Posts

    Re: Custom upload validator Link to this post

    hi zenmonkey,

    i dont know if this rule really works.
    We added it quiet a while ago and didnt use this rule meanwhile.

    But you could try it.
    Download the netefxValidator module and add a rule like:

    $rule_logoDimensions = new NetefxValidatorRule("Logo", "FUNCTION", array('NetefxValidatorLibrary',
    'check_image_size',
    array('field' => 'Image',
    'minWidth' => 10,
    'maxWidth' => 0,
    'minHeight' => 0,
    'maxHeight' => 500)), "The width of the image has to be at least 10px, the height is not allowed to be more than 500px");

    Of course you could split it in 2 seperate rules

    Let me know if this rule doenst work.

    778 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.