不输出验证错误,也不返回包含先前Form数据的字段

时间:2017-07-11 14:59:19

标签: validation laravel-5.2

Laravel 5.2

查看其中发送数据的形式('刀片'):

@if( count($errors) > 0 )
        <div class="alert alert-danger">
            <ul>
                @foreach( $errors->all() as $error ) <li>{{ $error }}</li> @endforeach
            </ul>
        </div>
    @endif

    <form method="POST" action="{{  route('contact') }}">  <!-- <?//='/contact');?> Or <?//=route('contact');?> -->
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
        <div class="form-group">
            <label for="name">Name:</label>
            <input type="text" class="form-control" id="name" name="name" value="{{ old('name') }}" placeholder="Enter Name">
        </div>
        <div class="form-group">
            <label for="email">Email address:</label>
            <input type="email" class="form-control" id="email" name="email" value="{{ old('email') }}" placeholder="Enter E-mail">
        </div>
        <div class="form-group">
            <label for="site">Site:</label>
            <input type="text" class="form-control" id="site" name="site" value="{{ old('site') }}" placeholder="Enter Site">
        </div>
        <div class="form-group">
            <label for="text_area">Text:</label>
            <textarea class="form-control" id="text_area" name="text_area" rows="3" placeholder="Some text....."> {{ old('text_area') }} </textarea>
        </div>

        <div class="checkbox">
            <label><input type="checkbox" name="checkbox"> Remember me</label>
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
    </form>

    </div> <!--/class="col-"-->
</div> <!--/class="row"-->

ContactController.php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ContactController extends Controller {


    public function show( Request $request, $prm=false ){

        $my_array = ['title1'=>'This variable `$title1` content', 'title2'=>'This variable `$title2` content', 'title3'=>'This variable `$title3` content']; //массив
        $my_array2 = ['one'=>array('param1'=>'This variable `param1` content', 'param2'=>'This variable `param2` content', 'param3'=>'This variable `param3` content'),
            'two'=>array('param4'=>'This variabl e `param4` content', 'param5'=>'This variable `param5` content', 'param6'=>'This variable `param6` content')
        ];

        $my_array3 = array(
            'title'=>'Contact',
            'data'=>[ 'one'=>'list 1',
                'two'=>'list 2',
                'three'=>'list 3',
                'four'=>'list 4',
                'five'=>'list 5',
            ],
            'dataI'=>['list-1','list-2','list-3','list-4','list-6','list-6'],
            'bvar'=>true,
            'script'=>"<script>alert('Hello! ++')</script>"
        );

        /** VALIDATION on Request */
        if( $request->isMethod('post') ) {

            $rules = [
                'name' => 'required|max:10',
                'email' => 'required|email',
                //'site'=>'required',
                //'text_area'=>'required',
            ];

            $messages = [
                'required' => 'The :attribute field is required.',
            ];

            $this->validate($request, $rules, $messages);
                dump( $request->all() );
                dump( $request->session()->all() );
        }


        if( view()->exists('default.contact') ){
            return view('default.contact')
                ->withMydata($my_array2)
                ->withMydata2($my_array)
                ->withMydata3($my_array3);
        }
        else { abort(404); }

    }

}

/app/Http/Kernel.php

   /**
 * The application's global HTTP middleware stack.
 *
 * These middleware are run during every request to your application.
 *
 * @var array
 */
protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
];

我怎样才能看到验证符合并且其规则生效, 但是,当没有传递时,我没有看到验证错误的显示,并且在重定向时填充表单时输入字段中的数据。

2 个答案:

答案 0 :(得分:0)

让我向您展示我的方法,该方法也使用Laravel 5.2上的验证,并找出您对此代码的不同之处:

处理请求的Controller:

$validator = \Validator::make($request->all(), [
        'data1' => 'required',
        'data2' => 'required|in:bla1,bla2,bla3',
        'data3' => 'required|array',
        'data3.*' => 'required|json',
        'data4' => 'required_if:data2,bla2',
    ]);

    if ($validator->fails()) {
        $request->flash();
        return \Response::make(\View::make('theform')
            ->withErrors($validator)
            ->withInput($request->all())
            ->render()
        , 406);
    }

包含已提交并使用错误日志重新绘制的表单的表单,名为“theform”:

<input type="text" class="form-control" name="trip_name" id="trip_name" 
    placeholder="Gezi ismi" value="{{ old('trip_name') }}">
@if ($errors->has('trip_name'))
    <span class="help-block">
        <strong>{{ $errors->first('trip_name') }}</strong>
    </span>
@endif

这是展示它的一种方式。您也可以按照以下方式查看:

@if( count($errors) > 0 )
    <div class="alert alert-danger">
        <ul>
            @foreach( $errors->all() as $error ) 
                <li>{{ $error }}</li> 
            @endforeach
        </ul>
    </div>
@endif

答案 1 :(得分:0)

嗯,问题并没有就此止步))

我通过自己的类Request创建验证,并为GET和POST划分路由,并在我的Controller中为它们定义了单独的方法。

1。 app / Http / routes.php

Route::get('/contact_form/{prm?}', ['uses'=>'Admin\ContactformController@show_form_get'])->name('contact_form');

Route::post('/contact_form', ['uses'=>'Admin\ContactformController@show_form_post']);

2。 app / Http / Requests / ContactRequest.php - 我的自定义Request类,包含验证规则:

<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;

class ContactRequest extends Request
{

    public function authorize()
    {
        return true;  //false
    }


    public function rules()
    {
        return [
            'name' => 'required|max:10',
            //'name' => 'exists:users,name',
            'email' => 'required|email',
            'site'=>'required',
        ];
    }

}  //__/class ContactRequest

3。 app / Http / Requests / ContactRequest.php - 我的带POST和GET处理的控制器:

<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\ContactRequest; //custom Request class with validation rules
use App\Http\Controllers\Controller;
use \Illuminate\Support\Facades\Validator;

class ContactformController extends Controller {

    public $show_controller_method = array(__METHOD__);


    /** Method handler http-request with GET
    */
    public function show_form_get( ){

        $this->show_controller_method[] = 'showform()';
        if( view()->exists('default.contact_form') ){
            return view('default.contact_form')->withInfoMethodController($this->show_controller_method);
        }
        else { abort(404); }

    }  //__/public function show_form_get()


    /** Method handler http-request with POST
    */
    public function show_form_post( ContactRequest $request ){

        if( $request->isMethod('post') ):
            dump( $request->all() );
        endif;
    }

3。视图保持不变,并且返回数据“旧输入”:

value="{{ old('name') }}"  value="{{ old('email') }}"   and so on...

以及验证错误(如果存在):

@if( count($errors) > 0 )
    <div class="alert alert-danger">
        <ul>
            @foreach( $errors->all() as $error ) <li>{{ $error }}</li> @endforeach
        </ul>
    </div>
@endif
  • 现在验证工作(如果成功通过 - 我看到一个转储()POST。
    • 如果验证失败,则会发生重定向,但没有验证错误且没有旧输入。

请告诉我,我在做什么不对?