Publicación de datos del formulario en el controlador ZF2 con Ajax
EDITAR-
He publicado la respuesta a continuación.
La pregunta es que no entiendo cómo / dónde ZF2 publica datos de formulario cuando se presiona un botón de envío. Entonces, cuando lo hagoif ($this->getRequest()->isPost()){
después de la llamada ajax a continuación, me dice que no se han publicado datos.
Cuando hago lo anteriorisPost()
si la declaración funciona perfectamente cuando presiono el botón de envío, me dice que los datos se han publicado y luego me dice que los datos del formulario son válidos.
Aquí está la llamada ajax
<script>
$.ajax({
url: urlform,
type: 'POST',
dataType: 'json',
contentType: "application/json; charset=utf-8",
async: true,
data: ($("#newThoughtForm").serialize() + '&submit=go'),
success: function () {
console.log('SUBMIT WORKS');
setTimeout(function () { <?php echo $this->invokeIndexAction()->test(); ?> ;
}, 1000);
},
//This keeps getting executed because there is no response, as the controller action is not run on a Post()
error: function () {
console.log('There is error while submit');
setTimeout(function () { <?php echo $this->invokeIndexAction()->test(); ?> ;
}, 1000);
}
//I assume the data won't get pushed to the server if there is no response,
//but I can't figure out how to give a response in ZF2 since the controller is not
//run when the Post() is made.
});
Aquí está la forma-
use Zend\Form\Form;
class newAlbumForm extends Form
{
public function __construct()
{
parent::__construct('newAlbumForm');
$this->setAttribute('method', 'post');
$this->add(array(
'type' => 'AlbumModule\Form\newAlbumFieldset',
'options' => array(
'use_as_base_fieldset' => true
)
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'go'
),
));
}
}
La solicitud de la llamada ajax-
Request URL:http://test/newAlbum.html
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:46
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:PHPSESSID=h46r1fmj35d1vu11nua3r49he4
Host:test
Origin:http://test
Referer:http://test/newAlbum.html
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
album[albumText]:hello world
submit:go
Response Headersview source
Connection:Keep-Alive
Content-Length:4139
Content-Type:text/html
Date:Sun, 20 Oct 2013 16:52:15 GMT
Keep-Alive:timeout=5, max=99
Server:Apache/2.4.4 (Win64) PHP/5.4.12
X-Powered-By:PHP/5.4.12
La solicitud del botón de enviar
Request URL:http://test/newAlbum.html
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:46
Content-Type:application/x-www-form-urlencoded
Cookie:PHPSESSID=h46r1fmj35d1vu11nua3r49he4
Host:test
Origin:http://test
Referer:http://test/newAlbum.html
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Form Dataview sourceview URL encoded
album[albumText]:hello world
submit:go
Response Headersview source
Connection:Keep-Alive
Content-Length:4139
Content-Type:text/html
Date:Sun, 20 Oct 2013 16:52:14 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.4 (Win64) PHP/5.4.12
X-Powered-By:PHP/5.4.12
Aquí está el indexAction () en el controlador para completar-
public function indexAction()
{
echo 'console.log("Index Action is Called");';
$form = new \AlbumModule\Form\newAlbumForm();
if ($this->getRequest()->isPost()){
echo 'console.log("Data posted");';
$form->setData($this->getRequest()->getPost());
if ($form->isValid()){
echo 'console.log("Form Valid");';
//todo
$this->forward()->dispatch('newAlbum', array('action' => 'submitAlbum'));
return new ViewModel(
array(
'form' => $form
)
);
} else {
echo 'console.log("Form Invalid");';
return new ViewModel(
array(
'form' => $form
)
);
}
} else {
echo 'console.log("No data posted")';
return new ViewModel(
array(
'form' => $form
)
);
}
}
Como dije al principio, elisPost()
class devolverá un valor de true cuando el botón envíe el formulario, pero devolverá un valor de false cuando el formulario se envíe a través de Ajax.
EDITAR-
He publicado la respuesta a continuación.