CodeIgniter Forums

Full Version: Using a form creator and validator in side file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, Smile
My wish list for version 4.0 will be to have the possibility to create the forms in a separate folder with the validation constraints. The goal is to make the controllers lighter. It will also be possible to easily reuse a form in several places.

like this in symfony :
PHP Code:
<?php

namespace MicroCMS\Form\Type;

use 
Symfony\Component\Form\AbstractType;
use 
Symfony\Component\Form\FormBuilderInterface;
use 
Symfony\Component\Form\Extension\Core\Type\TextType;
use 
Symfony\Component\Form\Extension\Core\Type\EmailType;
use 
Symfony\Component\Form\Extension\Core\Type\TextareaType;
use 
Symfony\Component\Validator\Constraints as Assert;

class 
ContactType extends AbstractType
{

    public function 
buildForm(FormBuilderInterface $builder, array $options)
    {
        
$builder->add('email'EmailType::class, [
                    
'label'           => 'Votre eMail',
                    
'required'        => true,
                    
'attr'            => ['placeholder' => '(ne sera pas publié)'],
                    
'constraints'     => [new Assert\NotBlank(), new Assert\Email(['checkMX' => true])],
                    
'invalid_message' => "Le champ d'adresse e-mail doit être rempli correctement."])
                ->
add('lastname'TextType::class, [
                    
'label'    => 'Nom',
                    
'required' => false,
                ])
                ->
add('firstname'TextType::class, [
                    
'label'       => 'Prénom',
                    
'required'    => true,
                    
'constraints' => [new Assert\NotBlank(), new Assert\Length(['min' => 3'max' => 100])],
                ])
                ->
add('phone'TextType::class, [
                    
'label'       => 'Téléphone',
                    
'required'    => false,
                    
'constraints' => new Assert\Length(['min' => 8'max' => 20]),
                ])
                ->
add('message'TextareaType::class, [
                    
'constraints'     => [new Assert\NotBlank(), new Assert\Length(['min' => 3])],
                    
'attr'            => ['placeholder' => 'Votre message'],
                    
'label'           => 'Contenu',
                    
'required'        => true,
                    
'invalid_message' => 'Le champ ne doit pas être vide.',
        ]);
    }

    public function 
getName()
    {
        return 
'contact';
    }