horcle_buzz
horcle_buzz

Reputation: 2171

Submitting date from form via ExtJS.request.Ajax POST to Flask API

Why are dates such a royal PITA:

I have an ExtJS (ver. 6.2.0) form panel with a date picker:

{
    allowBlank:false,
    fieldLabel: 'Consent Date',
    name: 'consent_date',
    emptyText: 'consent_date',
    //inputType: 'date',
    xtype: 'datefield',
    //maxValue: new Date(),
    format: 'm/d/Y',
    submitFormat: 'm/d/Y'
}

And I am submitting this in my controller via an Ext.request.Ajax call:

onNewPatientFormSubmit: function () {
    var formPanel = this.lookupReference('newPatientForm'),
        form = formPanel.getForm(),
        url = 'http://127.0.0.1:5000/mrnconsentview/api/create';

        if (form.isValid()) {
            values = form.getFieldValues(true);

            console.log('form');
            console.log(form);
            console.log(values);

            form.reset();
            Ext.MessageBox.alert(
                'Thank you!',
                'Your inquiry has been sent. We will respond as soon as possible.'
            );

            Ext.Ajax.request({
                method: 'POST',
                cors: true,
                timeout: 6000000, //default is 30 seconds
                useDefaultXhrHeader: false,
                url: url,
                params: values,
                headers: {
                    'Accept': 'application/json'
                },
                disableCaching: false,

                success: function (response) {
                    json = Ext.decode(response.responseText);
                    console.log('json');
                    console.log(json);
                    console.log(response);

                    if (response.status === 200) {
                        console.log(json.items);

                    }
                    else {
                        Ext.MessageBox.alert('Error', response.message);
                    }
                }
            });
        }
}

The problem is, regardless of the submitFormat in my form panel, it ALWAYS renders submitted consent_date formatted like: Fri Jan 02 1995 00:00:00 GMT-0600 (CST) and thus, the JSON in my Ajax form submission looks like: {mrn: "testing12345", consent_date: Mon Jan 02 1995 00:00:00 GMT-0600 (CST)}

It's even worse on the server (Flask, with wtforms), where it looks like: 1995-01-02T00:00:00 and thus, chokes since it does not see it as a datetime object... The response from the server is

{
  "error_details": {
    "consent_date": [
      "Not a valid datetime value"
    ]
  }, 
  "message": "Validation error"
}

I could do post processing on it, but I would prefer to get it without having to do so. (I tried minimal post processing using the python datetime method, but gave up... ala: t = datetime.datetime.strptime(dict(request.form)['consent_date'][0], "%Y-%m-%dT%H:%M:%S.%f") and got an error: time data '1995-01-02T00:00:00' does not match format '%Y-%m-%d%H:%M:%S.%f')

Upvotes: 0

Views: 823

Answers (1)

scebotari66
scebotari66

Reputation: 3480

You should use the getValues form method instead. getFieldValues doesn't take into the account the submitFormat.

getFieldValues: ..This is similar to getValues except that this method collects type-specific data values (e.g. Date objects for date fields) while getValues returns only String values for submission.

getValues: ..This is similar to getFieldValues except that this method collects only String values for submission, while getFieldValues collects type-specific data values (e.g. Date objects for date fields.)

Upvotes: 2

Related Questions