So i've got some information I send with a Post ajax request like this:
$.ajax("<?php echo $this->url('panel/academy/locals/editlocal')?>", {
method: 'POST',
data: {
id: id, // number
distrito: newDistrito, // string
direccion: newDireccion, // string
telefono1: newTelf1, // string
telefono2: newTelf2, // string
efectivo: $('#local_'+id).find('.efectivo').prop('checked'),
visa: $('#local_'+id).find('.visa').prop('checked'),
mastercard: $('#local_'+id).find('.mastercard').prop('checked'),
american: $('#local_'+id).find('.american').prop('checked'),
deposito: $('#local_'+id).find('.deposito').prop('checked'),
central: newCentral,
}
})
efectivo, visa, american, mastercard and central are all booleans.
In the server I do this:
$prg = $this->prg();
if($prg instanceof \Zend\Http\PhpEnvironment\Response)
return $prg;
elseif ($prg === false)
return new JsonModel(array(
'msg' =>'error prg false',
'success' => false
));
$localForm = new LocalForm();
$localForm->setData($prg);
if(!$localForm->isValid()){
return new JsonModel(array(
'success ' => false,
'error' => 'Invalid',
));
}
$id = (int) $localForm->get('id')->getValue();
$local = (new LocalMapper())->byId($id);
//change local model
$local->setDistrictId($localForm->get('distrito')->getValue());
$local->setAddress($localForm->get('direccion')->getValue());
$local->setPhone1($localForm->get('telefono1')->getValue());
$local->setPhone2($localForm->get('telefono2')->getValue());
$local->setCentral($localForm->get('central')->getValue());
$localpayments = (new LocalPaymentMapper())->byLocalId($id);
// I will have to fix what I'm about to do as soon as possible
foreach($localpayments as $payment){
// Efectivo 1
// Visa 2
// Mastercard 3
// American Express 4
switch($payment->getPaymentId()){
case 1:
if(!$prg['efectivo']){
$payment->delete();
}
break;
case 2:
if(!$prg['visa']){
$payment->delete();
}
break;
case 3:
if(!$prg['mastercard']){
$payment->delete();
}
break;
case 4:
if(!$prg['american']){
$payment->delete();
}
break;
default:
break;
}
}
the problem is that when i try to add an element to the localForm that holds a boolean value like a checkbox the form is always invalid and so I never get to the part where I acccess the db and save the changes the user made. I tried using the $prg array to acces the info but had no luck either. How can I acomplish this? am I trying the wrong approach?
Thanks in advance
here is the full form
<?php
namespace GrouPanel\Form\Locals;
use GrouCore\Form\Form;
use GrouCore\Form\Element\DistrictSelector;
use GrouCore\Form\Element\UserPhone;
use GrouCore\Form\Element\LocalAddress;
use Zend\Form\Element\Checkbox;
class LocalForm extends Form {
public function __construct($name = "localForm") {
parent::__construct ( $name );
$this->setAttribute ( 'novalidate', 'novalidate' );
$localDistrict = new DistrictSelector ( 'distrito' );
$localAddress = new LocalAddress ( 'direccion' );
$localPhone1 = new UserPhone ( 'telefono1' );
$localPhone2 = new UserPhone ( 'telefono2' );
$localCentral = new Checkbox ( 'central' );
$this->add ( array (
'name' => 'id',
'type' => 'text'
) );
$this->add( $localDistrict )
->add( $localAddress )
->add ( $localPhone1 )
->add ( $localPhone2 )
->add ( $localCentral );
$this->getInputFilter ();
}
DistrictSelector, UserPhone and LocalAddress all work as expected, the checkbox seems to be the problem somehow
You use the syntax jQuery.ajax( url [, settings ] ). Try adding key :
dataType: 'json',
in your setting structure.
Http stringified every thing before send. so you can use 1 or 0 instead of true or false in js.
efectivo: ($('#local_'+id).find('.efectivo').prop('checked')) ? 1 : 0,
add to ajax
dataType: 'json',