Hi everyone,
I’m using the last version of Livewire and Laravel and I don’t know what I’m doing wrong. I have a big form that has some validations that I need to correct on real time.
The most dificult part is that the responses are stored verticaly, so for every form entry I have 95 responses entries
This is my Livewire Component
<?php
namespace App\Http\Livewire;
use App\Models\Escala;
use App\Models\Evaluacion;
use App\Models\Respuesta;
use Auth;
use Livewire\Component;
use function PHPUnit\Framework\isNull;
class PautaDigital extends Component
{
//I have to
public $evaluacionid, $respuestas,$gestiones, $resoluciones;
public $resultado = '';
public $saludo1 = '';
public $saludo2 = '';
public $saludo3 = '';
public $saludo4 = '';
public $saludo5 = '';
public $saludo6 = '';
.
.
.
public $deteccion1 = '';
public $deteccion2 = '';
public $deteccion3 = '';
public $deteccion4 = '';
public $infocorrecta1 = '';
public $infocorrecta2 = '';
public $infocorrecta3 = '';
public $infocorrecta4 = '';
public $procedimiento1 = '';
public $procedimiento2 = '';
public $procedimiento3 = '';
public $procedimiento4 = '';
public $usuarios3 = '';
public $comentario_interno = '';
public $retroalimentacion = '';
public $rules1 = [];
public $rules2 = [];
public $rules3 = [];
public function mount($evaluacionid){
$this->evaluacionid = $evaluacionid;
$this->respuestas = Respuesta::where('evaluacion_id',$evaluacionid)->get();
$this->gestiones = Escala::where('grupo_id',1)->get();
$this->resoluciones = Escala::where('grupo_id',2)->get();
foreach ($this->respuestas as $respuesta){
if($respuesta->atributo_id == 1){$this->saludo1 = $respuesta->respuesta_text;}
if($respuesta->atributo_id == 2){$this->saludo2 = $respuesta->respuesta_text;}
if($respuesta->atributo_id == 3){$this->saludo3 = $respuesta->respuesta_text;}
if($respuesta->atributo_id == 4){$this->saludo4 = $respuesta->respuesta_text;}
if($respuesta->atributo_id == 5){$this->saludo5 = $respuesta->respuesta_text;}
if($respuesta->atributo_id == 6){$this->saludo6 = $respuesta->respuesta_text;}
if($respuesta->atributo_id == 7){$this->frases1 = $respuesta->respuesta_text;}
if($respuesta->atributo_id == 8){$this->frases2 = $respuesta->respuesta_text;}
if($respuesta->atributo_id == 9){$this->frases3 = $respuesta->respuesta_text;}
if($respuesta->atributo_id == 10){$this->frases4 = $respuesta->respuesta_text;}
if($respuesta->atributo_id == 11){$this->frases5 = $respuesta->respuesta_text;}
.
.
.
if($respuesta->atributo_id == 91){$this->usuarios1 = $respuesta->respuesta_text;}
if($respuesta->atributo_id == 92){$this->usuarios2 = $respuesta->respuesta_text;}
if($respuesta->atributo_id == 93){$this->usuarios3 = $respuesta->respuesta_text;}
if($respuesta->atributo_id == 94){$this->comentario_interno = $respuesta->respuesta_text;}
if($respuesta->atributo_id == 95){$this->retroalimentacion = $respuesta->respuesta_text;}
}
$this->rules1 = [
'personalizacion4' => 'required',
'motivo' => 'required',
'gestion1' => 'required',
'deteccion1' => 'required',
'infocorrecta1' => 'required',
'procedimiento1' => 'required',
'resolucion1' => 'required',
'retroalimentacion' => 'required',
];
}
public function guardarespuestahijo($atributoid, $atributotext){
$respuesta = Respuesta::where('evaluacion_id', $this->evaluacionid)->where('atributo_id', $atributoid)->first();
if($respuesta){
$respuesta->update(['respuesta_text' => $atributotext]);
}else{
Respuesta::create([
'atributo_id' => $atributoid,
'evaluacion_id' => $this->evaluacionid,
'respuesta_text' => $atributotext,
'origen_id' => 1,
]);
}
}
public function save(){
// VALIDATIONS
if(array_count_values($this->rules2)>1 and array_count_values($this->rules3)>1){
$this->validate(array_merge($this->rules1, $this->rules2, $this->rules3));
}elseif(array_count_values($this->rules2)>1){
$this->validate( array_merge($this->rules1, $this->rules2));
}else{
$this->validate($this->rules1);
}
// SAVE RESPONSES
$this->guardarespuestahijo(1, $this->saludo1);
$this->guardarespuestahijo(2, $this->saludo2);
$this->guardarespuestahijo(3, $this->saludo3);
$this->guardarespuestahijo(4, $this->saludo4);
$this->guardarespuestahijo(5, $this->saludo5);
$this->guardarespuestahijo(12, $this->cordialidad1);
$this->guardarespuestahijo(13, $this->cordialidad2);
$this->guardarespuestahijo(14, $this->cordialidad3);
$this->guardarespuestahijo(15, $this->cordialidad4);
.
.
// CALCULATE SCORES
$penc = 0;
$pecu = 100;
$pecn = 100;
$pecc = 100;
$penctotal = 100;
//Saludo
$respuesta = Respuesta::where('evaluacion_id', $this->evaluacionid)->where('atributo_id', 6)->first();
if($respuesta->respuesta_text == 'Si'){
$penc = $penc + 10;
}
//Frases
$respuesta = Respuesta::where('evaluacion_id', $this->evaluacionid)->where('atributo_id', 11)->first();
if($respuesta->respuesta_text == 'Si'){
$penc = $penc + 10;
}
//Cordialidad
$respuesta = Respuesta::where('evaluacion_id', $this->evaluacionid)->where('atributo_id', 16)->first();
if($respuesta->respuesta_text == 'Si'){
$penc = $penc + 10;
}
//Gestion de Planillas e Info
$respuesta = Respuesta::where('evaluacion_id', $this->evaluacionid)->where('atributo_id', 21)->first();
if($respuesta->respuesta_text == 'Si'){
$penc = $penc + 10;
}
//Ortografia
$respuesta = Respuesta::where('evaluacion_id', $this->evaluacionid)->where('atributo_id', 28)->first();
if($respuesta->respuesta_text == 'Si'){
$penc = $penc + 10;
}
//Personalizacion
$respuesta = Respuesta::where('evaluacion_id', $this->evaluacionid)->where('atributo_id', 32)->first();
if($respuesta->respuesta_text == 'No Aplica'){
$penctotal = $penctotal - 10;
}elseif($respuesta->respuesta_text == 'Si'){
$penc = $penc + 10;
}
//Seguridad
$respuesta = Respuesta::where('evaluacion_id', $this->evaluacionid)->where('atributo_id', 35)->first();
if($respuesta->respuesta_text == 'Si'){
$penc = $penc + 10;
}
//Manejo de Silencios
$respuesta = Respuesta::where('evaluacion_id', $this->evaluacionid)->where('atributo_id', 39)->first();
if($respuesta->respuesta_text == 'Si'){
$penc = $penc + 10;
}
//Aseguramiento
$respuesta = Respuesta::where('evaluacion_id', $this->evaluacionid)->where('atributo_id', 42)->first();
if($respuesta->respuesta_text == 'No Aplica'){
$penctotal = $penctotal - 10;
}elseif($respuesta->respuesta_text == 'Si'){
$penc = $penc + 10;
}
//Ofrecimiento
$respuesta = Respuesta::where('evaluacion_id', $this->evaluacionid)->where('atributo_id', 47)->first();
if($respuesta->respuesta_text == 'No Aplica'){
$penctotal = $penctotal - 10;
}elseif($respuesta->respuesta_text == 'Si'){
$penc = $penc + 10;
}
$pencfinal = $penc / $penctotal * 100;
if($this->pecu_deteccion == 'checked' or $this->pecu_infocorrecta == 'checked' or $this->pecu_procedimiento == 'checked' or $this->pecu_pocoprofesional == 'checked' or $this->pecu_manipulacliente == 'checked' or $this->pecu_cierreinteraccion == 'checked' or $this->pecu_provocacierre == 'checked'){
$pecu = 0;
}
if($this->pecn_beneficio == 'checked' or $this->pecn_fraude == 'checked' or $this->pecn_nosondea == 'checked' or $this->pecn_tipificacion == 'checked' or $this->pecn_factibilidad == 'checked' or $this->pecn_otragestion == 'checked'){
$pecn = 0;
}
if($this->pecc_infoconfidencial == 'checked' or $this->pecc_novalidadatos == 'checked' or $this->pecc_cierre == 'checked' or $this->pecc_infoerronea == 'checked'){
$pecc = 0;
}
// SAVE THE SCORES AND MORE STATES ON THE FORM ID
$evaluacion = Evaluacion::where('id',$this->evaluacionid)->first();
$evaluacion->penc = $pencfinal;
$evaluacion->pecu = $pecu;
$evaluacion->pecn = $pecn;
$evaluacion->pecc = $pecc;
$evaluacion->estado_id = 2;
$evaluacion->user_id = Auth::user()->id;
$evaluacion->created_at = now();
$evaluacion->user_completa = Auth::user()->name;
$evaluacion->fecha_completa = now()->format('Y-m-d H:i:s');
$evaluacion->save();
}
public function render()
{
// VALIDATIONS ON REAL TIME
if($this->deteccion1 == "No" or $this->deteccion2 == "No" or $this->deteccion3 == "No"){
$this->pecu_deteccion = "checked";
}else{
$this->pecu_deteccion = "";
}
if($this->infocorrecta1 == "No" or $this->infocorrecta2 == "No" or $this->infocorrecta3 == "No"){
$this->pecu_infocorrecta = "checked";
}else{
$this->pecu_infocorrecta = "";
}
if($this->procedimiento1 == "No" or $this->procedimiento2 == "No" or $this->procedimiento3 == "No"){
$this->pecu_procedimiento = "checked";
}else{
$this->pecu_procedimiento = "";
}
if($this->gestion2 != "" or $this->deteccion2 != "" or $this->infocorrecta2 != "" or $this->procedimiento2 != "" or $this->resolucion2 != ""){
$this->rules2 = [
'gestion2' => 'required',
'deteccion2' => 'required',
'infocorrecta2' => 'required',
'procedimiento2' => 'required',
'resolucion2' => 'required',
];
}
if($this->gestion3 != "" or $this->deteccion3 != "" or $this->infocorrecta3 != "" or $this->procedimiento3 != "" or $this->resolucion3 != ""){
$this->rules3 = [
'gestion3' => 'required',
'deteccion3' => 'required',
'infocorrecta3' => 'required',
'procedimiento3' => 'required',
'resolucion3' => 'required',
];
}
return view('livewire.pauta-digital');
}
}
On the form I have all the variables conected with wire:model.lazy
So, I have all the variables on mount() and I want to reload all the page after save all the data, because it is updating external data from the component.