Symfony\Component\Form\Form::setData PHP Метод

setData() публичный Метод

Updates the field with default data.
public setData ( array $appData ) : Form
$appData array The data formatted as expected for the underlying object
Результат Form The current form
    public function setData($appData)
    {
        $event = new DataEvent($this, $appData);
        $this->dispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);

        // Hook to change content of the data
        $event = new FilterDataEvent($this, $appData);
        $this->dispatcher->dispatch(FormEvents::SET_DATA, $event);
        $appData = $event->getData();

        // Treat data as strings unless a value transformer exists
        if (!$this->clientTransformers && !$this->normTransformers && is_scalar($appData)) {
            $appData = (string) $appData;
        }

        // Synchronize representations - must not change the content!
        $normData = $this->appToNorm($appData);
        $clientData = $this->normToClient($normData);

        $this->appData = $appData;
        $this->normData = $normData;
        $this->clientData = $clientData;
        $this->synchronized = true;

        if ($this->dataMapper) {
            // Update child forms from the data
            $this->dataMapper->mapDataToForms($clientData, $this->children);
        }

        $event = new DataEvent($this, $appData);
        $this->dispatcher->dispatch(FormEvents::POST_SET_DATA, $event);

        return $this;
    }

Usage Example

 /**
  * @param EntityManager $user
  *
  * @return bool
  */
 public function process(ProjetModel $projet)
 {
     $this->form->setData($projet);
     if ('POST' == $this->request->getMethod()) {
         $this->form->bind($this->request);
         if ($this->form->isValid()) {
             $projet->setUser($this->connectedUser);
             ############ Edit settings ##############
             $DestinationDirectory = __DIR__ . '/../../../../../web/uploads/';
             //specify upload directory ends with / (slash)
             $Quality = 90;
             //jpeg quality
             ##########################################
             //Let's check allowed $ImageType, we use PHP SWITCH statement here
             $ext = $projet->file->getMimeType();
             switch (strtolower($projet->file->getMimeType())) {
                 case 'image/png':
                     //Create a new image from file
                     $CreatedImage = imagecreatefrompng($projet->file->getRealPath());
                     break;
                 case 'image/gif':
                     $CreatedImage = imagecreatefromgif($projet->file->getRealPath());
                     break;
                 case 'image/jpeg':
                 case 'image/pjpeg':
                     $CreatedImage = imagecreatefromjpeg($projet->file->getRealPath());
                     break;
                 default:
                     die('Unsupported File!');
                     //output error and exit
             }
             // Crop and save image to upload directory
             $cropService = $this->cropper;
             $destImage = uniqid() . '.' . $projet->file->guessExtension();
             if (!$cropService->cropImage($projet->getX(), $projet->getY(), $projet->getW(), $projet->getH(), $DestinationDirectory . $destImage, $CreatedImage, $Quality, $projet->file->getMimeType())) {
                 die('Erreur lors du rognage de l\'image');
             }
             $projet->setPhoto($destImage);
             // Create entity project
             $entity = new Projet();
             $entity->setNomprojet($projet->getNomprojet());
             $entity->setDescription($projet->getDescription());
             $entity->setDuree($projet->getDuree());
             $entity->setDaterealisation($projet->getDaterealisation());
             $entity->setGains($projet->getGains());
             $entity->setFinancement($projet->getFinancement());
             $entity->setPhoto($projet->getPhoto());
             $entity->setCout($projet->getCout());
             $entity->setUser($projet->getUser());
             $this->entityManager->persist($entity);
             $this->entityManager->flush();
             return true;
         }
     }
     return false;
 }
All Usage Examples Of Symfony\Component\Form\Form::setData