Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | 354x 354x 458x 458x 458x 458x 238x 148x 148x 148x 148x 5831x 5831x 486x 5831x 5831x 93x 93x 93x | 'use strict';
/**
 * Validation extension
 *
 * @author    Julien Sanchez <julien@akeneo.com>
 * @author    Filips Alpe <filips@akeneo.com>
 * @author    Yohan Blain <yohan.blain@akeneo.com>
 * @copyright 2015 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
define(
    [
        'jquery',
        'underscore',
        'backbone',
        'pim/form',
        'oro/mediator',
        'oro/messenger',
        'pim/field-manager',
        'pim/product-edit-form/attributes/validation-error',
        'pim/user-context'
    ],
    function ($, _, Backbone, BaseForm, mediator, messenger, FieldManager, ValidationError, UserContext) {
        return BaseForm.extend({
            validationErrors: {},
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.listenTo(this.getRoot(), 'pim_enrich:form:entity:pre_save', this.onPreSave);
                this.listenTo(this.getRoot(), 'pim_enrich:form:entity:bad_request', this.onValidationError);
                this.listenTo(this.getRoot(), 'pim_enrich:form:field:extension:add', this.addFieldExtension);
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * Pre save callback
             */
            onPreSave: function () {
                this.validationErrors = {};
            },
 
            /**
             * On validation callback
             *
             * @param {Event} event
             */
            onValidationError: function (event) {
                this.validationErrors = event.response;
                var globalErrors = _.where(this.validationErrors.values, {global: true});
 
                // Global errors with an empty property path
                _.each(globalErrors, function (error) {
                    messenger.notify('error', error.message);
                });
 
                this.getRoot().trigger('pim_enrich:form:entity:validation_error', event);
            },
 
            /**
             * On field extension
             *
             * @param {Event} event
             */
            addFieldExtension: function (event) {
                var field = event.field;
                var valuesErrors = _.uniq(this.validationErrors.values, function (error) {
                    return JSON.stringify(error);
                });
 
                var errorsForAttribute = _.where(valuesErrors, {attribute: field.attribute.code});
 
                if (!_.isEmpty(errorsForAttribute)) {
                    this.addErrorsToField(field, errorsForAttribute);
                }
            },
 
            /**
             * Add an error to a field
             *
             * @param {Object} field
             * @param {Array}  fieldError
             */
            addErrorsToField: function (field, fieldErrors) {
                field.addElement(
                    'footer',
                    'validation',
                    new ValidationError(fieldErrors, this)
                );
 
                field.setValid(false);
            },
 
            /**
             * Change the current context
             *
             * @param {[type]} locale
             * @param {[type]} scope
             */
            changeContext: function (locale, scope) {
                if (locale) {
                    UserContext.set('catalogLocale', locale);
                }
 
                if (scope) {
                    UserContext.set('catalogScope', scope);
                }
            }
        });
    }
);
  |