a falta de validar

This commit is contained in:
Jaime Jiménez
2023-07-13 08:22:09 +02:00
parent c4a7cfc799
commit ade327004f
4 changed files with 71 additions and 8 deletions

View File

@ -75,15 +75,23 @@ class Maquinasdefecto extends \App\Controllers\GoBaseResourceController {
// JJO
$sanitizedData['user_created_id'] = $session->id_user;
$noException = true;
if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) :
if ($this->canValidate()) :
// JJO: se añade que se checkeen los intervalos de ancho y tirada.
// En caso de error se devuelve un mensaje.
try {
$successfulResult = $this->model->skipValidation(true)->save($sanitizedData);
$error = $this->model->checkIntervals($sanitizedData);
if(empty($error)){
$successfulResult = $this->model->skipValidation(true)->save($sanitizedData);
}
else{
$successfulResult = false;
$this->viewData['errorMessage'] = $error;
$this->session->setFlashdata('formErrors', $this->model->errors());
}
} catch (\Exception $e) {
$noException = false;
$this->dealWithException($e);
@ -160,12 +168,20 @@ class Maquinasdefecto extends \App\Controllers\GoBaseResourceController {
$noException = true;
if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) :
if ($this->canValidate()) :
try {
$successfulResult = $this->model->skipValidation(true)->update($id, $sanitizedData);
// JJO: se añade que se checkeen los intervalos de ancho y tirada.
// En caso de error se devuelve un mensaje.
try {
$error = $this->model->checkIntervals($sanitizedData);
if(empty($error)){
$successfulResult = $this->model->skipValidation(true)->update($id, $sanitizedData);
}
else{
$successfulResult = false;
$this->viewData['errorMessage'] = $error;
$this->session->setFlashdata('formErrors', $this->model->errors());
}
} catch (\Exception $e) {
$noException = false;
$this->dealWithException($e);

View File

@ -31,6 +31,8 @@ return [
'userCreatedId' => 'User Created ID',
'userUpdatedId' => 'User Updated ID',
'validation' => [
'error_ancho_overlap' => 'The range [Min Width, Max Width] is overlapped with another one for the selected type.',
'error_tirada_overlap' => 'The range [Min Printing, Max Printing] is overlapped with another one for the selected type.',
'alto_max' => [
'decimal' => 'The {field} field must contain a decimal number.',
'required' => 'The {field} field is required.',

View File

@ -31,6 +31,8 @@ return [
'userCreatedId' => 'User Created ID',
'userUpdatedId' => 'User Updated ID',
'validation' => [
'error_ancho_overlap' => 'El rango [Ancho Min, Ancho Max] se solapa con otro existente para el tipo seleccionado.',
'error_tirada_overlap' => 'El rango [Tirada Min, Tirada Max] se solapa con otro existente para el tipo seleccionado.',
'alto_max' => [
'decimal' => 'El campo {field} debe contener un número decimal.',
'required' => 'El campo {field} es obligatorio.',

View File

@ -145,6 +145,7 @@ class MaquinasDefectoModel extends \App\Models\GoBaseModel
->select(
"t1.id AS id, t1.tipo AS tipo, t1.ancho_min AS ancho_min, t1.ancho_max AS ancho_max, t1.alto_min AS alto_min, t1.alto_max AS alto_max, t1.tirada_min AS tirada_min, t1.tirada_max AS tirada_max, t2.nombre AS maquina"
);
$builder->where('t1.is_deleted', 0);
$builder->join("lg_maquinas t2", "t1.maquina_id = t2.id", "left");
return empty($search)
@ -172,4 +173,46 @@ class MaquinasDefectoModel extends \App\Models\GoBaseModel
->orLike("t2.nombre", $search)
->groupEnd();
}
public function checkIntervals($data = []){
$ancho_min = $this->db
->table($this->table)
->selectMin("ancho_min")
->where("is_deleted", 0)
->where("tipo", $data["tipo"])->get()->getRow()->ancho_min;
$ancho_max = $this->db
->table($this->table)
->selectMin("ancho_max")
->where("is_deleted", 0)
->where("tipo", $data["tipo"])->get()->getRow()->ancho_max;
$tirada_min = $this->db
->table($this->table)
->selectMin("tirada_min")
->where("is_deleted", 0)
->where("tipo", $data["tipo"])->get()->getRow()->tirada_min;
$tirada_max = $this->db
->table($this->table)
->selectMin("tirada_max")
->where("is_deleted", 0)
->where("tipo", $data["tipo"])->get()->getRow()->tirada_max;
if(!is_null($ancho_max) && !is_null($ancho_min)){
if($data["ancho_min"]>=intval($ancho_min) ||
$data["ancho_max"]<=intval($ancho_max) )
return lang('MaquinasPorDefecto.validation.error_ancho_overlap');
}
if(!is_null($tirada_max) && !is_null($tirada_min)){
if($data["tirada_min"]>=intval($tirada_min) ||
$data["tirada_max"]<=intval($tirada_max) )
return lang('MaquinasPorDefecto.validation.error_tirada_overlap');
}
return "";
}
}