• Hi, I have an issue on a form which I have basically narrowed down to happening when a [file] field is set to “required”, e.g.

    [file* documents id:documents class:required-field limit:4mb filetypes:jpg|jpeg|pdf]

    When I select a file I get this JavaScript error in the browser console after selecting the file:

    Unhandled Promise Rejection: TypeError: t.trim is not a function. (In 't.trim()', 't.trim' is undefined)

    I am then unable to submit the form as the validation still thinks that there is an invalid field.

    After a fair bit of googling etc trying to troubleshoot it seems like this relates to the underlying SWV validation in CF7. So for some reason when a file field is set to “required” it triggers this error, without “required” it works just fine.

    Unfortunately troubleshooting this issue has been a little more complicated in that I actually have a Staging and a Live version of the site, but the issue is only occurring on the Staging version. I have exactly the same theme, form template HTML, JavaScript on both sites (running on the same server, PHP version etc), but it only occurs on the Staging site.

    I’m not sure if it’s helpful / relevant or not but if I compare the SWV schemas for the form on both Staging and Live and they don’t seem to match, specifically on Staging it has this 4th ruleset related to the “documents” field:

    {
    "rule": "required",
    "field": "documents",
    "error": "The field is required."
    }

    The other rulesets for “documents” that are in the schema on both Staging and Live are these:

    {
    "rule": "requiredfile",
    "field": "documents",
    "error": "The field is required."
    },
    {
    "rule": "file",
    "field": "documents",
    "accept": [
    ".jpg",
    ".jpeg",
    ".pdf"
    ],
    "error": "You are not allowed to upload files of this type."
    },
    {
    "rule": "maxfilesize",
    "field": "documents",
    "threshold": 4194304,
    "error": "The file is too big."
    },

    Thanks for any help!

    • This topic was modified 6 months, 1 week ago by Rick Curran. Reason: Added some extra information for clarity
