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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Validate image on save


Go to End


6 Posts   1616 Views

Avatar
Rodskagg

Community Member, 26 Posts

4 April 2015 at 11:49am

I'm trying to validate the dimensions of an image when saving a page, but have run into an issue. In the validate method I check if the image exists. The problem is that the image always refers to the image selected when the page editor form was loaded. If I edit a page with an already selected image, remove the image and then clicks Save, the image object refers to the image I just deleted. I expect it to be null however, since I just removed it from the image field.

Avatar
Nicolaas

Forum Moderator, 224 Posts

5 April 2015 at 8:02am

Hi

how do you try to validate the image? Are you using the Validate method? if so, where, etc...?

Avatar
Rodskagg

Community Member, 26 Posts

5 April 2015 at 8:51am

Edited: 05/04/2015 8:52am

I'm using the validate method in my Page class. Maybe that's the wrong way to do it?

public function validate() {
        $result = parent::validate();
        $image = $this->ListImage();
        if ($image->exists()) {
	        $requiredAspectRatio = 294/150;
	        $aspectRatioOfImage = $image->getWidth()/$image->getHeight();
	        if ($requiredAspectRatio != $aspectRatioOfImage) {
		        $result->error("Wrong aspect ratio");
	        }
        }
        return $result;
    }

Avatar
grgcnnr

Community Member, 5 Posts

13 July 2016 at 9:26am

Rodskagg, did you ever get to the bottom of this? I'm seeing a similar issue.

Avatar
Rodskagg

Community Member, 26 Posts

28 July 2016 at 8:17pm

I never managed to solve this, unfortunately.

Avatar
grgcnnr

Community Member, 5 Posts

29 July 2016 at 9:35am

So this is basically Versioned:: doing a number of passes as it writes a sitetree descendant class (or any versioned class) You probably won't have this issue with plain old data objects.

The way Validate seems to work when you hit save and publish is something like:

  • Validate the current (old) data
  • Validate the new data before writing
  • Validate the new data after writing to stage
  • Validate after writing to live

So where we run into trouble is if you write a new validation rule that makes the current (old) data invalid you will never get pas the first validation pass and on to the second with your new data.

If you want to look at this a bit more closely, i'd be keen to see your full code.