I have a template with one dependent select with another (affiliate with enterprise), I fill that dependent select with ajax and jquery, but for some reason the request gives me an error: Error: Controller actions can only return ResponseInterface instance or null. Got string instead.
How can I solve this issue?
This is the code of the action:
public function add()
{
$this->loadModel('Observations');
$affiliateCertificate = $this->AffiliateCertificates->newEmptyEntity();
if($this->request->is('ajax')) {
$this->autorender = false;
$enterprise_id = $this->request->getData('enterprise_id');
$affiliates = $this->AffiliateCertificates->Affiliates->find('list')->where(['enterprise_id' => $enterprise_id]);
$html = '<option value="0">- Seleccione un afiliado- </option>\n';
foreach($affiliates as $key => $value) {
$html .= '<option value="' . $key . '">' . $value . '</option>\n';
}
return $html;
}elseif($this->request->is('post')) {
$discount_request_url = $this->request->getData('discount_request_url');
$type = $discount_request_url->getClientMediaType();
if($type == "image/gif" || $type == "image/jpeg" || $type == "image/x-png") {
$affiliateCertificate = $this->AffiliateCertificates->patchEntity($affiliateCertificate, $this->request->getData());
$affiliateCertificate->discount_request_url = DS . 'constancia_afiliacion' . DS . $discount_request_url->getClientFilename();
$affiliateCertificate->discount_request = WWW_ROOT . 'constancia_afiliacion' . DS . $discount_request_url->getClientFilename();
$affiliateCertificate->discount_request_filename = $discount_request_url->getClientFilename();
if ($this->AffiliateCertificates->save($affiliateCertificate)) {
$discount_request_url->moveTo(WWW_ROOT . 'constancia_afiliacion' . DS . $discount_request_url->getClientFilename());
$observations = $this->Observations->newEmptyEntity();
$user = $this->Authentication->getIdentity();
$observations->user_id = $user->id;
$observations->observation = sprintf('Se agregó una nueva constancia. (%s)', $affiliateCertificate->affiliate->full_name);
$this->Observations->save($observations);
$this->Flash->success(__('La constancia de afiliación ha sido guardada.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('La constancia de afiliación no pudo ser guardada. Por favor, intente nuevamente.'));
}else{
$this->Flash->error(__('Debe seleccionar un archivo de imagen.'));
}
}
$enterprises = $this->AffiliateCertificates->Enterprises->find('list', ['limit' => 200]);
$this->set(compact('affiliateCertificate', 'enterprises'));
$this->set('movement', ['anulada' => 'anulada', 'recibida' => 'recibida', 'archivada' => 'archivada']);
}
And this is the code of the template:
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\AffiliateCertificate $affiliateCertificate
*/
?>
<div class="row">
<aside class="column">
<div class="side-nav">
<h4 class="heading"><?= __('Acciones') ?></h4>
<?= $this->Html->link(__('Listar Constancias de Afiliación'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
</div>
</aside>
<div class="column-responsive column-80">
<div class="affiliateCertificates form content">
<?= $this->Form->create($affiliateCertificate, ['type' => 'file']) ?>
<fieldset>
<legend><?= __('Agregar Constancia de Afiliación') ?></legend>
<?php
echo $this->Form->control('enterprise_id', ['label' => 'Empresa', 'empty' => '- Seleccione una empresa -', 'options' => $enterprises, 'id' => 'enterpriseId']);
echo '<div class="inputOculto" style="display:none">'.$this->Form->control('affiliate_id', ['label' => 'Afiliado', 'id' => 'affiliateId']).'</div>';
echo $this->Form->control('start_date', ['label' => 'Fecha de inicio']);
echo $this->Form->control('reception_date', ['label' => 'Fecha de recepción']);
echo $this->Form->control('discount_request_url', ['label' => 'Solicitud de descuento', 'type' => 'file']);
echo $this->Form->control('movement_constancy', ['label' => 'Movimiento de constancia', 'options' => $movement]);
echo $this->Form->control('observations', ['label' => 'Observaciones']);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
</div>
</div>
<script>
$(function(){
$('#enterpriseId').change(function(){
$.ajax({
method:"POST",
url:"<?= $this->Url->build(['controller' => 'AffiliateCertificates', 'action' => 'add']) ?>",
data:{
enterprise_id:$(this).val()
},
success: function(data) {
$('#affiliateId').html(data);
$('.inputOculto').show();
},
headers:{
'X-CSRF-Token':$('meta[name="csrfToken"]').attr('content')
}
})
})
})
</script>