Answer the question
In order to leave comments, you need to log in
How to group form validation in Symfony 4.4.8?
There was a question: I have such a form
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\Url;
/**
* Class CommonFieldType
* Implements adjacent fields for the short link form and the file upload form
* @package App\Form
*/
class CommonFieldType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('url',TextType::class,['constraints' => new Url()])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
// Configure your form options here
]);
}
}
Symfony\Component\Validator\Constraints\Url
['constraints' => new Url()]
['constraints' => new Length(['max' => 255])]
If you are using validation groups, you need to either reference the Default group when creating the form, or set the correct group on the constraint you are adding: new NotBlank(['groups' => ['create', 'update'] ]);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question