Laravel表单方法PUT无法检查单击了哪个提交

时间:2014-10-10 02:48:45

标签: php forms laravel laravel-4

所以我按照Tut +教程在Laravel中构建表单并使用restfull ...我的当前表单有2个提交按钮,一个用于保存/更新,另一个用于取消/返回上一页。

但是当我尝试dd(Input::all())时;除了我的提交按钮之外它只返回所有输入的数组..所以无论如何都要检查点击了哪个提交按钮以便我可以处理它?<​​/ p>

并且由于Input::all()没有返回提交按钮的数组,所以我无法使用此

进行检查
Input::get('submit_button_name')

这是我的完整表格

@extends('admin._layouts.admin')

@section('content')
{{ Form::model($artikel, array('route' => array('admin.artikels.update',$artikel->id), 'method' => 'put')) }}
    <div class="panel panel-default">
        <!--button-->
        <div class="panel-heading tooltip-demo">
            {{ Form::submit('Simpan',array('class' => 'btn btn-primary', 'data-toggle' => 'tooltip', 
               'data-placement' => 'top','title' => 'Menyimpan artikel' )) }}
            {{ Form::submit('Batal',array('class' => 'btn btn-default', 'data-toggle' => 'tooltip', 
               'data-placement' => 'top','title' => 'Batal menambah artikel dan kembali ke halaman kelola artikel' )) }}
        </div>
        <!--/button-->
        <div class="panel-body">
            <!--judul-->
            <div class="col-lg-10">
            <div class="form-group">
                {{ Form::label('Judul Artikel') }}
                {{ Form::text('judul',null,array('class' => 'form-control', 'placeholder' => 'Silahkan masukkan judul artikel'))}}
                {{ $errors->first('judul', '<p class="error">:message</p>') }}
            </div>
            </div>
            <!--/judul-->
            <!--kategori-->
            <div class="col-lg-4">
            <div class="form-group">
                {{ Form::label('Kategori') }}
                <?php 
                    $tambah = array('tambah' => 'Tambah Kategori Baru');
                    $list = array_merge(KategoriArtikel::lists('name','id'),$tambah); 
                ?>
                {{ Form::select('kategori',$list,'Pilih Kategori Artikel',array('class' => 'form-control', 'onChange' => 'changeFunc(value)')) }}
            </div>
            </div>
            <!--/kategori-->
            <!--kategori baru-->
            <div class="col-lg-4"  id="pilihan" style="display:none;">
            <div class="form-group">
                {{ Form::label('Kategori Baru') }}
                {{ Form::text('kategori_baru',null,array('class' => 'form-control', 'placeholder' => 'Silahkan masukkan kategori baru', 
                   'maxlength' => '30'))}} 
            </div>
            </div>
            <!--/kategori baru-->
            <!--status-->
            <div class="col-lg-4">
            <div class="form-group">
                {{ Form::label('Status') }}
                {{ Form::select('status',array('0' => 'Tidak diterbikan', '1' => 'Terbitkan'),null, array('class' => 'form-control')) }}
            </div>
            </div>
            <!--/status-->
            <!--artikel pilihan-->
            <div class="col-lg-5">
            <div class="form-group">
                {{ Form::label('Artikel Pilihan') }}
                <div class="input-group">
                <span class="input-group-addon">
                {{ Form::checkbox('pilihan','1',true,array('id' => 'artikelpilihan')) }}
                </span>
                {{ Form::text('null','Tidak',array('class' => 'form-control', 'id' => 'artikeltext' ,'disabled' => 'true'))}}
                </div>
            </div>
            </div>
            <!--/artikel pilihan-->
            <!--content-->
            <div class="col-lg-12">
                {{ Form::label('Isi Artikel') }}
                {{ Form::textarea('content',null,array('style' => 'height:300px')) }}
                {{ $errors->first('content', '<p class="error">:message</p>') }}
            </div>
            <!--/content-->
        </div>
     </div>
{{ Form::close() }}
{{ HTML::script('js/tinymce/tinymce.min.js') }}
<script type="text/javascript">
    tinymce.init({
        selector: "textarea",
        theme: "modern",
        skin: 'light',
        plugins: [
            "advlist autolink lists link image charmap print preview hr anchor pagebreak",
            "searchreplace wordcount visualblocks visualchars code fullscreen",
            "insertdatetime media nonbreaking save table contextmenu directionality",
            "emoticons template paste textcolor colorpicker textpattern"
        ],
        toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
        toolbar2: "print preview media | forecolor backcolor emoticons | fontselect fontsizeselect",
        image_advtab: true,
        templates: [
            {title: 'Test template 1', content: 'Test 1'},
            {title: 'Test template 2', content: 'Test 2'}
        ],
        file_browser_callback: RoxyFileBrowser
    });


    function RoxyFileBrowser(field_name, url, type, win) {
      var roxyFileman = '../../../../public/js/tinymce/plugins/fileman/index.html?integration=tinymce4';
      if (roxyFileman.indexOf("?") < 0) {     
        roxyFileman += "?type=" + type;   
      }
      else {
        roxyFileman += "&type=" + type;
      }
      roxyFileman += '&input=' + field_name + '&value=' + document.getElementById(field_name).value;
      tinyMCE.activeEditor.windowManager.open({
         file: roxyFileman,
         title: 'File Manager',
         width: 800, 
         height: 480,
         resizable: "yes",
         plugins: "media",
         inline: "yes",
         close_previous: "no"  
      }, {     window: win,     input: field_name    });
      return false; 
    }
</script>
@stop

这是我的更新控制器

public function update($id)
{
    $artikel = Artikel::findOrFail($id);

    //dd(Input::all());

    $validator = Validator::make($data = Input::all(), Artikel::$rules);

    if ($validator->fails())
    {
        return Redirect::back()->withErrors($validator)->withInput();
    }

    $judul = Input::get('judul');
    $artikel->update($data);

    return Redirect::route('admin.artikels.index')->with('message', 'Artikel ' .$judul. ' Telah berhasil di ubah.');
}

1 个答案:

答案 0 :(得分:0)

提交您的提交按钮名称将解决问题。

<button type="submit" name="simpan" class="btn btn-primary" data-toggle ='tooltip' 
    data-placement='top' title ='Batal menambah artikel dan kembali ke halaman kelola artikel' value="simpan">Simpan</button>

<button type="submit" name="batal" class="btn btn-primary" data-toggle ='tooltip' 
    data-placement='top' title ='Menyimpan artikel' value="simpan">Batal</button>

请注意,您一次只能访问一个提交按钮。如果按下Simpan按钮提交表单,您可以访问“simpan”提交按钮,如下所示:

Input::get('simpan')

对其他提交按钮执行相同的操作。