@@ -420,11 +420,11 @@ export const dialog_open = (def) => {
420
420
function run_action ( progress_callback , variant ) {
421
421
const func = ( ) => {
422
422
return validate ( variant )
423
- . then ( ( ) => {
423
+ . then ( validated_values => {
424
424
const visible_values = { variant } ;
425
425
fields . forEach ( f => {
426
426
if ( is_visible ( f , values ) )
427
- visible_values [ f . tag ] = values [ f . tag ] ;
427
+ visible_values [ f . tag ] = validated_values [ f . tag ] ;
428
428
} ) ;
429
429
if ( def . Action . wrapper )
430
430
return def . Action . wrapper ( visible_values , progress_callback ,
@@ -506,16 +506,32 @@ export const dialog_open = (def) => {
506
506
} ;
507
507
508
508
const validate = ( variant ) => {
509
+ // The validation functions sometimes change the dialog values
510
+ // for the benefit of the action functions. For example, a
511
+ // SizeSlider will convert from "text plus unit" to a numeric
512
+ // value during validation.
513
+ //
514
+ // However, if the action fails, we don't want the values in
515
+ // the dialog to change. The SizeSlider would convert back
516
+ // from a numeric value to "text plus unit", for example, and
517
+ // that conversion might change what the user had type.
518
+ //
519
+ // So we make a copy of the dialog state and let the validate
520
+ // and action functions work with that.
521
+ //
522
+ const validated_values = { ...values } ;
523
+
509
524
return Promise . all ( fields . map ( f => {
510
525
if ( is_visible ( f , values ) && f . options && f . options . validate )
511
- return f . options . validate ( values [ f . tag ] , values , variant ) ;
526
+ return f . options . validate ( validated_values [ f . tag ] , validated_values , variant ) ;
512
527
else
513
528
return null ;
514
529
} ) ) . then ( results => {
515
530
const errors = { } ;
516
531
fields . forEach ( ( f , i ) => { if ( results [ i ] ) errors [ f . tag ] = results [ i ] ; } ) ;
517
532
if ( Object . keys ( errors ) . length > 0 )
518
533
return Promise . reject ( errors ) ;
534
+ return validated_values ;
519
535
} ) ;
520
536
} ;
521
537
0 commit comments