src/CRUDlex/Controller.php
protected function modifyEntity(Request $request, AbstractData $crudData, Entity $instance, $entity, $edit)
    {
        $fieldErrors = [];
        $mode        = $edit ? 'edit' : 'create';
        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($optimisticLocking, $mode);
            } else {
                $modified = $edit ? $crudData->update($instance) : $crudData->create($instance);
                $response = $modified ? $this->modifyFilesAndSetFlashBag($request, $crudData, $instance, $entity, $mode) : false;
                if ($response) {
                    return $response;
                }
                $this->session->getFlashBag()->add('danger', $this->translator->trans('crudlex.'.$mode.'.failed'));
            }
        }

        return new Response($this->twig->render($this->service->getTemplate('template', 'form', $entity), [
            'crud' => $this->service,
            'crudEntity' => $entity,
            'crudData' => $crudData,
            'entity' => $instance,
            'mode' => $mode,
            'fieldErrors' => $fieldErrors,
            'layout' => $this->service->getTemplate('layout', $mode, $entity)
        ]));
    }
protected function modifyEntity(Request $request, AbstractData $crudData, Entity $instance, $entity, $edit)
    {
        $fieldErrors = [];
        $mode        = $edit ? 'edit' : 'create';
        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($optimisticLocking, $mode);
            } else {
                $modified = $edit ? $crudData->update($instance) : $crudData->create($instance);
                $response = $modified ? $this->modifyFilesAndSetFlashBag($request, $crudData, $instance, $entity, $mode) : false;
                if ($response) {
                    return $response;
                }
                $this->session->getFlashBag()->add('danger', $this->translator->trans('crudlex.'.$mode.'.failed'));
            }
        }

        return new Response($this->twig->render($this->service->getTemplate('template', 'form', $entity), [
            'crud' => $this->service,
            'crudEntity' => $entity,
            'crudData' => $crudData,
            'entity' => $instance,
            'mode' => $mode,
            'fieldErrors' => $fieldErrors,
            'layout' => $this->service->getTemplate('layout', $mode, $entity)
        ]));
    }
protected function modifyEntity ( Request $ request , AbstractData $ crudData , Entity $ instance , $ entity , $ edit ) { $ fieldErrors = [ ] ; $ mode = $ edit ? 'edit' : 'create' ; 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 ( $ optimisticLocking , $ mode ) ; } else { $ modified = $ edit ? $ crudData -> update ( $ instance ) : $ crudData -> create ( $ instance ) ; $ response = $ modified ? $ this -> modifyFilesAndSetFlashBag ( $ request , $ crudData , $ instance , $ entity , $ mode ) : false ; if ( $ response ) { return $ response ; } $ this -> session -> getFlashBag ( ) -> add ( 'danger' , $ this -> translator -> trans ( 'crudlex.' . $ mode . '.failed' ) ) ; } } return new Response ( $ this -> twig -> render ( $ this -> service -> getTemplate ( 'template' , 'form' , $ entity ) , [ 'crud' => $ this -> service , 'crudEntity' => $ entity , 'crudData' => $ crudData , 'entity' => $ instance , 'mode' => $ mode , 'fieldErrors' => $ fieldErrors , 'layout' => $ this -> service -> getTemplate ( 'layout' , $ mode , $ entity ) ] ) ) ; }
Validates and saves the new or updated entity and returns the appropriate HTTP
response.

@param Request $request
the current request
@param AbstractData $crudData
the data instance of the entity
@param Entity $instance
the entity
@param string $entity
the name of the entity
@param boolean $edit
whether to edit (true) or to create (false) the entity

@return Response
the HTTP response of this modification
Validates and saves the new or updated entity and returns the appropriate HTTP response .