我有一个HTML表单。这是一个片段:
<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>
如你所见,我尝试在answer_text
中给出一个数组。接下来我试图在Laravel 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' => 'Время на ответ должно быть задано числом',
];
}
}
但是,如果我的字段qazxsw poi是空的,我看到如下:
answer_text
这是Laravel的默认错误。但是我希望看到我的消息
检查这个以获取自定义错误消息
public function messages()
{
return [
// ...
'answer_text.0' => 'Не указан первый вариант ответа.',
'answer_text.1' => 'Не указан второй вариант ответа.',
'answer_text.2' => 'Не указан третий вариант ответа.',
// ...
];
}
当您显示错误时,您需要此Custom error messages in laravel。