Como Implementar Validações Avançadas em Formulários com Contact Form 7 e Bootstrap 5.3
O Contact Form 7 (CF7) é um dos plugins mais populares para criar formulários no WordPress. Ele é extremamente flexível e permite que você adicione validações personalizadas usando filtros e hooks do WordPress. Neste post, vamos mostrar como integrar validações avançadas ao CF7, utilizando o Bootstrap 5.3 para estilização do formulário.
O Que Vamos Criar?
Um formulário com os seguintes campos:
CPF: Validação personalizada para garantir que o CPF seja válido.
E-mail: Verificação se o e-mail segue o formato correto e se o domínio é válido.
CEP: Integração com a API ViaCEP para buscar automaticamente o endereço.
O formulário será estilizado com o Bootstrap 5.3 e incluirá mensagens de erro dinâmicas.
1. Criando o Formulário no Contact Form 7
No painel do WordPress, crie ou edite um formulário no CF7 com o seguinte código:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Adicionamos classes do Bootstrap para estilização.
O campo endereco, cidade e estado são marcados como readonly para serem preenchidos automaticamente com a API ViaCEP.
IDs foram atribuídos para facilitar a manipulação via JavaScript.
2. Adicionando Validações Personalizadas
Validação de CPF
Adicione o seguinte código ao arquivo functions.php do seu tema:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Adicione este código para buscar os dados do CEP automaticamente:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Crie o arquivo viacep.js na pasta assets/js do tema com o seguinte código:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Adicione os estilos do Bootstrap ao seu tema. Caso ainda não esteja usando o Bootstrap, inclua o seguinte no arquivo functions.php:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Crie uma nova página no WordPress e insira o shortcode do CF7.
Preencha os campos com dados inválidos para verificar se as validações estão funcionando corretamente.
Teste o CEP para garantir que os campos de endereço sejam preenchidos automaticamente.
Conclusão
Com este guia, você aprendeu a integrar validações avançadas no Contact Form 7, utilizando o poder dos filtros do WordPress, JavaScript e a estilização do Bootstrap 5.3. Agora, seus formulários estão mais interativos, seguros e profissionais.
Se você gostou deste tutorial ou tem dúvidas, deixe um comentário abaixo!
Hi, my name is Cesar H. Ribeiro. I’m Brazilian and currently reside in Hortolândia, São Paulo. I’ve been working in Web Development since 2006, specializing in creating custom websites using the WordPress platform. My expertise includes transforming PSD/layouts into custom themes. Over the years, I’ve collaborated with numerous agencies worldwide, including those in Brazil, Australia, the USA, Germany, and Austria.
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional
Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.