Формы Netlify не работают с моим сайтом GatsbyJS

Я работаю над веб-сайтом и использую Netlify для его размещения. Я добавил формы Netlify в форму обратной связи в файле contact.js, и имя формы отлично отображается на панели инструментов Netlify Form. Хотя, когда я отправляю форму, ничего не происходит. Никакие материалы не отображаются на Netlify ни в одобренных, ни в спаме.

Вот мой исходник contact.js.

import React from 'react';
import _ from 'lodash';
import {graphql} from 'gatsby';

import {Layout} from '../components/index';
import {toStyleObj, withPrefix, htmlToReact} from '../utils';
import FormField from '../components/FormField';

// this minimal GraphQL query ensures that when 'gatsby develop' is running,
// any changes to content files are reflected in browser
export const query = graphql`
  query($url: String) {
    sitePage(path: {eq: $url}) {
      id
    }
  }
`;

export default class Page extends React.Component {
    render() {
        return (
            <Layout {...this.props}>
              <article className="post post-full">
                <header className="post-header has-gradient outer">
                  {_.get(this.props, 'pageContext.frontmatter.image', null) && (
                  <div className="bg-img" style={toStyleObj('background-image: url(\'' + withPrefix(_.get(this.props, 'pageContext.frontmatter.image', null)) + '\')')}/>
                  )}
                  <div className="inner-sm">
                    <h1 className="post-title">{_.get(this.props, 'pageContext.frontmatter.title', null)}</h1>
                    {_.get(this.props, 'pageContext.frontmatter.subtitle', null) && (
                    <div className="post-subtitle">
                      {htmlToReact(_.get(this.props, 'pageContext.frontmatter.subtitle', null))}
                    </div>
                    )}
                  </div>
                </header>
                <div className="inner-md outer">
                  <div className="post-content">
                    {htmlToReact(_.get(this.props, 'pageContext.html', null))}
                  </div>
                  <form name={_.get(this.props, 'pageContext.frontmatter.form_id', null)} id={_.get(this.props, 'pageContext.frontmatter.form_id', null)} {...(_.get(this.props, 'pageContext.frontmatter.form_action', null) ? ({action: _.get(this.props, 'pageContext.frontmatter.form_action', null)}) : null)}name="contact" method="POST" data-netlify="true" data-netlify-honeypot="bot-field">
                        <div className="screen-reader-text">
                          <label>Don't fill this out if you're human: <input name="bot-field" /></label>
                        </div>
                        <input type="hidden" name="form-name" value={_.get(this.props, 'pageContext.frontmatter.form_id', null)} />
                        {_.map(_.get(this.props, 'pageContext.frontmatter.form_fields', null), (field, field_idx) => (
                          <FormField key={field_idx} {...this.props} field={field} />
                        ))}
                        <div className="form-submit">
                          <button type="submit" className="button">{_.get(this.props, 'pageContext.frontmatter.submit_label', null)}</button>
                        </div>
                      </form>
                </div>
              </article>
            </Layout>
        );
    }
}

person Scott Montford    schedule 29.09.2020    source источник


Ответы (1)


Ваша форма сильно отличается от стандартов Netlify. В Netlify, чтобы связать вашу форму и информационную панель, вам нужно добавить некоторые метаданные и мета-атрибуты, такие как data-netlify="true", data-netlify-honeypot="bot-field" (чтобы избежать СПАМА) и атрибут name с точно таким же именем, что и ваша форма Netlify на вашей панели. Это должно выглядеть так:

<form name="contact" method="post" data-netlify="true" data-netlify-honeypot="bot-field">
  {/* You still need to add the hidden input with the form name to your JSX form */}
  <input type="hidden" name="form-name" value="contact" />
  ...
</form>

Взгляните на Обработка форм с помощью генераторов статических сайтов и Статьи о том, как использовать формы Netlify с Gatsby (dev.to).

person Ferran Buireu    schedule 29.09.2020
comment
У меня есть эти атрибуты в коде. Они заключены в очень длинный тег ‹формы name› - person Scott Montford; 30.09.2020
comment
Что показывает ваш запрос во вкладке сети (в инспекторе)? - person Ferran Buireu; 30.09.2020
comment
Netlify это обнаруживает. Каждый раз, когда я нажимаю "Отправить", я попадаю на экран Netlify 404 с тем же URL-адресом, что и в контактной форме. - person Scott Montford; 30.09.2020
comment
Добавить атрибут действия - person Ferran Buireu; 01.10.2020