【问题标题】:Netlify form submission blank with react-final-form plugin带有 react-final-form 插件的 Netlify 表单提交空白
【发布时间】:2020-10-14 20:37:50
【问题描述】:

我正在使用 react-final-form 开发一个多页向导表单,但现在我坚持将我的提交内容发送到 Netlify。

Netlify 已检测到我的表单,但在提交时未收到我的表单数据

这是我第一次使用 Netlify 处理表单提交,所以我不确定我是否在做某事 Netlifys 方面或 react-final-forms 方面的错误。

重要 我正在使用 GATSBY

index.js

<Wizard onSubmit={onSubmit}>
<Wizard.Page>
    <div>
        <div>
            <label css={label}>> Name</label>
        </div>
        <Field
            autocomplete="off"
            css={input}
            name="Name"
            component="input"
            type="text"
            placeholder="$"
            validate={required}
        />
        <Error name="Name" />
    </div>
    <div>
        <div>
            <label css={label}>> Email</label>
        </div>
        <Field
            autocomplete="off"
            css={input}
            name="Email"
            component="input"
            type="email"
            placeholder="$"
            validate={required}
        />
        <Error name="Email" />
    </div>

    <div>
        <div>
            <label css={label}>> Social handle</label>
        </div>
        <Field
            autocomplete="off"
            css={input}
            name="Social"
            component="input"
            type="text"
            placeholder="$"
        />
        <Error name="Social" />
    </div>
</Wizard.Page>

<Wizard.Page>
    <div>
        <div>
            <label css={label}>> What can we do for you?</label>
        </div>
        <div>
            <label
                style={{
                    display: 'flex',
                    alignItems: 'center',
                    paddingTop: '0.5em',
                }}
            >
                <Field
                    css={checkbox}
                    onClick={play}
                    name="services"
                    component="input"
                    type="checkbox"
                    value="Shopify build"
                />{' '}
                <span css={checkboxlabel}>Shopify build</span>
            </label>
        </div>
        <div>
            <label style={{ display: 'flex', alignItems: 'center' }}>
                <Field
                    css={checkbox}
                    onClick={play}
                    name="services"
                    component="input"
                    type="checkbox"
                    value="pop-up store"
                />{' '}
                <span css={checkboxlabel}>Pop-up store</span>
            </label>
        </div>
        <div>
            <label style={{ display: 'flex', alignItems: 'center' }}>
                <Field
                    css={checkbox}
                    onClick={play}
                    name="services"
                    component="input"
                    type="checkbox"
                    value="WebVR"
                />{' '}
                <span css={checkboxlabel}>WebVR</span>
            </label>
        </div>
        <div>
            <label style={{ display: 'flex', alignItems: 'center' }}>
                <Field
                    css={checkbox}
                    onClick={play}
                    name="services"
                    component="input"
                    type="checkbox"
                    value="Website audit"
                />{' '}
                <span css={checkboxlabel}>Website audit</span>
            </label>
        </div>
        <div>
            <label style={{ display: 'flex', alignItems: 'center' }}>
                <Field
                    css={checkbox}
                    onClick={play}
                    name="services"
                    component="input"
                    type="checkbox"
                    value="Asset creation"
                />{' '}
                <span css={checkboxlabel}>Asset creation</span>
            </label>
        </div>
        <div>
            <label style={{ display: 'flex', alignItems: 'center' }}>
                <Field
                    css={checkbox}
                    onClick={play}
                    name="services"
                    component="input"
                    type="checkbox"
                    value="Other"
                />{' '}
                <span css={checkboxlabel}>Other</span>
            </label>
        </div>
    </div>

    <div>
        <div style={{ paddingTop: '1em' }}>
            <label css={label}>> Budget</label>
        </div>
        <Field css={budget} name="Budget" component="select">
            <option />
            <option value="UNDER > $3000">UNDER > $3000</option>
            <option value="$3000 - $10000">$3000 - $10000</option>
            <option value="$10000 - $15000">$10000 - $15000</option>
            <option value="$15000 - $25000">$15000 - $25000</option>
            <option value="$25000+">$25000+</option>
        </Field>
        <Error name="budget" />
    </div>
</Wizard.Page>
<Wizard.Page>
    <div>
        <div>
            <label css={label}>> Message</label>
        </div>
        <Field
            css={message}
            name="message"
            component="textarea"
            placeholder="$"
        />
        <Error name="message" />
    </div>
</Wizard.Page>