Viewing 15 replies - 1 through 15 (of 16 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Can we see the Live site?

    Thread Starter Rick Curran

    (@rickcurran)

    @takayukister Thanks, the site is a dev site for a client, so it is publicly viewable, but in order to not leave a permanent link here I have set up a redirect via my own website that should take you to an example form showing the error:

    https://qreate.co.uk/cf7form-issue

    I will remove this redirect at a later date.

    Thread Starter Rick Curran

    (@rickcurran)

    If you step through that form you should see an error in the console when you select a file, if you go to the last step of the form, check the tick box and try to submit it will then come up with the “One or more fields have an error” message.

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    What other plugins and theme do you use on the site?

    Thread Starter Rick Curran

    (@rickcurran)

    @takayukister There are quite a number of other plugins in use on the site, some third-party and some specifically made for this site. I have tested by disabling all of them on the Staging version but the issue persists, so it doesn’t appear to be another plugin causing the issue. I also have the exact same set of plugins active on the Live site where the issue is not occurring.

    I know that testing the issue on a default theme is a common debugging scenario, I have also tried this, however, it is a little difficult to test as the theme for the site includes functionality specific for these forms. So I am still really trying to figure out why there is a difference between the behaviour on Staging and Live versions.

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    Which plugin (or theme) do you use to implement the multi-step form mechanism?

    Thread Starter Rick Curran

    (@rickcurran)

    @takayukister The multi-step form functionality is my own code, it is basically some JavaScript code that handles the steps and does some of its own validation on the fields at each step by checking which fields are set as “required”. Generally I don’t rely on the default CF7 validation for these forms as the aim is to ensure everything is valid before the user proceeds to the next step and then to ultimately submit the form. In this case however it does seem to be the CF7 validation that is being triggered when the required file field is used. I am definitely happy to find an issue in my code which is causing or contributing to this error but so far I have not been able to. It’s the difference in behaviour between the Staging and Live versions of these sites / forms which is the key issue.

    Sorry, one thing I should have included before is a link to the Live version of this same form which doesn’t trigger the error so that you can compare. This is the most confusing aspect of this issue, why the error occurs on the Staging site but not on Live. Here is a link to the Live version (Note, please don’t submit the form on the Live version as this an active form, the key point to note is when you actually select a file with the file selector, on Staging you should see the JS error, on Live you should not):

    https://qreate.co.uk/cf7form-issue-live

    Can you comment on the differences in the SWV schemas that I highlighted in my original post, this does seem to be an important difference between the two forms. I have a few questions about the schema:

    • Does the SWV schema get updated when a form is saved?
    • And does this schema get affected by updates to the CF7 plugin itself?
    • Is the 4th ruleset I show in my original post an expected rule that should exist for forms with a “required” file field?
    • The contact form entry on the Live site has not been updated / saved recently, so if the SWV schema is affected by updates and saving I am wondering then will this SWV schema change to match what is happening on the Staging site?

    The last question above has been my main concern, at the moment the issue is only happening on Staging, but the form continues to work on the Live site. So I am concerned that updating / saving the form post on Live is going to introduce the same error.

    • This reply was modified 6 months, 1 week ago by Rick Curran. Reason: Fixed typos, grammatical changes
    Plugin Author Takayuki Miyoshi

    (@takayukister)

    So which plugin (or theme) do you use to implement the code?

    Thread Starter Rick Curran

    (@rickcurran)

    @takayukister It is my own custom theme that I use to implement the JavaScript code.

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    What if you switch to the default theme? The redundant required rule on the documents field disappears?

    Thread Starter Rick Curran

    (@rickcurran)

    Yes, if I switch to the default theme the redundant required rule does not appear in the schema.

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    OK. Now it’s clear that the original theme is causing the problem, maybe at the PHP level, not JS. If you need further support on your theme coding, please share the public repository for it.

    Thread Starter Rick Curran

    (@rickcurran)

    Thanks for looking at this further. I do agree that all the signs point to it being something within my theme, however I just can’t get to the bottom of what could be the cause.

    Today I did some further tests which have made it even more confusing, I have taken a further look at it and I purposely disabled some other JavaScript and PHP includes on the site that relate to forms to see if they were causing an issue. I then went through the form, selected a file with the file select and I got no JavaScript error, I then re-enabled all of the other JS and PHP includes and tried it again and now there is still no error(!).

    So unfortunately it really doesn’t make sense, the issue has disappeared but not in a way that I can pin down to any specific behaviour or repeatable action. I have on occasion had this issue before and it did seem to clear up by simply re-uploading the JavaScript file to the server, but on this occasion it has been a much bigger problem. Additionally I still do not encounter this issue on the Live version of the site which adds to the weirdness of this issue.

    Interestingly both SWV schemas now show identical when I compare the Staging and Live versions, so the stray additional “required” file field is no longer appearing in the Schema. Can you clarify how the SWV schema is updated? Is it modified whenever a Contact Form entry is updated in the system or does the Schema generate when the Contact Form is loaded in the page of the site? I’m just trying to gain a better understanding of processes are happening here, whilst this bug has gone for now I do think there is some underlying situation that is causing this to be a weird “heisenbug” type of issue.

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    No, it’s not a bug at all. The required rule was something you intentionally included. Maybe the PHP file you excluded relates to it. Or, it might be cache.

    Thread Starter Rick Curran

    (@rickcurran)

    I do understand that it’s not a bug as such in Contact Form 7, it’s just that there is some odd behaviour happening which was causing an extra rule to appear in the schema.

    In a working form a required file field adds a requiredfile rule in the schema, but for some reason I was getting a required rule in the schema too, which when it existed then caused the JavaScript trim error I was seeing in the console.

    I do now have a correctly functioning form as the required rule is no longer appearing in the schema. But the bit I can’t figure out is why this required rule was getting added, based on all of the testing I have done such as disabling plugins and switching themes it does appear to be triggered by some code in my theme but it’s just not clear what exactly that was. In the end I have actually changed nothing and neither removed or edited any of my code and the issue has suddenly just gone away, so it is a little concerning situation. I will just have to monitor it and hopefully the issue will not reappear.

    One final question if that’s ok, I’d asked this above but it would be useful to know for my own better understanding: Can you clarify how the SWV schema is updated? Is it modified whenever a Contact Form entry is updated or does the Schema generate when the Contact Form is loaded in the page of the site?

    Thank you again for taking the time to respond, it is much appreciated!

Viewing 15 replies - 1 through 15 (of 16 total)

The topic ‘JavaScript error after selecting a file from a required file field’ is closed to new replies.