Copied to clipboard

Flag this post as spam?

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


  • Stuart Burrows 61 posts 110 karma points
    Dec 19, 2013 @ 13:57
    Stuart Burrows
    0

    Custom property editors: how to stop a page from being saved

    I've been playing with custom property editors but I can't figure out how to stop the page saving when I know there is an error in the data.

    I've made a workaround:

    $scope.$on("formSubmitting", function (ev, args) {
        $scope.hasError = validateJSON( myJSON );
    
        if ( !$scope.hasError )
        {
            $scope.model.value = myJSON;
        }
        else
        {
            notificationsService.error( "Error", "The page has not been updated." );
        }
    });

    This will save the page but not update my custom property with malformed JSON. The problem being that the user gets a success message and an error message which is pretty silly. I'm sure there is an easy way to do this but I can't find anything.

    Any help will be appreciated!

    Thanks :)

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Dec 21, 2013 @ 01:59
    Shannon Deminick
    0

    Hi Stuart, I'll get back to you about this on Monday. Have a great weekend!

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Dec 24, 2013 @ 00:02
    Shannon Deminick
    4

    Hi,

    We use standard angular js validation for all of our form validation. By setting a part of the form to invalid, this will restrict the form from submitting.

    Here's the docs for angularjs validation: http://docs.angularjs.org/guide/forms

    As an example, if you have a text field as a property editor or pre-value editor, and you want to make this required on the client side - thus preventing form submission, you'd do this:

    <input name="myField" type="text" class="umb-editor umb-textstring"
        ng-model="model.value"
        required />
    

    similarly, if you wanted the field to be required based on some parameter of your scope, you can use ng-required instead:

    <input name="myField" type="text" class="umb-editor umb-textstring"
        ng-model="model.value"
        ng-required="isRequired" />
    

    now this field is only required when your scope.isRequired is true.

    We have a few helper directives for validation, one if which is to show validation messages nicely in Umbraco. Here's a complete example:

    <div>
        <input name="myField" type="text" class="umb-editor umb-textstring"
            ng-model="model.value"
            required />
    
            <span class="help-inline" val-msg-for="myField" val-toggle-msg="required">Required</span>
    
    </div>
    

    This will show the message for the 'required' validation directive once the user tries to submit the form if the required validation for the 'myField' is invalid.

    We'll be documenting all of the validation techniques we use in v7 soon. In the meantime, your best option is to read up and try the standard angularjs validation techniques and then have a look at our source code for property editors and pre-value editors.

  • Stuart Burrows 61 posts 110 karma points
    Jan 02, 2014 @ 10:18
    Stuart Burrows
    0

    Hi Shannon,

    Thanks for your response. I've been away for Christmas so only just looking at this. I will give it a go and let you know, hopefully today.

     

    I'm pretty sure I tried this but maybe I got some aspect of the syntax wrong... or maybe hasError is added too late (??)

     

    Thanks again :)

  • Stuart Burrows 61 posts 110 karma points
    Jan 02, 2014 @ 11:31
    Stuart Burrows
    0

    Right so I've had a few minutes to look at this. I think the issue is where my hasError is being set.

    I've done everything as shown above but it won't work. The issue is when the error is found. I think I need a function to run before $on("formSubmit")

    I'll keep investigating but if there is an earlier function or standard way around this issue please do let me know.

     

    Thanks!!

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Jan 03, 2014 @ 07:41
    Shannon Deminick
    101

    Your hasError doesn't seem to be a part of the angular validation process, validation happens in real time in angular, not when the form submits.

    If your validation is based on validateJSON( myJSON );

    then you'd need to do create your own validator, see the Custom Validation section of that link (http://docs.angularjs.org/guide/forms)

  • Stuart Burrows 61 posts 110 karma points
    Jan 03, 2014 @ 10:13
    Stuart Burrows
    0

    Ah curses!

    Hoping not to have to do that - oh well :)

    Thanks for your help!

  • Comment author was deleted

    Jan 06, 2014 @ 17:58

    @shannon, thanks for the lead.

    @Stuart, I did an example here: http://bit.ly/JDNpyI  (Have to wade thru it all)

  • Stuart Burrows 61 posts 110 karma points
    Jan 06, 2014 @ 18:12
    Stuart Burrows
    0

    @kevin, thanks for that - really helpful. I've got mine pretty much working but I'm using a 3rd party bit of code and I need to find a place to add a callback. I think... I need to run an error checking function (non-angular). It's a bit like your updateWeightedTotal() function but without the option of just running an .on('slideStop'). I'm using contenteditable divs etc so I might end up going down the .on('blur') route... we'll see.

    Having said that you have done a bunch of stuff I don't understand as well so I'll be going through it with a tab open on the angular docs as soon as I have some more time.

    Thanks again! :)

  • Comment author was deleted

    Jan 06, 2014 @ 18:56

    I think you'll need to do a 'directive' and do you error checking with a $formatter or $parser.

    Angularjs is still mind-boggling at times for me :)

  • Stuart Burrows 61 posts 110 karma points
    Jan 06, 2014 @ 19:09
    Stuart Burrows
    0

    Yeah those are the things I need to read up on!

    Got some ideas to try out, I'll let you know if I have any success :)

Please Sign in or register to post replies

Write your reply to:

Draft