Validate ProjectBuildRequest

This commit is contained in:
Jan Böhmer 2023-01-22 14:13:56 +01:00
parent 83d734747a
commit 31a20d0692
7 changed files with 262 additions and 14 deletions

View file

@ -28,6 +28,7 @@ use Symfony\Component\Form\Event\PreSetDataEvent;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ProjectBuildType extends AbstractType implements DataMapperInterface
@ -58,6 +59,7 @@ class ProjectBuildType extends AbstractType implements DataMapperInterface
$form->add('lot_' . $lot->getID(), SIUnitType::class, [
'label' => false,
'measurement_unit' => $bomEntry->getPart()->getPartUnit(),
'max' => min($build_request->getNeededAmountForBOMEntry($bomEntry), $lot->getAmount()),
]);
}
}
@ -65,16 +67,41 @@ class ProjectBuildType extends AbstractType implements DataMapperInterface
});
}
public function mapDataToForms($viewData, \Traversable $forms)
public function mapDataToForms($data, \Traversable $forms)
{
if (!$data instanceof ProjectBuildRequest) {
throw new \RuntimeException('Data must be an instance of ' . ProjectBuildRequest::class);
}
/** @var FormInterface[] $forms */
$forms = iterator_to_array($forms);
foreach ($forms as $key => $form) {
//Extract the lot id from the form name
$matches = [];
if (preg_match('/^lot_(\d+)$/', $key, $matches)) {
$lot_id = (int) $matches[1];
$form->setData($data->getLotWithdrawAmount($lot_id));
}
}
}
public function mapFormsToData(\Traversable $forms, &$data)
{
if (!$data instanceof ProjectBuildRequest) {
throw new \RuntimeException('Data must be an instance of ' . ProjectBuildRequest::class);
}
/** @var FormInterface[] $forms */
$forms = iterator_to_array($forms);
dump($viewData);
dump ($forms);
}
public function mapFormsToData(\Traversable $forms, &$viewData)
{
// TODO: Implement mapFormsToData() method.
foreach ($forms as $key => $form) {
//Extract the lot id from the form name
$matches = [];
if (preg_match('/^lot_(\d+)$/', $key, $matches)) {
$lot_id = (int) $matches[1];
$data->setLotWithdrawAmount($lot_id, $form->getData());
}
}
}
}