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.

Form Questions

Custom upload validator


Reply

3 Posts   796 Views

Avatar
lku

29 June 2011 at 12:53am Community Member, 8 Posts

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:
[url]https://github.com/silverstripe/sapphire/blob/2.4/forms/FileIFrameField.php#L181[/url]
but without error message, how can i show these messages?

Avatar
zenmonkey

19 July 2011 at 3:15am Community Member, 528 Posts

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

28 July 2011 at 8:25pm Community Member, 83 Posts

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.