Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    dateFormatter and inputFormat

    I have a few problems with regards to dateFormatter and inputFormat. We have overwritten these two properties and provided our own implementation. Our logic includes custom formatting as well as validation. We have a validation flag on the element that we maintain called "$cwInvalid". This is originally set in the inputFormat function. When dateFormatter is called, it checks if this flag is set and if the date object coming in is invalid (because changing the value from the picker doesn't call inputFormat, therefore we would not have set the flag), return the last known string value (we want to keep the invalid value on the screen). If it is not currently set, then we format it and return the display string.

    My problem right now is that dateFormatter is not only called to format the value coming in, it is also called to format the previous value to check if the value has changed which messes up our validation flag logic. The validation flag is wiped out because a previous value is valid.

    This is the same issue in inputFormat because it is not only called when the user inputs some values, it is also called for formatting the startDate. when startDate comes in, my validation flag is wiped out because it is a valid value and I have no idea that it is in fact just for the startDate and not for the actual value.

    What I would like to know is if there is any way around this? Can I somehow detect where the value is coming from? The key things I want to achieve:

    - when the user has entered an invalid value, I want that invalid value to stay on the screen.
    - when the user has entered a valid value, I want the valid value to appear and be saved on the date element.
    - when the user enters and invalid value then a valid value, I want the valid value to be saved.
    - when the user enters a valid value then an invalid value, I want the invalid value to stay on the screen.
    - i don't care about formatting the old value OR the startDate


    Below is a VERY rough idea of what we have but with hard-coded values- our formatting logic is in GWT's java code so I could not add it in. I hope it is somewhat clear - please let me know if there is some clarification needed.

    To run the testcase, input the string "11" in the date textfield and focus out of the date element. At this point, our flag $cwIsValid is set to true and the date element has an invalid date object as its value. Now click on the picker icon and select a date. The displayFormat function will be called with the new AND the old invalid value.

    Code:
    <HTML><HEAD><TITLE>Copy-Paste Issue</TITLE>
    
        
    </HEAD>
    <body class="pageBackground" marginwidth="0" marginheight="0" topmargin="0" leftmargin="0" scroll="no" style="overflow:hidden">
    
    
    
    
    <SCRIPT>var isomorphicDir = "isomorphic/"</SCRIPT>
        <SCRIPT>var isomorphicDir = "isomorphic/"</SCRIPT>
        <SCRIPT SRC=isomorphic/system/modules/ISC_Core.js></SCRIPT>
        <SCRIPT SRC=isomorphic/system/modules/ISC_Foundation.js></SCRIPT>
        <SCRIPT SRC=isomorphic/system/modules/ISC_Containers.js></SCRIPT>
        <SCRIPT SRC=isomorphic/system/modules/ISC_Grids.js></SCRIPT>
        <SCRIPT SRC=isomorphic/system/modules/ISC_Forms.js></SCRIPT>
        <SCRIPT SRC=isomorphic/system/modules/ISC_DataBinding.js></SCRIPT>
    	<SCRIPT SRC=isomorphic/skins/Enterprise/load_skin.js></SCRIPT>
    
    <SCRIPT>
    
    window.convertDateFormat= function(value,element){
    		isc.Log.logWarn(" convertDateFormat: being called with value : " +value);
    
    	 if(element.$cwInvalid){
    		isc.Log.logWarn(" convertDateFormat: current value is invalid, returning: " +element.cwCurrentStringFormat);
            return element.cwCurrentStringFormat;
          }
         var newDateFormat = value.getTime() == -1?"12/31/1969":"06/05/2011";
    	 
         //if formatting this date value has failed, return the current string value that caused the error in the first place
         if(value.getTime()=="-1"){
          return "-1";
         }else{
    		element.$cwInvalid = null;
    		isc.Log.logWarn(" convertDateFormat: current value is 'valid', returning: " +newDateFormat);
            return newDateFormat;
         }
         
        };
    window.convertInputToDate = function(string, element){
         var newDate = window.Date.create();
    	var isValue = false;
    	isc.Log.logWarn(" convertInputFormat:  being called with value : " +string);
    	if(element.textField!=null && element.textField.getValue() == string){
    		isValue = true;
    	}
    	
    	element.cwCurrentStringFormat = string;
    	 
    	 if(string == "11"){
    		isc.Log.logWarn(" convertInputFormat:   called with invalid value");
    		newDate.setTime(-1);
    		element.$cwInvalid = true;
    	 }else if(isValue){
    		 isc.Log.logWarn(" convertInputFormat:   erasing invalidvalue flag");
             element.$cwInvalid = null;
         }
    	 
         return newDate;
         
        };
    
    
    
    isc.setAutoDraw(false);
    isc.VStack.create({
    ID:"LoginGrid_vCentering",
    name:"LoginGrid_vCentering",
    autoDraw:true,
    height:"100%",width:"100%",
    align:"center",
    defaultLayoutAlign:"center",
    members:
    	[isc.DynamicForm.create({
    		ID:"LoginGrid",
    		name:"LoginGrid",
    		numCols:3,
    		colWidths:["144","288","144"],
    		titleOrientation:"left",
    		margin:0,
    		fields:
    			[{title:"Date Issue",
    				name:"dateissue",
    				_constructor:"DateItem",
    				useTextField:true,
    				startDate:"01\/01\/1995",
    				init: function(){
    					var dateItem = this;
    					this.dateFormatter = function(){return window.convertDateFormat(this,dateItem);};
    					this.inputFormat = function(string){return window.convertInputToDate(string,dateItem);};
    					this.Super("init");
    				}
    			},
    			{type:"text",name:"dummy"}
    			]})]});
    
    </script>
    </BODY>
    </html>

    #2
    Just in case, the version i'm using is 8.2p 05-30-2012

    Comment


      #3
      By default if you enter a string into a date item that doesn't map to a valid date, the string is stored as the data value from the item.
      In other words if you enter "11" the value for the item will be set to that string, but if you enter "11/11/2011" the value will be stored as the appropriate date.

      If you override the date formatter and parser methods as you have, you can maintain this behavior by simply having the case where you are unable to parse the user-entered string to a date return that string (and when formatting, if the value passed in is a string, just return it unmolested).

      This would seem to be the simplest way to get things working.
      Can we just clarify whether this kind of thing is going to work for you?
      It looks like the main difference in your code is that you're mapping unparseable strings to an invalid value'd Date instance (new Date().setTime(-1);). Is that a requirement for your app for some reason?

      If you're worried about downstream code (when 'saveData()' is called on the form, for example) failing to handle being passed strings, this should be handled by normal type validation on the form (the user-entered, unparseable string would be rejected if the field is of type date or datetime).


      Just for completeness - we understand and acknowledge that the value passed to the parse/format methods may not always be the user entered value. This is known to be the case and not likely to change - there are reasons for the framework to call these methods to format the 'startDate' / to parse the old value for comparison etc.
      There are other code flows that will only be executed when the user actually picks a value (such as change / changed), which you might be able to leverage to get things working, or within the transformInput flow you could probably use getEnteredValue() to get at the value in the text box and determine whether that's the value being parsed when the method runs. We can talk through this further but it may be introducing more complexity than you need.

      Comment


        #4
        returning a "-1" is actually how we determine when to show our validation error. the setTime(-1) isn't a requirement - but what do I return in convertInputDate instead?

        I've managed to get a workaround with the textfield value - I didn't know there was a getEntredvalue() - is that the same as date.textfield.getValue()?

        Comment


          #5
          Yes getEnteredValue() is a synonym for looking at the string value currently entered in the text field.

          You could presumably return the invalid string (unchanged) from your date parsing function instead of a date, and for the formatting function, if the value is not a date (IE it's the invalid string), just return it so its displayed to the user.

          Comment

          Working...
          X