lundi 1 avril 2019

Validation array laravel

I try to validate two arrays and the second array need the values from the first.

the rules are that:

$rules = [
            'user'                => 'required',
            'resultadoCambio'     => 'required',
            'conclusion'          => 'required'
        ];
        $preguntas = PreguntasPir::all();
        foreach ($preguntas as $pregunta) {
            $rules['pregunta'][$pregunta->id] = 'required';
            $rules['justificacion'][$pregunta->id] = 'required_if:pregunta['.$pregunta->id.'],'.strtolower($pregunta->caso_pregunta);
        }
        error_log('reglas -->' . print_r($rules, TRUE));
        return $rules;

and the log:

[Mon Apr  1 11:07:12 2019] reglas -->Array
(
    [user] => required
    [resultadoCambio] => required
    [conclusion] => required
    [pregunta] => Array
        (
            [1] => required
            [2] => required
            [3] => required
            [4] => required
            [5] => required
            [6] => required
        )

    [justificacion] => Array
        (
            [1] => required_if:pregunta[1],no
            [2] => required_if:pregunta[2],si
            [3] => required_if:pregunta[3],si
            [4] => required_if:pregunta[4],no
            [5] => required_if:pregunta[5],si
            [6] => required_if:pregunta[6],si
        )

)

and the messages I configure that:

public function messages()
    {
        $messages = [
            'resultadoCambio.required'     => 'El campo resultado de cambio es obligatorio .',
            'conclusion.required'          => 'El campo conclusión es obligatorio'
        ];
        $preguntas = PreguntasPir::all();
        foreach ($preguntas as $pregunta) {
            $messages['pregunta['.$pregunta->id.'].required'] = 'El campo es obligatorio';
            $messages['justificacion['.$pregunta->id.'].required_if'] = 'El campo es obligatorio';
        }
        return $messages;
    }

But when I try to validate, the fields 'pregunta' and 'justificacion' not validate. How can I validate this fields to show me the errors?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire