0

I have a html form. Here is a fragment:

<div class="form-group">
    <label for="answer_text_1">Текст ответа 1</label>
    <textarea class="form-control" id="answer_text_1" rows="3"
              name="answer_text[0]"></textarea>
</div>

<div class="form-group">
    <label for="answer_text_2">Текст ответа 2</label>
    <textarea class="form-control" id="answer_text_2" rows="3"
              name="answer_text[1]"></textarea>
</div>

<div class="form-group">
    <label for="answer_text_3">Текст ответа 3</label>
    <textarea class="form-control" id="answer_text_3" rows="3"
              name="answer_text[2]"></textarea>
</div>

As you can see I try to give an array in answer_text. Next I am trying to validate it with the help of Laravel Request and set my own error messages. Here is code of the Request

class CreateQuestionRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'question' => 'required|string',
            'answer_text.0' => 'required|string',
            'answer_text.1' => 'required|string',
            'answer_text.2' => 'required|string',
            'answer_text.*' => 'distinct',
            'answer' => 'required',
            'cost' => 'required|integer',
            'rating' => 'required|integer',
            'duration' => 'required|integer',
        ];
    }

    public function messages()
    {
        return [
            'question.required' => 'Текст вопроса не установлен.',
            'answer_text.distinct' => 'У вас есть одинаковые варианты ответа.',
            'answer_text.0' => 'Не указан первый вариант ответа.',
            'answer_text.1' => 'Не указан второй вариант ответа.',
            'answer_text.2' => 'Не указан третий вариант ответа.',
            'cost.required' => 'Не указана цена вопроса.',
            'rating.required' => 'Не указана сложность вопроса.',
            'duration.required' => 'Не указано количество времени, данное пользователю на ответ',
            'cost.integer' => 'Цена вопроса должна быть числом',
            'rating.integer' => 'Сложность вопроса должна быть числом',
            'duration.integer' => 'Время на ответ должно быть задано числом',
        ];
    }
}

But if I the fields answer_text are empty I see follownig:

enter image description here

This is a default Laravel errors. But I would like to see my messages from

 public function messages()
{
    return [
       // ...
        'answer_text.0' => 'Не указан первый вариант ответа.',
        'answer_text.1' => 'Не указан второй вариант ответа.',
        'answer_text.2' => 'Не указан третий вариант ответа.',
        // ...
    ];
}
1

1 Answer 1

1

check this for custom error messages

Custom error messages in laravel

You need this answer_text.0 when you displaying error.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, but where I need it? I already have it)) See the last code listing.
try with this 'answer_text.*' => 'required|string' and for displaying error in view use @if ($errors->has('name')) <span class="text-danger" role="alert"> <strong>{{ $errors->first('name') }}</strong> </span> @endif
Ohhh, sorry. I just forget specify rule for messages array. It was 'answer_text.2' => 'error message', but need 'answer_text.2.required' => 'error message',. And it works nice) Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.