Last active
August 29, 2015 14:16
-
-
Save juliangorge/9ea9ac461517b1778fbd to your computer and use it in GitHub Desktop.
My form with Ajax does not work (is not valid). Why?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Admin\Entity; | |
use DateTime; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* @ORM\Entity | |
* @ORM\Table(name="concursos") | |
*/ | |
class Concursos | |
{ | |
/** | |
* @ORM\Id | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
protected $id; | |
/** @ORM\Column(name="titulo", type="string", length=150, nullable=false) */ | |
protected $titulo; | |
/** @ORM\Column(name="fecha", type="date", nullable=false) */ | |
protected $fecha; | |
/** @ORM\Column(name="cuerpo", type="string", nullable=false) */ | |
protected $cuerpo; | |
public function getId() { | |
return $this->id; | |
} | |
public function setTitulo($titulo) { | |
$this->titulo = $titulo; | |
} | |
public function getTitulo() { | |
return $this->titulo; | |
} | |
public function setFecha(DateTime $fecha) { | |
$this->fecha = $fecha; | |
} | |
public function getFecha() { | |
return $this->fecha; | |
} | |
public function setCuerpo($cuerpo) { | |
$this->cuerpo = $cuerpo; | |
} | |
public function getCuerpo() { | |
return $this->cuerpo; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Admin\Controller; | |
use Zend\Mvc\Controller\AbstractActionController; | |
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator; | |
use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator; | |
use Zend\Paginator\Paginator; | |
use Zend\View\Model\ViewModel; | |
class ConcursosController extends AbstractActionController | |
{ | |
/** | |
* @var Doctrine\ORM\EntityManager | |
*/ | |
protected $em; | |
public function getEntityManager() | |
{ | |
if(null === $this->em) | |
$this->em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); | |
return $this->em; | |
} | |
public function crearAction() | |
{ | |
$objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager'); | |
$form = new \Admin\Form\ConcursosForm($objectManager); | |
$concurso = new \Admin\Entity\Concursos(); | |
$form->bind($concurso); | |
$viewmodel = new ViewModel(); | |
$viewmodel->setTerminal($this->getRequest()->isXmlHttpRequest()); | |
$viewmodel->setVariables(array('form' => $form)); | |
if($this->getRequest()->isXmlHttpRequest()){ | |
$form->setData($this->getRequest()->getPost()); | |
if($form->isValid()){ | |
$objectManager->persist($concurso); | |
$objectManager->flush(); | |
} | |
} | |
return $viewmodel; | |
} | |
public function guardarAction() | |
{ | |
$objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager'); | |
$form = new \Admin\Form\ConcursosForm($objectManager); | |
$concurso = new \Admin\Entity\Concursos(); | |
$form->bind($concurso); | |
if($this->getRequest()->isXmlHttpRequest()){ | |
if($this->getRequest()->isPost()){ | |
$form->setData($this->getRequest()->getPost()); | |
$hello = 0; | |
if($form->isValid()){ | |
$hello = 1; | |
$objectManager->persist($concurso); | |
$objectManager->flush(); | |
}else{ | |
foreach($form->getMessages() as $key=>$row) | |
if(!empty($row) && $key != 'submit') | |
foreach($row as $keyer => $rower) | |
$messages[$key][] = $rower; | |
} | |
if(!empty($messages)) | |
$this->getResponse()->setContent(\Zend\Json\Json::encode($messages)); | |
else | |
$this->getResponse()->setContent(\Zend\Json\Json::encode(array('hello' => $hello))); | |
} | |
return $this->getResponse(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Admin\Form; | |
use Admin\Entity\Concursos; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator; | |
use Zend\Form\Fieldset; | |
use Zend\InputFilter\InputFilterProviderInterface; | |
class ConcursosFieldset extends Fieldset implements InputFilterProviderInterface | |
{ | |
public function __construct(ObjectManager $objectManager) | |
{ | |
parent::__construct('concursos'); | |
$this->setHydrator(new DoctrineHydrator($objectManager,'Admin\Entity\Concursos')) | |
->setObject(new Concursos()); | |
$this->add(array( | |
'name' => 'titulo', | |
'type' => 'Zend\Form\Element\Text', | |
'attributes' => array( | |
'required' => 'required', | |
'placeholder' => 'Ingrese aquí un título', | |
'class' => 'form-control titulo', | |
'autocomplete' => 'off' | |
), | |
)); | |
$this->add(array( | |
'name' => 'fecha', | |
'type' => 'Zend\Form\Element\Text', | |
'attributes' => array( | |
'id' => 'fecha', | |
'placeholder' => '31/01/2016', | |
'class' => 'form-control fecha', | |
'autocomplete' => 'off' | |
), | |
)); | |
$this->add(array( | |
'name' => 'cuerpo', | |
'type' => 'Zend\Form\Element\Textarea', | |
'attributes' => array( | |
'required' => 'required', | |
'placeholder' => 'Ingrese aquí un cuerpo', | |
'class' => 'form-control cuerpo', | |
'autocomplete' => 'off' | |
), | |
)); | |
} | |
public function getInputFilterSpecification() | |
{ | |
return array( | |
'titulo' => array( | |
'required' => true, | |
), | |
'fecha' => array( | |
'required' => true, | |
), | |
'cuerpo' => array( | |
'required' => true, | |
), | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Admin\Form; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator; | |
use Zend\Form\Form; | |
class ConcursosForm extends Form | |
{ | |
public function __construct(ObjectManager $objectManager) | |
{ | |
parent::__construct('concursos'); | |
$this->setHydrator(new DoctrineHydrator($objectManager,'Admin\Entity\Concursos'))->setObject(new \Admin\Entity\Concursos()); | |
$concurso = new \Admin\Form\ConcursosFieldset($objectManager); | |
$concurso->setName('concurso'); | |
$concurso->setUseAsBaseFieldset(true); | |
$this->add($concurso); | |
$this->add(array( | |
'type' => 'Zend\Form\Element\Csrf', | |
'name' => 'csrf', | |
)); | |
$this->add(array( | |
'name' => 'submit', | |
'attributes' => array( | |
'class' => 'form-control btn btn-primary', | |
'type' => 'submit', | |
'value' => 'Publicar concurso', | |
), | |
)); | |
/*$this->setValidationGroup(array( | |
'csrf', | |
'concurso' => array( | |
'titulo', | |
'fecha', | |
'cuerpo' | |
) | |
));*/ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$this->headLink() | |
->prependStylesheet($this->basePath().'/js/jquery-ui/jquery-ui.min.css'); | |
$this->headScript() | |
->appendFile($this->basePath().'/js/jquery-ui/jquery-ui.min.js') | |
->appendFile($this->basePath().'/js/ajax/ajax-dialog.js'); | |
?> | |
<?php | |
//$form->setAttribute('action', $this->url('concursos/default', array('action' => 'guardar')))->prepare(); | |
echo $this->form()->openTag($form); | |
$concurso = $form->get('concurso'); | |
?> | |
<br> | |
<?php echo $this->formRow($concurso->get('titulo')); ?> | |
<br> | |
<script> $(function() { $('#fecha').datepicker({dateFormat: 'yy-mm-dd'}); }); </script> | |
<div class="expiro"> | |
<label>Fecha de expiro: </label> | |
<?php echo $this->formRow($concurso->get('fecha')); ?> | |
</div> | |
<br> | |
<div class="cuerpo"> | |
<?php echo $this->formRow($concurso->get('cuerpo')); ?> | |
</div> | |
<br> | |
<?php echo $this->formRow($form->get('csrf')); ?> | |
<?php echo $this->formRow($form->get('submit')); ?> | |
<?php echo $this->form()->closeTag(); ?> | |
<a id="hola">x</a> | |
<div id="xxx"></div> | |
<script> | |
//$('#concursos').submit(function(){ | |
$('#hola').click(function(){ | |
$.ajax({ | |
url: "<?php echo $this->url('concursos/default', array('action' => 'crear')); ?>", | |
type: 'POST', | |
dataType: 'json', | |
contentType: "application/json; charset=utf-8", | |
async: true, | |
data: $('#concursos').serialize(), | |
success: function (data) { | |
alert('No hay error, y España te ataca.'); | |
$("#xxx").html(data); | |
console.log('Enviando información..'); | |
console.log(data); | |
}, | |
error: function (data) { | |
//alert('Error al enviar la información..'); | |
console.log('Error al enviar la información..'); | |
} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment