You could remove the constraint from the class and set it in the form builders, which would allow you to have different constraints for the same field:
class CreateHumanType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'surname',
TextType::class,
[
'label' => "surname",
'constraints' => [
new NotBlank(
[
'message' => "The surname is required",
]
),
],
]
);
}
//[...]
}
class SearchHumanType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'surname',
TextType::class,
[
'label' => "surname",
'required' => "false",
]
);
}
//[...]
}
Alternatively, you could keep the constraint in the class and set the field as not mapped for the search:
class SearchHumanType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'surname',
TextType::class,
[
'label' => "surname",
'mapped' => false,
'required' => false,
]
);
}
//[...]
}
You'd then have to handle it manually in the controller.