Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Form Questions /

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

Custom upload validator


Go to End


3 Posts   1539 Views

Avatar
lku

Community Member, 8 Posts

29 June 2011 at 12:53am

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?

Avatar
zenmonkey

Community Member, 545 Posts

19 July 2011 at 3:15am

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

Avatar
lx

Community Member, 83 Posts

28 July 2011 at 8:25pm

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.