Problems with textarea

0

You see, I have this code:

<div class="form-group row">
    <label for="texto" class="col-md-4 col-form-label text-md-right">Comentario</label>

    <div class="col-md-6">
        <textarea id="texto" name="texto" placeholder="Escribe aquí tu comentario" required>{{old('texto')}}</textarea>

        @if ($errors->has('texto'))
            <span class="invalid-feedback">
                <strong>{{ $errors->first('texto') }}</strong>
            </span>
        @endif
    </div>
</div>

This is a fragment of a form in which I allow a user to write a comment in a news item. The conditions in the Request are in this code:

public function messages(){
    return[
        'texto.required'=>'No puedes dejar este campo vacío',
        'texto.min'=>'Tu comentario debe tener como mínimo 10 caracteres'
    ];
}

public function rules(){
    return[
        'texto'=>'required|min:10'
    ];
}

In other types of form boxes if I try to create the new row without complying with the rules, it returns me to the form with the boxes marked in red and with the error message, but in the case of textarea the error message neither the red outline appears. Is there a solution?

More information: This is the complete form:

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-25">
            <div class="card">
                <div class="card-header">Publicar un comentario</div>

                <div class="card-body">
                    <form method="POST" action="{{route('comentario.publicar',$articulo)}}" enctype="multipart/form-data">
                        @csrf

                        <input type="hidden" name="articulo_id" value="{{ $articulo->id }}"/>

                        <input type="hidden" name="user_id" value="{{ auth()->user()->id }}"/>

                        <div class="form-group row">
                            <label for="texto" class="col-md-4 col-form-label text-md-right">Comentario</label>

                            <div class="col-md-6">
                                <textarea id="texto" name="texto" placeholder="Escribe aquí tu comentario" required>{{old('texto')}}</textarea>

                                @if ($errors->has('texto'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('texto') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    Publicar comentario
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

This is the function with which I enter the comment:

public function escribir(ComentarioRequest $request, Articulo $articulo){
    $comentario=Comentario::create($request->all());
    return back()->with('message',['success','Su comentario esta publicado.']);
}

And this is CommentRequest:

class ComentarioRequest extends FormRequest{
    public function authorize(){
        return true;
    }

    public function messages(){
        return[
            'texto.required'=>'No puedes dejar este campo vacío',
            'texto.min'=>'Tu comentario debe tener como mínimo 5 caracteres'
        ];
    }

    public function rules(){
        return[
            'texto'=>'required|min:5'
        ];
    }
}

How should I modify the code then?

    
asked by Miguel Alparez 01.12.2018 в 21:57
source

1 answer

1

The textarea needs the form-control class and the form tag needs the needs-validation class and, once validated, add was-validated. With that it should work.

I also recommend that you do not send the user id through a field in the form, as it is easily modified. Neither the article since you have access to it in the controller through the url.

<div class="container">
<div class="row justify-content-center">
    <div class="col-md-25">
        <div class="card">
            <div class="card-header">Publicar un comentario</div>

            <div class="card-body">
                <form class="needs-validation{!! $errors->any() ? ' was-validated' : '' !!}" method="POST" action="{{route('comentario.publicar',$articulo)}}" enctype="multipart/form-data">
                    @csrf

                    <div class="form-group row">
                        <label for="texto" class="col-md-4 col-form-label text-md-right">Comentario</label>

                        <div class="col-md-6">
                            <textarea id="texto" class="form-control" name="texto" placeholder="Escribe aquí tu comentario" required>{{old('texto')}}</textarea>

                            @if ($errors->has('texto'))
                                <span class="invalid-feedback">
                                    <strong>{{ $errors->first('texto') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group row mb-0">
                        <div class="col-md-6 offset-md-4">
                            <button type="submit" class="btn btn-primary">
                                Publicar comentario
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

class ComentarioRequest extends FormRequest{
    public function authorize(){
        return auth()->check();
    }

    public function messages(){
        return[
            'texto.required'=>'No puedes dejar este campo vacío',
            'texto.min'=>'Tu comentario debe tener como mínimo 5 caracteres'
        ];
    }

    public function rules(){
        return[
            'texto'=>'required|min:5'
        ];
    }
}

public function escribir(ComentarioRequest $request, Articulo $articulo){
    $comentario=Comentario::create([
        'id_usuario' => auth()->id(),
        'id_articulo' => $articulo->id,
        'texto' => $request->get('texto'),
    ]);
    return back()->with('message',['success','Su comentario esta publicado.']);
}
    
answered by 02.12.2018 в 11:45