有没有办法从请求类返回请求字段的值,而不是在laravel 5中检查验证 [英] Is there any way to return the value of request field from request class instead of checking validations in laravel 5

查看:81
本文介绍了有没有办法从请求类返回请求字段的值,而不是在laravel 5中检查验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel5.如果任何字段的验证失败,我想从我创建的请求类中获取特定字段的值,它可以像显示错误消息一样显示在视图类中.有谁知道该怎么编码?

I am using laravel 5. If the validation of any field fails, I want to get the value of a particular field from the request class which I have created and it can be displayed in the view class like displaying error messages. Does anyone knows how to code for that?

上图,对于id部分,如何使语法返回值?

above photo, for the id part how to make the syntax to return the value?

控制器:

public function edit(Requests\EventRequest1 $request){

    $date=$_POST['eventDate'];
    $title=$_POST['title'];
    $id=$_POST['id'];
    $events=EventCal::findOrFail($id);
    $events->update($request->all());
    DB::table('event_cals')
                ->where('id',$id)
                ->update(['title' => $title,'eventDate' => $date]);
    return redirect('/dcalendar');

}

型号:

class EventCal extends Model {

   protected $fillable = [
     'title',
     'eventDate',
   ];

}

查看:

@if($errors->has('title') )
    <td><ul class="alert alert-danger" style="width: 250px;height: 40px"> {{$id}}</ul></td>
@endif

@if($errors->has('eventDate'))
   <td><ul class="alert alert-danger" style="width: 250px;height: 40px"> {{$errors->first('eventDate')}}</ul></td>
@endif

EventRequest1(请求类别):

public function rules()
{


    return [
        'title' => 'required',
        'eventDate' => 'required|date|after:yesterday',
        'id' => Request::get('id')
    ];
}

public function messages(){
    return [
        'title.required' => 'Title is required.',
        'eventDate.after' => 'Event Date is passed.',
        'eventDate.required' => 'Event Date is required.',

    ];
}

我想返回查看页面的ID.在视图页面{{$ id}}中应显示id值.有什么办法吗?我不确定如何从请求中返回id的值.那是我唯一需要知道的事情.

I want to return the id for view page. In the view page {{$id}} should print the id value.Is there any way? I'm not sure how to return the value of id from request. That's the only thing I needed to know.

推荐答案

在请求类中,您必须覆盖response()函数:

Inside of your request class you must override the response() function:

public function response(array $errors)
{
       return $this->redirector->back()
       ->withInput($this->except($this->dontFlash))
       ->withErrors($errors)
       ->with('id', $this->get('id'));
}

这篇关于有没有办法从请求类返回请求字段的值,而不是在laravel 5中检查验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