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.
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.
Hi
how do you try to validate the image? Are you using the Validate method? if so, where, etc...?
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;
}
Rodskagg, did you ever get to the bottom of this? I'm seeing a similar issue.
I never managed to solve this, unfortunately.
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.