Copied to clipboard

Flag this post as spam?

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


  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Feb 10, 2017 @ 07:52
    David Brendel
    0

    Validation in custom section

    Hi,

    I'm currently trying to get validation going in a custom section I created. The properties on the view that I used are part custom; just textboxes and so on and part umbraco property editors where I fetch the config via API call.

    Now the problem that I'm facing is with date/time-picker. The picked value is shown correctly but the form still stays on error state. So my tab where the pickers are on is red to indicate errors on the page. When I check if the form is valid by using $scope.form.$valid it says that it has pickerErrors.

    I tried using the formHelper to do validation and it returns no errors. So the form seems to be valid for the form helper.

    I'm rendering the property editors like this:

    <umb-property property="picker" ng-repeat="picker in startDateTimePickers">
        <umb-editor model="picker"></umb-editor>
    </umb-property>
    

    I create the editor like this:

    ecDataTypeResource.getByName("EventCalendar DateTime Picker").then(function (response) {
                        var pickerConfig = response.data;
                        var startDateTimePicker = {
                            alias: pickerConfig.propertyEditorAlias,
                            view: pickerConfig.view,
                            config: pickerConfig.config,
                            label: 'DateTime',
                            hideLabel: true,
                            value: $scope.event.DateStart,
                            name: 'dateStart'
                        };
                        if ($scope.event.Type == 1) {
                            startDateTimePicker.validation = {
                                required: true
                            };
                        }
                        $scope.startDateTimePickers = [startDateTimePicker];
                    });
    

    My form is rendered like this in the view:

    <form novalidate val-form-manager
          name="eventForm"
          id="eventForm"
          class="show-validation"
          novalidate
          ng-submit="save()">
    

    And I check if the form is valid like this:

    if($scope.eventform.$valid) { //save form data here }
    

    I move the selected date value with a $watch to my model:

    $scope.$watch('startDateTimePickers', function (newValue, oldValue) {
                if (newValue !== undefined) {
                    $scope.event.DateStart = newValue[0].value;
                }
    }, true);
    

    I tried to use the formHelper to check validy with:

    if (formHelper.submitForm({ scope: $scope, formCtrl: $scope.eventForm }))
    {
    //save stuff
    }
    

    interesting fact is my create and edit method uses the same controller and views. During creation the validation works as expected but not when editing.

    One speciality might be that I have multiple tabs in my edit view with each tab has its own view and controller handling loading of assets or handling the watches like mentioned above.

    Any help?

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Feb 10, 2017 @ 12:04
    Morten Bock
    0

    So the datepicker works and validates correctly in the create view?

    And on the edit page, the datepicker is nested within an additional controller?

    Could it be somehow related to nested $scopes?

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Feb 10, 2017 @ 12:38
    David Brendel
    0

    Yes it works correctly when creating an entity. But the view/controller is the same when creating or editing.

    Basic setup of my pages is on view for the general edit view. Depending on the entity type it includes different additional views in its main body.

    The picker itself also creates a scope and has also a controller. Default umbraco behaviour.

    My problem is that I don't understand what the difference is between create and edit. Besides the values that are passed to the date picker when it is created. When on create its moment(new Date()).format('picker_format'). When on edit its moment(entty.date).format('picker_format').

    when checking the pickerErrors that I output to console it shows that modelValue of the picker is 'Invalid Date'. Which could be the reason - but it shows the date correctly and also changing the date doesn't remove the error.

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Feb 10, 2017 @ 13:52
    Morten Bock
    0

    It might help to take a look at the source:

    https://github.com/umbraco/Umbraco-CMS/tree/dev-v7/src/Umbraco.Web.UI.Client/src/views/propertyeditors/datepicker

    It looks like the pickerErrors is set explicitly using $setValidity, so it might be useful if you can debug into that, and see why the date is invalid. There might be some regex or similar going on?

    Is your entty.date a Date object? Or could that be a string?

    Maybe you could even copy paste the source into your own directive and debug the issue that way?

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Feb 10, 2017 @ 15:04
    David Brendel
    0

    That hint with the date object and string is indeed an interesting idea. But I think moment should return strings in both use-cases; with date object or the entity.date. Which is a DateTime parsed to json.

    I will have a look at the source and try a bit with string and date objects and see what I can find out.

    Thanks for the hint and the help!

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Feb 10, 2017 @ 23:21
    David Brendel
    100

    Ok I think I found it and it was my own stupidity.

    I had more then one date/time picker on my view. And in fact the error was thrown by some that were not shown as they were not needed for that specific entity.

    So after removing them from the DOM it worked. Now on to test everything. Like create and edit of all my entity shapes.

    Thanks anyway for the hints!

Please Sign in or register to post replies

Write your reply to:

Draft