Laravel - 相同的验证器在一个控制器方法中工作,但在另一个控制器方法中不起作用

时间:2014-11-27 09:13:17

标签: php forms validation laravel

我有两个相似的表单,一个用于向网站添加新闻,另一个用于编辑新闻:

{{ Form::open(array('action' => 'MyController@verifyAdminAddNews', 'files' => true)) }}

        {{ Form::text('title', Form::old('title'), ['required' => 'required']) }}<br><br>
        {{ Form::textarea('subtitle', Form::old('subtitle'), ['required' => 'required', 'style' => 'height:60px;']) }} <br><br>
        {{ Form::textarea('text', Form::old('text'), ['required' => 'required']) }} <br><br>
        {{ Form::file('image', '') }}

        @if(isset($errormessage))
            <div class="error-message"> {{ $errormessage }} </div>
        @endif

        {{ Form::submit('Pridať novinku', ['class' => 'form-control']) }}
{{ Form::close() }}

{{ Form::open(array('action' => 'MyController@verifyAdminEditNews', 'class'=>'bg-grey width')) }}
        {{ Form::text('title',$item->title, ['required' => 'required']) }}<br><br>
        {{ Form::textarea('subtitle', $item->subtitle, ['required' => 'required', 'style' => 'height:60px;']) }} 
        {{ Form::textarea('text', $item->text, ['required' => 'required']) }} <br><br>
        {{ Form::file('image', '') }}

        @if(isset($errormessage))
            <div class="error-message"> {{ $errormessage }}</div>
        @endif

        {{ Form::submit('Upraviť novinku', ['class' => 'form-control']) }}
{{ Form::close() }}

和在Controller中有两种添加和编辑新闻的方法:

public function verifyAdminAddNews(){ 

    if (is_object(DB::table('news')->orderBy('id', 'DESC')->first())) {
        $newid = DB::table('news')->orderBy('id', 'DESC')->first()->id + 1; }
    else { $newid = 0; }

    // validate if file is image
    $input = array('image' => Input::file('image'));
    $rules = array('image' => 'image');
    $validator = Validator::make($input, $rules);

    if ($validator->fails()) {
        Input::flash();
        return View::make('adminnadd', ['errormessage' => 'Chyba! Vybratý súbor nie je obrázok.'] );

    } else {  

        if (Input::file('image')==null) { 

            DB::insert('INSERT INTO news (id, title, subtitle, text, imageurl) VALUES (?, ?, ?, ?, ?)', 
                    array($newid, Input::get('title'), Input::get('subtitle'), Input::get('text'), 'none'));

        } else {

            $destination = 'uploadedimages';
            $filename = 'image'.$newid;

            Input::file('image')->move($destination, $filename);

            DB::insert('INSERT INTO news (id, title, subtitle, text, imageurl) VALUES (?, ?, ?, ?, ?)', 
                    array($newid, Input::get('title'), Input::get('subtitle'), Input::get('text'), $filename));

        }

        return View::make('adminnall',['items'=>DB::table('news')->get()]);
    }           
}

public function verifyAdminEditNews() {

    $id = Session::get('editnewsid');

    // validate if file is image
    $input = array('image' => Input::file('image'));
    $rules = array('image' => 'image');
    $validator = Validator::make($input, $rules);

    if ($validator->fails()) {
        Input::flash();
        return View::make('adminnedit', ['errormessage' => 'Chyba! Vybratý súbor nie je obrázok.'] );

    } else {  

        if (Input::file('image')==null) { 
            DB::table('news')->where('id', $id)->update(array('title' => Input::get('title'), 'subtitle' => Input::get('subtitle'), 'text'=>Input::get('text')));

        } else {

            $destination = 'uploadedimages';
            $filename = 'image'.$id;

            Input::file('image')->move($destination, $filename);
            DB::table('news')->where('id', $id)->update(array('title' => Input::get('title'), 'subtitle' => Input::get('subtitle'), 'text'=>Input::get('text'), 'imageurl' => $filename ));
        }

        return View::make('adminnall',['items'=>DB::table('news')->get()]);
    }   
}

这些方法具有完全相同的Validator,用于验证所选文件是否为image。在verifyAdminAddNews中,它工作正常,当文件不是图像时,将错误消息返回到带有表单的视图。但是在verifyAdminEditNews验证器中,当文件不是图像时,验证器不会失败,并且不会返回错误消息。这怎么可能?

1 个答案:

答案 0 :(得分:1)

哦,我忘了添加'files'=&gt;对于第二种形式'真实'。