I’m trying to figure out the best way to validate the input on a fieldset. The validation rules should work so if AcceptDeclineAssignment is set to Accept then person, CompanyName, ContactName, ContactEmail, ContactPhone and Reference should be required.
Should this be a custom validator, should it go in the getInputFilterSpecification via a callback but I don’t see here how I can get the input filter to make the fields required.
I am not sure if this is relevant but this is a collection so the validation would need to work on each item in the collection which is why I think it should be in the fieldset not in say the form.
Here is the fieldset code
class SurveyorAcknowledgeFieldset extends Fieldset implements InputFilterProviderInterface {
/**
* @var EntityManager
*/
private $entityManager;
public function __construct(EntityManager $entityManager)
{
parent::__construct();
$this->entityManager = $entityManager;
$this->setHydrator(new DoctrineObject($this->entityManager));
$this->setObject(new ClaimSurveyor());
}
public function init()
{
$this->add([
'name' => 'id',
'type' => Hidden::class,
]);
$this->add([
'name' => 'idCompanySurveyor',
'type' => Hidden::class,
]);
$this->add([
'name' => 'CompanyName',
'type' => Text::class,
'options' => [
'label' => 'Surveyor'
],
'attributes' => [
'placeholder' => 'Surveyor',
'readonly' => true
]
]);
$this->add([
'name' => 'person',
'type' => Select::class,
'options' => [
'label' => 'Contact',
'empty_option' => 'Please select a contact',
'disable_inarray_validator' => true
]
]);
$this->add([
'name' => 'ContactName',
'type' => Text::class,
'options' => [
'label' => 'Contact Name',
],
'attributes' => [
'readonly' => true
]
]);
$this->add([
'name' => 'ContactEmail',
'type' => Text::class,
'options' => [
'label' => 'Contact Email',
],
'attributes' => [
'readonly' => true
]
]);
$this->add([
'name' => 'ContactPhone',
'type' => Text::class,
'options' => [
'label' => 'Contact Phone',
],
'attributes' => [
'readonly' => true
]
]);
$this->add([
'name' => 'Reference',
'type' => Text::class,
'options' => [
'label' => 'Reference',
]
]);
$this->add([
'name' => 'AcceptDeclineAssignment',
'type' => Radio::class,
'options' => [
'label' => 'Accept/Decline Assignment',
'value_options' => [
AcceptDeclineAssignment::ACCEPT => 'I hear by accept this assignment blah de blah blah blah blah blah de blah blah blah blah blah de blah blah blah blah blah de blah blah blah blah',
AcceptDeclineAssignment::DECLINE => 'I shall not be accepting this assignment blah de blah blah blah blah blah de blah blah blah blah blah de blah blah blah blah blah de blah blah blah blah blah de blah blah blah blah blah de blah blah blah blah'
]
],
'attributes' => [
'placeholder' => 'Accept/Decline Assignment',
]
]);
$this->add([
'name' => 'DeclineAssignmentReason',
'type' => Textarea::class,
'options' => [
'label' => 'Decline Assignment Reason',
],
'attributes' => [
'placeholder' => 'Decline Assignment Reason',
],
]);
parent::init();
}