Commit 28d3f31f authored by Mat's avatar Mat

Affichage et soumission du formulaire create + son controller ->

* remarque, les use ajoutés sont importants pour la génération d'erreurs
400 parsées en json
* j'ai du m'occuper du datetime modify_at en prepersist
parent bd123a69
......@@ -75,4 +75,4 @@ swiftmailer:
fos_js_routing:
routes_to_expose:
- api_owner_*
- api_image_update
- api_image_*
......@@ -2,13 +2,38 @@
namespace APIBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use APIBundle\Form\Image\ImageAPIType;
use TechnoBundle\Entity\Image;
class ImageAPIController extends MasterAPIController
{
/**
* @param Request
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function createAction(Request $request)
{
$image = new Image();
$form = $this->createForm(ImageAPIType::class, $image);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($image);
$em->flush();
return $this->api($image);
}
return $this->api($form , Response::HTTP_BAD_REQUEST);
}
/**
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
......
......@@ -40,6 +40,12 @@ api_owner_delete_one:
## ImageAPI
api_image_create:
path: /image/add
defaults:
_controller: APIBundle:ImageAPI:create
methods: [ POST ]
api_image_read:
path: /images
defaults:
......
......@@ -80,6 +80,7 @@ class Image
public function prePersist()
{
$this->createdAt = new \DateTime();
$this->modifyAt = new \DateTime();
}
/**
......
......@@ -133,6 +133,24 @@
reloadGridImage();
let form = document.querySelector('form[name="image"]');
/*
* Affiche le formulaire (create)
*/
let createBtns = document.querySelectorAll('a.btn-create');
createBtns.forEach(function(createBtn)
{
createBtn.addEventListener('click', function(e)
{
form.parentNode.classList.remove('hidden');
submitURL = Routing.generate('api_image_create');
});
});
/*
* Recharge le tableau (read)
......@@ -148,5 +166,24 @@
});
/*
* Soumission du formulaire (create)
*/
form.addEventListener('submit', function(e)
{
let form = this;
e.preventDefault();
AJAX('POST', submitURL, function()
{
reloadGridImage();
form.parentNode.classList.add('hidden');
},
form);
});
</script>
{% endblock %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment