Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Greg Fyans 140 posts 342 karma points
    Oct 19, 2023 @ 12:54
    Greg Fyans
    0

    Custom Property Editor, complex model validation

    How do you validate a complex object in a custom property editor? This is my formSubmitting...

    $scope.$on('formSubmitting', (e, args) => {
        $scope.errors = [];
        $scope.modelIsValid = true;
        let errorsFound = false;
    
        if (!$scope.properties[0].mediaPicker.value || $scope.properties[0].mediaPicker.value.length <= 0) {
            $scope.errors.push('Desktop image must be selected');
            errorsFound = true;
        }
    
        $scope.model.value[0].desktopBreakpointImage = $scope.properties[0].mediaPicker.value;
        $scope.model.value[0].desktopImageAspectRatio = $scope.properties[0].aspectRatio.value;
        $scope.model.value[0].smallDesktopBreakpointImage = $scope.properties[1].mediaPicker.value;
        $scope.model.value[0].smallDesktopImageAspectRatio = $scope.properties[1].aspectRatio.value;
        $scope.model.value[0].tabletBreakpointImage = $scope.properties[2].mediaPicker.value;
        $scope.model.value[0].tabletImageAspectRatio = $scope.properties[2].aspectRatio.value;
        $scope.model.value[0].mobileBreakpointImage = $scope.properties[3].mediaPicker.value;
        $scope.model.value[0].mobileImageAspectRatio = $scope.properties[3].aspectRatio.value;
    
        if (errorsFound) {
            // prevent the publish here?
            $scope.modelIsValid = false;
        }
    });
    

    I have 4 media pickers, and only the first one is mandatory.

    I can set my data type to "Mandatory" but this obviously doesn't validate because I have a complex object as my value.

    As a test I tried setting my $scope.model.value to null to see what happened, and while I do get a validation error from the data type saying "Value cannot be null", the page still saves and publishes with the null value. I would've expected the Mandatory flag to prevent this.

    Ideally I want to show an alert with the contents of my $scope.errors property and prevent the publishing of the content.

Please Sign in or register to post replies

Write your reply to:

Draft