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.
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.
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:
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.
@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.
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:
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 :)
Hi Stuart, I'll get back to you about this on Monday. Have a great weekend!
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:
similarly, if you wanted the field to be required based on some parameter of your scope, you can use ng-required instead:
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:
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.
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 :)
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!!
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)
Ah curses!
Hoping not to have to do that - oh well :)
Thanks for your help!
Comment author was deleted
@shannon, thanks for the lead.
@Stuart, I did an example here: http://bit.ly/JDNpyI (Have to wade thru it all)
@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
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 :)
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 :)
is working on a reply...