add validation of existing file

This commit is contained in:
Julien Fastré 2018-06-07 16:54:13 +02:00
parent 299491cb5b
commit fcc74c993d
11 changed files with 75 additions and 8 deletions

View File

@ -13,6 +13,7 @@ use Chill\PersonBundle\Entity\Person;
use Symfony\Component\Security\Core\Role\Role; use Symfony\Component\Security\Core\Role\Role;
use Chill\PersonBundle\Security\Authorization\PersonVoter; use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Chill\DocStoreBundle\Security\Authorization\PersonDocumentVoter; use Chill\DocStoreBundle\Security\Authorization\PersonDocumentVoter;
use Symfony\Component\Translation\TranslatorInterface;
/** /**
* @Route("/{_locale}/person/{person}/document") * @Route("/{_locale}/person/{person}/document")
@ -21,6 +22,18 @@ use Chill\DocStoreBundle\Security\Authorization\PersonDocumentVoter;
*/ */
class DocumentPersonController extends Controller class DocumentPersonController extends Controller
{ {
/**
*
* @var TranslatorInterface
*/
protected $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/** /**
* @Route("/", name="person_document_index", methods="GET") * @Route("/", name="person_document_index", methods="GET")
*/ */
@ -84,8 +97,12 @@ class DocumentPersonController extends Controller
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$em->persist($document); $em->persist($document);
$em->flush(); $em->flush();
$this->addFlash('success', $this->translator->trans("The document is successfully registered"));
return $this->redirectToRoute('person_document_index', ['person' => $person->getId()]); return $this->redirectToRoute('person_document_index', ['person' => $person->getId()]);
} elseif ($form->isSubmitted() and !$form->isValid()) {
$this->addFlash('error', $this->translator->trans("This form contains errors"));
} }
return $this->render('ChillDocStoreBundle:PersonDocument:new.html.twig', [ return $this->render('ChillDocStoreBundle:PersonDocument:new.html.twig', [
@ -128,10 +145,14 @@ class DocumentPersonController extends Controller
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush(); $this->getDoctrine()->getManager()->flush();
$this->addFlash('success', $this->translator->trans("The document is successfully updated"));
return $this->redirectToRoute( return $this->redirectToRoute(
'person_document_edit', 'person_document_edit',
['id' => $document->getId(), 'person' => $person->getId()]); ['id' => $document->getId(), 'person' => $person->getId()]);
} elseif ($form->isSubmitted() and !$form->isValid()) {
$this->addFlash('error', $this->translator->trans("This form contains errors"));
} }
return $this->render( return $this->render(

View File

@ -27,6 +27,7 @@ class ChillDocStoreExtension extends Extension implements PrependExtensionInterf
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml'); $loader->load('services.yml');
$loader->load('services/media.yml'); $loader->load('services/media.yml');
$loader->load('services/controller.yml');
} }

View File

@ -6,7 +6,8 @@ use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Chill\MainBundle\Entity\HasScopeInterface; use Chill\MainBundle\Entity\HasScopeInterface;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use ChampsLibres\AsyncUploaderBundle\Validator\Constraints\AsyncFileExists;
use Symfony\Component\Validator\Constraints as Assert;
/** /**
* @ORM\MappedSuperclass() * @ORM\MappedSuperclass()
@ -22,6 +23,9 @@ class Document implements HasScopeInterface
/** /**
* @ORM\Column(type="text") * @ORM\Column(type="text")
* @Assert\Length(
* min=2, max=250
* )
*/ */
private $title; private $title;
@ -44,6 +48,7 @@ class Document implements HasScopeInterface
* targetEntity="Chill\DocStoreBundle\Entity\StoredObject", * targetEntity="Chill\DocStoreBundle\Entity\StoredObject",
* cascade={"persist"} * cascade={"persist"}
* ) * )
* @Assert\Valid()
*/ */
private $object; private $object;

View File

