Home » PHP » Pimcore » How can i create contact form and save the data in pimcore 5?

How can i create contact form and save the data in pimcore 5?

Pimcore

The steps are provided below to create the contact form and save the data in the database-

/var/www/html/pimcore/src/AppBundle/Controller/DefaultController.php

<?php
declare(strict_types=1);
/**
 * Pimcore
 *
 * This source file is available under two different licenses:
 * - GNU General Public License version 3 (GPLv3)
 * - Pimcore Enterprise License (PEL)
 * Full copyright and license information is available in
 * LICENSE.md which is distributed with this source code.
 *
 * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
 * @license    http://www.pimcore.org/license     GPLv3 and PEL
 */

namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\Request;

class DefaultController extends FrontendController {

    /**
     * Method to render content pages having default template
     * @param Request $request
     * @access public
     */
    public function defaultAction(Request $request) {
        /** @var Form $form */
        $form = $this->createFormBuilder()
            ->add('firstname', \Symfony\Component\Form\Extension\Core\Type\TextType::class, [
                'label'       => 'Firstname',
                'required'    => true
            ])
            ->add('lastname', \Symfony\Component\Form\Extension\Core\Type\TextType::class, [
                'label'    => 'Lastname',
                'required' => false
            ])
            ->add('email', \Symfony\Component\Form\Extension\Core\Type\EmailType::class, [
                'label'    => 'E-Mail',
                'required' => false,
                'attr'     => [
                    'placeholder' => 'example@example.com'
                ]
            ])
            ->add('checkbox', \Symfony\Component\Form\Extension\Core\Type\CheckboxType::class, [
                'label' => 'Check me out',
                 'required' => false
            ])
            ->add('submit', \Symfony\Component\Form\Extension\Core\Type\SubmitType::class, [
                'label' => 'Submit'
            ])
            ->getForm();
        
        $form->handleRequest($request);
        $formData = ($form->isSubmitted() && $form->isValid()) ? $form->getData() : []; 
        
        if ( count($formData) > 0 ) {       
        $task = $form->getData();        
        $person = \Pimcore\Model\DataObject\Form::getByFirstname($task['firstname']);   
                if (count($person) == 0) { 
                     $obj = new \Pimcore\Model\DataObject\Form(); 
                     $obj->setKey(\Pimcore\File::getValidFilename($task['firstname']));
                     $obj->setFirstname($task['firstname']);
                     $obj->setLastname($task['lastname']);
                     $obj->setEmail($task['email']);
                     $obj->setCheckbox($task['checkbox']);
                     $obj->setParentId(1);
                     $obj->setPublished(1);
                     $obj->save();
                     $this->view->success='Data Added Successfully!';   
                    
                }else{
                     $this->view->success='Data Already Exist!';
                }        
        $this->view->getParameters()->add($task);
    }
        $this->view->form    = $form->createView();
    }

}

/var/www/html/pimcore/app/Resources/views/Default/default.html.twig

<style>
label {
  display: inline-block;
  width: 140px;
  text-align: right;
  padding-right: 10px;
  padding-bottom: 10px;
}​
</style>
<div style="display: inline-block;padding-left: 91px;padding-bottom: 20px;padding-top: 20px;">
{% if success is defined %}
    {{ success }} for {% if firstname is defined %}{{ form.firstname.vars['label'] }}( {{ firstname }} ){% endif %}
{% endif %}
</div>

{{ form(form) }}

Check Also

Pimcore

Top Pimcore 5 Interview Questions And Answers Part 1

1. How can we show the localizedfield data in Pimcore 5? The variables of the …

Leave a Reply

Your email address will not be published. Required fields are marked *