2016-12-14 11 views
0

Laravel 5.3とVue.jsを使用して簡単なフォーム検証をしようとしています。Vue.jsでデータが設定されていません

Laravelコントローラ:

public function test(\Illuminate\Http\Request $request) 
    { 
     $this->validate($request, [ 
      'name' => 'required', 
      'date' => 'required' 
     ]); 
... 
    } 

Vueのデータ:

let app = new Vue({ 
     el: '#app', 

data: { 
     submitted: false, 
     errors: [], 
     post: { 
       name: '', 
       date: '', 
       }, 
}, 

Vueのポスト:

Vue.http.post('url', post).then((response) => { 
    // form submission successful, reset post data and set submitted to true 
    app.post = { 
     name: '', 
     date: '', 
    }; 

    // clear previous form errors 
    app.$set('errors', ''); 

    app.submitted = true; 

}, (response) => { 
    // form submission failed, pass form errors to errors array 
    console.log(response.data.name); //"The Name field is required." 
    app.$set('errors', response.data); // TypeError 
}); 

私は

TypeError: can't assign to properties of (new String("errors")): not an object

を取得しています

app.$set('errors', response.data);

どこが間違っていますか?

+0

「Vue.http.post」をトリガーするメソッドはどのように見えますか? 'Vue.http'はグローバルな使い方のためのもので、Vueのインスタンスを参照する' this。$ http.post'がありますので、正しい使用事例について考えてみてください。この約束は私にはちょっと変わって見えます。レスポンスは? –

+0

@BelminBedak、 'this。$ http.post'は同じ結果を返します。 – suncoastkid

+0

さて、約束はどうですか?約束が満たされたときには '.then()'のシナリオがあり、問題があるときは '.catch()'が2つあります。パラメータ応答を持つ第2の方法は何ですか? –

答えて

3

チェックvm.$set(object, key, value)、あなたはキーとしてresponse.dataを使用してに文字列「エラーを」プロパティを追加しようとしています。

正しい構文はapp.$set(app, 'errors', response.data)ですが、app.errors = response.dataも同様に機能するはずです。

+0

ああ!私はもともと、オブジェクトが必要でないところで 'this'を使っていました:' this。$ set( 'errors'、response.data) '明らかにしてくれてありがとう。 – suncoastkid

+0

['this。$ set'と' Vue.set'](https://github.com/vuejs/vue/blob/dev/src/core/observer/index.js#L191)(どちらも同じですオブジェクト)は、ターゲットオブジェクトが最初のパラメータとして渡されることを要求します。 –

+0

あなたは正しいと思われます。それはv0.12のように見えます。obj。$ set(key、value)http:///012.vuejs.org/guide/best-practices.html#Adding_and_Deleting_Properties – suncoastkid

関連する問題