PHP code example of dobryprogramator / smartform-bundle
1. Go to this page and download the library: Download dobryprogramator/smartform-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dobryprogramator / smartform-bundle example snippets
namespace App\Entity;
use DobryProgramator\SmartformBundle\Entity\AbstractSmartformAddress;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\AddressRepository")
*/
class Address extends AbstractSmartformAddress
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
public function getId(): ?int
{
return $this->id;
}
}
declare(strict_types=1);
namespace App\Form\Model;
use DobryProgramator\SmartformBundle\Form\Model\SmartformAddressModel;
use Symfony\Component\Validator\Constraints as Assert;
final class AddressModel
{
/**
* @Assert\Valid
*
* @var SmartformAddressModel
*/
public $address;
}
declare(strict_types=1);
namespace App\Form\Type;
use App\Form\Model\AddressModel;
use DobryProgramator\SmartformBundle\Form\Type\SmartformWholeAddressType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
final class AddressType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add(
'address',
SmartformWholeAddressType::class,
[
'label' => 'Address'
]
)
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(
[
'data_class' => AddressModel::class
]
);
}
public function getBlockPrefix(): string
{
return 'app_address_';
}
}
declare(strict_types=1);
namespace App\Form\DataMapper;
use App\Entity\Address;
use App\Form\Model\AddressModel;
use DobryProgramator\SmartformBundle\Exception\SmartformFieldNotFilledException;
use DobryProgramator\SmartformBundle\Form\DataMapper\SmartformAddressMapper;
final class AddressDataMapper
{
private SmartformAddressMapper $smartformAddressMapper;
public function __construct(SmartformAddressMapper $smartformAddressMapper)
{
$this->smartformAddressMapper = $smartformAddressMapper;
}
/**
* @throws SmartformFieldNotFilledException
*/
public function mapEntityFromModel(Address $entity, AddressModel $model): void
{
$this->smartformAddressMapper->mapEntityFromModel($entity, $model->address);
}
public function mapModelFromEntity(Address $entity, AddressModel $model): void
{
$this->smartformAddressMapper->mapModelFromEntity($entity, $model->address);
}
}
...
$addressModel = new AddressModel();
$addressForm = $this->createForm(AddressType::class, $addressModel);
$addressForm->handleRequest($request);
if($addressForm->isSubmitted() && $addressForm->isValid()) {
$address = new Address();
$this->addressDataMapper->mapEntityFromModel($address, $addressModel);
// Do whatever you need with the entity, for example persist to the databas
}
...