Jump to content


Set Custom Message Validation (CALLBACK BEFORE INSERT)

custom message callback_before_insert Problem Message Validation

  • Please log in to reply
2 replies to this topic

#1 amarokinteractive

amarokinteractive

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 04 October 2016 - 11:55 PM

Hello everyone. Please if you can help me, because this simple problem, it seems that this library GC is a huge problem and is taking me patience.
 
I launches the default error sign and shows no sign me my validation. I've looked around the forum and many people had the same problem, but all the answers are in the year 2012 or 2013, nothing new ... and I raises many questions that custom validation functionality works well.
 
 

The validation works just perfect, which is the most elaborate part. And the stupidest part of putting the validation message that desire is driving me crazy !!!

 

 
 
Here is the code
$crud = $this->generate_crud('facturas')->columns('persona_fk','tipo','numero_factura','fecha_creacion','importe_total');


$crud->fields('persona_fk','tipo','numero_factura','fecha_creacion','importe_total','detalle','url_archivo');
$crud->required_fields('persona_fk','tipo','numero_factura','fecha_creacion','importe_total','detalle');


$this->mPageTitle = 'Facturas';


$crud->display_as('persona_fk','Persona');
$crud->display_as('tipo',' Tipo de Factura');
$crud->display_as('numero_factura',' Número de Factura');
$crud->display_as('url_archivo','Imagen Factura');
$crud->display_as('fecha_creacion','Fecha Factura');
        $crud->display_as('importe_total','Importe Total');


$crud->set_rules('numero_factura',' Número de Factura','numeric|exact_length[12]');


$crud->field_type('tipo','dropdown', array('A' => 'A', 'B' => 'B','C' => 'C' , 'R' => 'R' , 'M' => 'M'));


        $crud->set_field_upload('url_archivo','assets/uploads/facturas');


$crud->callback_before_insert(array($this,'verificar_factura_existente'));


$crud->set_relation('persona_fk','personas','{apellido} {nombre} ' );


$crud->unset_bootstrap();
$this->render_crud();




}


  }


public function verificar_factura_existente($post_array){


$data = $this->facturas->verificar_factura_existente($post_array['numero_factura'],$post_array['persona_fk'],$post_array['tipo']);


if($data['resultado'] == 1){


/*echo json_encode(
            array('success' => false,
      'error_message' => 'Factura ya existe para la persona',
  'error_fields' => array('numero_factura' => 'Factura ya existe para la persona'))
);*/
//$this->set_echo_and_die();
//return FALSE;


$this->form_validation->set_message('verificar_factura_existente', 'Numero de Factura y Tipo existente para la persona seleccionada.');


return false;


    }else{


     return true;


}


//return $post_array;


}

THANKS !!!

 



#2 giuseppe

giuseppe

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 12 October 2017 - 09:56 AM

anyone has a solution or a workaround?

 

thanks



#3 Json Sean

Json Sean

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 01 November 2017 - 05:32 AM

https://www.groceryc...ges/#entry15705

 

 

Here is the way to custom error message when editing :

 

1、modify Grocery_CRUD.php

protected function db_update($state_info)
{

...

if($this->callback_before_update !== null)
{
$callback_return call_user_func($this->callback_before_update$post_data$primary_key);

if(!empty($callback_return) && is_array($callback_return))
{
$post_data $callback_return;
}
elseif($callback_return === false)
{
return false;
}elseifis_string($callback_return))
{
return array('status' => false,'message' => $callback_return);
}


}

 

...

 

}

 

 

 

 

protected function update_layout($update_result false$state_info null)

{
@ob_end_clean();
if($update_result === false)
{
echo json_encode(array('success' => $update_result));
}elseif(isset($update_result['status'])){
//TODO:custom error
$error_message '<p>'.$update_result['message']. '</p>';
echo json_encode(array(
'success' => $update_result['status'] ,
'error_message' => $error_message,
));
}

else
{

 

...

 

}

 

2、edit.js

 

...

 

$('#crudForm').ajaxSubmit({

dataType'text',
cache'false',
beforeSendfunction(){
$("#FormLoading").show();
},
successfunction(result) {
data = $.parseJSON(result);
if (data.success) {

if(save_and_close)
{
if ($('#save-and-go-back-button').closest('.ui-dialog').length === 0) {
window.location = data.success_list_url+'?type=edit';
//form_success_message(data.success_list_url);
else {
$(".ui-dialog-content").dialog("close");
success_message(data.success_message);
}

return true;
}

form_success_message(data.success_message);
}else if (!data.success & data.error_message != null){
//TODO:custom error message
form_error_message(data.error_message);
else {
form_error_message(message_update_error);
}
},
errorfunction(){
form_error_messagemessage_update_error );
}
});

...

 

3、in you callback function, just return the string of your error message:

 

function before_xxx_update_callback($post_array$primary_key) {

if( error ){
$message 'error message';
return $message;
}

return $post_array;

}

 

 

To make it extendibility , you may define Cutom_Grocery_CRUD.php extending Grocery_CRUD.php , then modify Cutom_Grocery_CRUD.php.

 






Also tagged with one or more of these keywords: custom message, callback_before_insert, Problem Message Validation

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users