@ -6,6 +6,7 @@ namespace Chill\DocStoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use ChampsLibres\AsyncUploaderBundle\Model\AsyncFileInterface; use ChampsLibres\AsyncUploaderBundle\Model\AsyncFileInterface;
use ChampsLibres\AsyncUploaderBundle\Validator\Constraints\AsyncFileExists;
/** /**
* Represent a document stored in an object store * Represent a document stored in an object store
@ -14,6 +15,9 @@ use ChampsLibres\AsyncUploaderBundle\Model\AsyncFileInterface;
* *
* @ORM\Entity() * @ORM\Entity()
* @ORM\Table("chill_doc.stored_object") * @ORM\Table("chill_doc.stored_object")
* @AsyncFileExists(
* message="The file is not stored properly"
* )
*/ */
class StoredObject implements AsyncFileInterface class StoredObject implements AsyncFileInterface
{ {

View File

@ -65,7 +65,9 @@ class PersonDocumentType extends AbstractType
->add('description', TextareaType::class, [ ->add('description', TextareaType::class, [
'required' => false 'required' => false
]) ])
->add('object', StoredObjectType::class) ->add('object', StoredObjectType::class, [
'error_bubbling' => true
])
->add('scope', ScopePickerType::class, [ ->add('scope', ScopePickerType::class, [
'center' => $options['center'], 'center' => $options['center'],
'role' => $options['role'] 'role' => $options['role']

View File

@ -0,0 +1,8 @@
services:
Chill\DocStoreBundle\Controller\:
resource: '../../../Controller'
tags: ['controller.service_arguments']
Chill\DocStoreBundle\Controller\DocumentPersonController:
autowire: true

View File

@ -0,0 +1,2 @@
## YAML Template.
---

View File

@ -0,0 +1 @@
The file is not stored properly: Le fichier n'est pas téléchargé correctement

View File

@ -1 +0,0 @@
{{ form_widget(form) }}

View File

@ -17,14 +17,32 @@
{% extends "ChillPersonBundle::layout.html.twig" %} {% extends "ChillPersonBundle::layout.html.twig" %}
{% import "@ChillDocStore/Macro/macro.html.twig" as m %}
{% set activeRouteKey = '' %} {% set activeRouteKey = '' %}
{% block title %}{{ 'Editing document for %name%'|trans({ '%name%': person.firstName|capitalize ~ ' ' ~ person.lastName } )|capitalize }}{% endblock %} {% block title %}{{ 'Editing document for %name%'|trans({ '%name%': person.firstName|capitalize ~ ' ' ~ person.lastName } )|capitalize }}{% endblock %}
{% block personcontent %} {% block personcontent %}
<h1>{{ 'Edit Document' | trans }}</h1> <h1>{{ 'Edit Document' | trans }}</h1>
{{ form_errors(form) }}
{{ form_start(form) }} {{ form_start(form) }}
{{ include('ChillDocStoreBundle:PersonDocument:_form.html.twig', {'button_label': 'Update'}) }}
{{ form_row(form.title) }}
{{ form_row(form.date) }}
{{ form_row(form.category) }}
{{ form_row(form.scope) }}
{{ form_row(form.description) }}
<div class="container">
<div class="grid-4 clear">
<label>{{ 'Existing document'|trans }}</label>
</div>
<div class="grid-8">
{{ m.download_button(document.object, document.title) }}
</div>
</div>
{{ form_row(form.object, { 'label': 'Document', 'existing': document.object }) }}
<ul class="record_actions"> <ul class="record_actions">
<li class="cancel"> <li class="cancel">
@ -33,16 +51,20 @@
</a> </a>
</li> </li>
<li class="edit"> <li class="edit">
<button class="sc-button bt-edit">{{ 'Edit'|trans }}</button> <button class="sc-button bt-edit">{{ 'Edit'|trans }}</button>
</li> </li>
{% if is_granted('CHILL_PERSON_DOCUMENT_DELETE', document) %} {# {% if is_granted('CHILL_PERSON_DOCUMENT_DELETE', document) %}
<li class="delete"> <li class="delete">
{{ include('ChillDocStoreBundle:PersonDocument:_delete_form.html.twig') }} {{ include('ChillDocStoreBundle:PersonDocument:_delete_form.html.twig') }}
</li> </li>
{% endif %} {% endif %} #}
</ul> </ul>
{{ form_end(form) }} {{ form_end(form) }}
{% endblock %} {% endblock %}
{% block js %}
<script src="{{ asset('build/async_upload.js') }}" type="text/javascript"></script>
{% endblock %}

View File

@ -22,9 +22,11 @@
{% block personcontent %} {% block personcontent %}
<h1>{{ 'Create new Document' | trans }}</h1> <h1>{{ 'Create new Document' | trans }}</h1>
{{ form_errors(form) }}
{{ form_start(form) }} {{ form_start(form) }}
{{ include('ChillDocStoreBundle:PersonDocument:_form.html.twig') }} {{ form_widget(form) }}
<ul class="record_actions"> <ul class="record_actions">
<li class="cancel"> <li class="cancel">