CRUDlex\ControllerProvider::modifyEntity PHP Method

modifyEntity() protected method

Validates and saves the new or updated entity and returns the appropriate HTTP response.
protected modifyEntity ( Silex\Application $app, AbstractData $crudData, Entity $instance, string $entity, boolean $edit ) : Response
$app Silex\Application the current application
$crudData AbstractData the data instance of the entity
$instance Entity the entity
$entity string the name of the entity
$edit boolean whether to edit (true) or to create (false) the entity
return Symfony\Component\HttpFoundation\Response the HTTP response of this modification
    protected function modifyEntity(Application $app, AbstractData $crudData, Entity $instance, $entity, $edit)
    {
        $fieldErrors = [];
        $mode = $edit ? 'edit' : 'create';
        $request = $app['request_stack']->getCurrentRequest();
        if ($request->getMethod() == 'POST') {
            $instance->populateViaRequest($request);
            $validator = new EntityValidator($instance);
            $validation = $validator->validate($crudData, intval($request->get('version')));
            $fieldErrors = $validation['errors'];
            if (!$validation['valid']) {
                $optimisticLocking = isset($fieldErrors['version']);
                $this->setValidationFailedFlashes($app, $optimisticLocking, $mode);
            } else {
                $modified = $edit ? $crudData->update($instance) : $crudData->create($instance);
                $response = $modified ? $this->modifyFilesAndSetFlashBag($app, $crudData, $instance, $entity, $mode) : false;
                if ($response) {
                    return $response;
                }
                $app['session']->getFlashBag()->add('danger', $app['translator']->trans('crudlex.' . $mode . '.failed'));
            }
        }
        return $app['twig']->render($app['crud']->getTemplate($app, 'template', 'form', $entity), ['crudEntity' => $entity, 'crudData' => $crudData, 'entity' => $instance, 'mode' => $mode, 'fieldErrors' => $fieldErrors, 'layout' => $app['crud']->getTemplate($app, 'layout', $mode, $entity)]);
    }