22
33var __ = require ( 'underscore' ) ,
44 Backbone = require ( 'backbone' ) ,
5- $ = require ( 'jquery' ) ;
6- Backbone . $ = $ ;
7-
8- var loadTemplate = require ( '../utils/loadTemplate' ) ,
5+ $ = require ( 'jquery' ) ,
6+ loadTemplate = require ( '../utils/loadTemplate' ) ,
97 countriesModel = require ( '../models/countriesMd' ) ,
108 Taggle = require ( 'taggle' ) ,
119 MediumEditor = require ( 'medium-editor' ) ,
@@ -69,6 +67,7 @@ module.exports = baseVw.extend({
6967 this . prevShipsToVal = [ ] ;
7068 this . defaultDate = nowDate . getFullYear ( ) + "-" + padTime ( nowMonth ) + "-" + padTime ( nowDate . getDate ( ) ) + "T" + padTime ( nowDate . getHours ( ) ) + ":" + padTime ( nowDate . getMinutes ( ) ) ;
7169 this . imgHashes = this . model . get ( 'vendor_offer' ) . listing . item . image_hashes ;
70+ __ . bindAll ( this , 'validateDescription' ) ;
7271
7372 self . model . set ( 'expTime' , self . model . get ( 'vendor_offer' ) . listing . metadata . expiry . replace ( " UTC" , "" ) ) ;
7473
@@ -111,7 +110,6 @@ module.exports = baseVw.extend({
111110 } ) ;
112111
113112 editor . subscribe ( 'blur' , self . validateDescription ) ;
114-
115113
116114 //set chosen inputs
117115 self . $ ( '.chosen' ) . chosen ( {
@@ -194,7 +192,15 @@ module.exports = baseVw.extend({
194192 tags : keywordTags ,
195193 preserveCase : true ,
196194 saveOnBlur : true ,
197- placeholder : window . polyglot . t ( 'KeywordsPlaceholder' )
195+ placeholder : window . polyglot . t ( 'KeywordsPlaceholder' ) ,
196+ onTagAdd : ( ) => {
197+ this . $ ( '#inputKeyword' ) . removeClass ( 'invalid' ) ;
198+ } ,
199+ onTagRemove : ( ) => {
200+ if ( ! self . inputKeyword . getTags ( ) . elements . length ) {
201+ self . $ ( '#inputKeyword' ) . addClass ( 'invalid' ) ;
202+ }
203+ }
198204 } ) ;
199205 } , 0 ) ;
200206
@@ -296,7 +302,12 @@ module.exports = baseVw.extend({
296302 $ ( e . target ) . val ( newVal ) ;
297303 this . $ ( '.chosen' ) . trigger ( 'chosen:updated' ) ;
298304 this . prevShipsToVal = newVal ;
299- this . $ ( '.js-shipToWrapper' ) . removeClass ( 'invalid' ) ;
305+
306+ if ( newVal . length ) {
307+ this . $ ( '.js-shipToWrapper' ) . removeClass ( 'invalid' ) ;
308+ } else {
309+ this . $ ( '.js-shipToWrapper' ) . addClass ( 'invalid' ) ;
310+ }
300311 } ,
301312
302313 clearDate : function ( ) {
@@ -351,6 +362,7 @@ module.exports = baseVw.extend({
351362
352363 if ( ! imageFiles . length ) return ;
353364
365+ this . $ ( '#imageForm .invalid' ) . removeClass ( 'invalid' ) ;
354366 imageCount = imageFiles . length ;
355367 this . $el . find ( '.js-itemEditImageLoading' ) . removeClass ( "fadeOut" ) ;
356368
@@ -488,6 +500,7 @@ module.exports = baseVw.extend({
488500
489501 this . imgHashes . splice ( imgIndex , 1 ) ;
490502 this . updateImages ( ) ;
503+ ! this . imgHashes . length && this . $ ( '.js-editItemSubImagesWrapper' ) . addClass ( 'invalid' ) ;
491504 } ,
492505
493506 validateInput : function ( e ) {
@@ -500,7 +513,7 @@ module.exports = baseVw.extend({
500513
501514 validateDescription : function ( ) {
502515 validateMediumEditor . checkVal ( this . $ ( '#inputDescription' ) ) ;
503- } ,
516+ } ,
504517
505518 saveChanges : function ( ) {
506519 var self = this ,
@@ -509,16 +522,15 @@ module.exports = baseVw.extend({
509522 submitForm = this . $el . find ( '#contractForm' ) [ 0 ] ,
510523 keywordsArray = this . inputKeyword . getTagValues ( ) ,
511524 shipsToInput = this . $ ( '#shipsTo' ) ,
512- invalidInputList = "" ;
525+ invalidInputList = [ ] ,
526+ hasError ;
513527
514528 validateMediumEditor . checkVal ( this . $ ( '#inputDescription' ) ) ;
515529
516530 if ( keywordsArray . length < 1 ) {
517- this . $ ( '#inputKeyword' ) . closest ( '.flexRow' ) . addClass ( 'invalid' ) ;
518- messageModal . show ( window . polyglot . t ( 'errorMessages.saveError' ) , window . polyglot . t ( 'errorMessages.missingError' ) + "<br><i>" + window . polyglot . t ( 'Tags' ) + "</i>" ) ;
519- return $ . Deferred ( ) . reject ( 'failed form validation' ) . promise ( ) ;
531+ this . $ ( '#inputKeyword' ) . addClass ( 'invalid' ) ;
532+ hasError = true ;
520533 }
521- this . $ ( '#inputKeyword' ) . closest ( '.flexRow' ) . removeClass ( 'invalid' ) ;
522534
523535 keywordsArray = keywordsArray . map ( function ( tag ) {
524536 var re = / # / g;
@@ -542,8 +554,7 @@ module.exports = baseVw.extend({
542554 //make sure a ships to value is entered
543555 if ( ! shipsToInput . val ( ) && ! this . noShipping ) {
544556 this . $ ( '.js-shipToWrapper' ) . addClass ( 'invalid' ) ;
545- messageModal . show ( window . polyglot . t ( 'errorMessages.saveError' ) , window . polyglot . t ( 'errorMessages.missingError' ) ) ;
546- return $ . Deferred ( ) . reject ( 'failed form validation' ) . promise ( ) ;
557+ hasError = true ;
547558 }
548559
549560 //add old and new image hashes
@@ -554,12 +565,17 @@ module.exports = baseVw.extend({
554565 }
555566 } ) ;
556567
557- if ( keywordsArray . length > 0 ) {
568+ if ( ! this . imgHashes . length ) {
569+ this . $ ( '.js-editItemSubImagesWrapper' ) . addClass ( 'invalid' ) ;
570+ hasError = true ;
571+ }
572+
573+ if ( keywordsArray . length ) {
558574 __ . each ( keywordsArray , function ( keyword ) {
559575 formData . append ( 'keywords' , keyword ) ;
560576 } ) ;
561577 } else {
562- formData . append ( 'keywords' , "" ) ;
578+ formData . append ( 'keywords' , '' ) ;
563579 }
564580
565581 //if this is an existing product, do not delete the images
@@ -581,7 +597,7 @@ module.exports = baseVw.extend({
581597 //add formChecked class to form so invalid fields are styled as invalid
582598 this . $el . find ( '#contractForm' ) . addClass ( 'formChecked' ) ;
583599
584- if ( this . checkFormValidity ( ) ) {
600+ if ( this . checkFormValidity ( ) && ! hasError ) {
585601 return $ . ajax ( {
586602 type : "POST" ,
587603 url : self . model . get ( 'serverUrl' ) + "contracts" ,
@@ -603,13 +619,20 @@ module.exports = baseVw.extend({
603619 }
604620 } ) ;
605621 }
606- $ ( submitForm ) . find ( 'input, textarea' ) . each ( function ( ) {
607- if ( $ ( this ) . is ( ":invalid" ) ) {
608- var inputName = $ ( "label[for='" + $ ( this ) . attr ( 'id' ) + "']" ) . text ( ) || $ ( this ) . attr ( 'id' ) ;
609- invalidInputList += "<br/>" + inputName ;
610- }
622+
623+ $ ( submitForm ) . add ( this . $ ( '#imageForm' ) ) . find ( ':invalid, .invalid' ) . each ( function ( ) {
624+ var inputName ,
625+ $label ;
626+
627+ inputName = ( ( $label = self . $ ( "label[for='" + $ ( this ) . attr ( 'id' ) + "']" ) ) . length && $label . text ( ) ) ||
628+ $ ( this ) . attr ( 'data-label' ) || $ ( this ) . attr ( 'id' ) ;
629+
630+ invalidInputList . push ( inputName . trim ( ) )
611631 } ) ;
612- messageModal . show ( window . polyglot . t ( 'errorMessages.saveError' ) , window . polyglot . t ( 'errorMessages.missingError' ) + "<br><i>" + invalidInputList + "</i>" ) ;
632+
633+ invalidInputList = __ . uniq ( invalidInputList ) ;
634+ messageModal . show ( window . polyglot . t ( 'errorMessages.saveError' ) , window . polyglot . t ( 'errorMessages.missingError' ) +
635+ '<br><i><br />' + invalidInputList . join ( '<br />' ) + '</i>' ) ;
613636 return $ . Deferred ( ) . reject ( 'failed form validation' ) . promise ( ) ;
614637 } ,
615638
0 commit comments