Wizard.js

    handleSubmit = values => {
    const { children, onSubmit } = this.props
    const { page } = this.state
    const isLastPage = page === React.Children.count(children) - 1
    if (isLastPage) {
        return onSubmit(values)
    } else {
        this.next(values)
    }
}



render() {
    const { children } = this.props
    const { page, values } = this.state
    const activePage = React.Children.toArray(children)[page]
    const isLastPage = page === React.Children.count(children) - 1
    
    return (
        <Form
            autocomplete="off"
            initialValues={values}
            validate={this.validate}
            onSubmit={this.handleSubmit}  
        >
            {({ handleSubmit, submitting, values }) => (
                <form name="notypo-services" method="POST" data-netlify="true" onSubmit={handleSubmit}>
                    {activePage}
                    <div className="buttons">
                        {page > 0 && (
                            <div onClick={this.previous}>
                                <PrevButton></PrevButton>
                            </div>
                        )}
                        {!isLastPage && (
                            <NextButton></NextButton>   
                        )}
                        {isLastPage && (
                            <div type="submit">
                                <SendButton></SendButton>
                            </div>
                        )}
                    </div>
                </form>
            )}
        </Form>
    )
}

【问题讨论】:

    标签: reactjs gatsby netlify react-final-form


    【解决方案1】:

    您的&lt;form&gt; 标签应该有一个action,并且提供的数据应该有一个key-value 对:

    <form name="notypo-services" 
          method="POST" 
          data-netlify="true" 
          action="/"
          onSubmit={handleSubmit}>
    

    您的数据结构应如下所示:

    form-name: "notypo-services"
    Name: "Your typed name"
    Email: "Your typed email"
    

    注意form-name 值。这是强制性的。

    如果您没有thank-you 页面,您可以通过/ 将操作重定向到自己。

    另外,我建议添加data-netlify-honeypot 以避免垃圾邮件。

    您可以查看Netlify documentation了解更多详情。

    【讨论】:

    • 嘿!添加了 action="/" 但它没有将我发送到感谢页面,我的数据结构如下: 预算:“低于 > 3000 美元” 电子邮件:“mitch@gmail.com” 姓名:“mitch” 社交:“ mitch313123”消息:“这是针对堆栈溢出的测试提交”服务:[“资产创建”] 基于我的 console.log(values)
    • 在 Netlifys 端仍然没有收到数据
    • 你说的没有form-name字段
    • 好的,有点问题。 未在值中检测到。所以我尝试了 - 但根据 react-final-form 文档,您无法将值添加到输入字段(仅限复选框和收音机)final-form.org/docs/react-final-form/types/FieldProps#value 因此,我认为 Netlify 和 react-final-form 可能不兼容,至少与我设置的方式不兼容。
    • 您始终可以在发送数据对象之前对其进行操作以添加您的表单名称值。
    【解决方案2】:

    经过几天的拖延,我终于把这个放在一起

    在提交时,我将我的数据传输到一个隐藏的表单中,然后发送到 Netlify(成功)

    handleSubmit = values => {
        const { children, onSubmit } = this.props
        const { page } = this.state
        const isLastPage = page === React.Children.count(children) - 1
        if (isLastPage) {
            document.getElementById("realFormName").value = "notypo-services";
            document.getElementById("realName").value = values.Name ;
            document.getElementById("realEmail").value =  values.Email;
            document.getElementById("realSocial").value =  values.Social;
            document.getElementById("realServices").value =  values.Services;
            document.getElementById("realBudget").value =  values.Budget;
            document.getElementById("realMessage").value =  values.Message;
            
            
            document.getElementById("myForm").submit() ;
    
            return false ;
        } else {
            this.next(values)
        }
    }
    
    <form id="myForm" name="notypo-services" method="POST" data-netlify="true" data-netlify-honeypot="bot-field" action="/" style={{display: 'none'}}>
         <input hidden="true" name="form-name" type="text" id="realFormName" />
         <input hidden="true" name="Name" type="text" id="realName" />
         <input hidden="true" name="Email" type="email" id="realEmail" />
         <input hidden="true" name="Social" type="text" id="realSocial" />
         <input hidden="true" name="Services" type="text" id="realServices" />
         <input hidden="true" name="Budget" type="text" id="realBudget" />
         <input hidden="true" name="Message" type="text" id="realMessage" />
                
         <input hidden="true" type="submit" value="Submit" />
    </form>
    

    【讨论】:

    • 这是您公用文件夹中的 index.html 文件吗?
    猜你喜欢
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    相关资源
    最近更新 更多