I'm doing validation with ajax, I'm starting with ajax now what is the problem:
When i use a div like this
<div id="success"></div>
, my screen duplicates my original form but with ajax form...when i remove this div "success" everything is ok.When I need doing some validation in my form i call my function in ajax but it doesn't show me any message.
this is my form
add.ctp
<div id="success"></div>
<div class="form" >
<h3>Registro de Compra de Grano </h3>
<?php echo $this->Form->create('SoyaProductorCompra');?>
<fieldset>
<?php
echo $this->Form->input('proveedor_carnet', array('label' => 'Cantidad en tonelada(s) métrica(s) del producto (TM)', 'id'=>'proveedor_carnet'));
echo $this->Form->input('producto', array(
'options' => array(
'GRANO DE SOYA' => 'Grano de Soya',
'GRANO DE GIRASOL' => 'Grano de Girasol'
), 'label'=>'Tipo de Grano'
));
echo $this->Form->input('toneladas', array('label' => 'Cantidad en tonelada(s) métrica(s) del producto (TM)', 'id'=>'toneladas'));
echo $this->Form->input('preciodolar', array(
'label' => 'Precio en Dolares Americanos por tonelada métrica (TM / $us)',
'style'=>'width:500px; height:30px;',
'default'=>0));
echo $this->Form->input('total', array('label' => 'Total en Dolares Americanos ($us)', 'default'=>0));
echo $this->Form->input('nrofactura', array('label' => 'Numero Factura', 'style'=>'width:500px; height:30px;','id'=>'total'));
?>
<div id="sending" style="display:none;">
<?php echo $this->html->image('ajax-loader.gif', array('alt' => 'Cargando...')); ?>
</div>
<?php
echo $this->Js->submit('Agregar Existencia', array(
'before'=>$this->Js->get('#sending')->effect('fadeIn'),
'success'=>$this->Js->get('#sending')->effect('fadeOut'),
'update'=>'#success'
));
?>
</fieldset>
<?php echo $this->Form->end(); ?>
</div>
validation.js
here is mi second problem I'm trying that my function show me a message error after this
echo $this->Form->input('proveedor_carnet', array('label' => 'Cantidad en tonelada(s) métrica(s) del producto (TM)', 'id'=>'proveedor_carnet'));
But my function doesn't show me any message
$(document).ready(function(){
$('#proveedor_carnet').blur(function(){
$.post(
'/cake/soyaproductorcompras/validate_form',
{ field: $('#proveedor_carnet').attr('id'), value: $('#proveedor_carnet').val() },
handleNmeValidation
);
});
function handleNmeValidation(error){
if(error.length > 0){
if($('#proveedor_carnet-notempty').length == 0){
$('proveedor_carnet').after('<div id="proveedor_carnet-notEmpty" class="error-message">' + error + '</div>');
}
}else{
$('#proveedor_carnet-notEmpty').remove();
}
}
});
SoyaProductorComprasController.php
public $helpers = array('Html', 'Form', 'Js');
public function add()
{
$this->loadModel('SoyaProductorCompra');
$this->loadModel('SoyaProveedor');
$this->loadModel('Productora');
$this->set('productores', $this->Productora->find('first', array('conditions' => array('user_id' => $this->Auth->user('id')))));
$this->set('soyaproveedores', $this->SoyaProveedor->find('list', array(
'fields' => array('id', 'nombre')
)));
if ($this->request->is('post')) {
$this->request->data['SoyaProductorCompra']['user_id'] = $this->Auth->user('id');
$this->request->data['SoyaProductorCompra']['soya_proveedor_id'] = $this->request->data['SoyaProductorCompra']['proveedor_carnet'];
if ($this->SoyaProductorCompra->save($this->request->data)) {
if($this->RequestHandler->isAjax()){
$this->render('success', 'ajax');
}else{
$this->Session->setFlash(__('La Información no fue Guardada.'));
return $this->redirect(array('action' => 'index'));
}
}
}
public function validate_form(){
$this->loadModel('SoyaProductorCompra');
if ($this->RequestHandler->isAjax()) {
debug($this->data['SoyaProductorCompra'][$this->params['form']['field']]);
$this->data['SoyaProductorCompra'][$this->params['form']['field']]=$this->params['form']['value'];
$this->SoyaProductorCompra->set($this->data);
if($this->SoyaProductorCompra->validate()){
$this->autoRender = FALSE;
}else{
$error=$this->validateErrors($this->SoyaProductorCompra);
$this->set('error', $error[$this->params['form']['field']]);
}
}
}
success.ctp
<p> Guardado </p>
validate_form.ctp
<?php echo $error; ?>
the error:
POST http://localhost:8080/cake/soyaproductorcompras/validate_form 500 (Internal Server Error) jquery2.1.js:4
n.ajaxTransport.k.cors.a.crossDomain.send jquery2.1.js:4
n.extend.ajax jquery2.1.js:4
n.each.n.(anonymous function) jquery2.1.js:4
(anonymous function) validation.js:4
n.event.dispatch jquery2.1.js:3
n.event.add.r.handle