【问题标题】:laravel vue getting info by hidden fieldlaravel vue 通过隐藏字段获取信息
【发布时间】:2018-08-10 14:05:38
【问题描述】:

我需要将记录的用户 ID 传递给后端,并且我有 vuex 存储,所以我可以获得像 {{currentUser.id}} 这样的用户信息问题是我无法将它传递给后端它给了我验证错误 user_id当我的表单中有这个隐藏的输入时是必需的

<input type="hidden" name="user_id" :value="currentUser.id">

对于普通输入,我有 v-model 之类的 v-model="project.title",它不能用于隐藏字段。

这里的问题是如何将我的user_id 传递到后端?

代码

<script>
import validate from 'validate.js';

    export default {
        data: function () {
            return {
                project: {
                    title: '',
                    body: '',
                    attachment: '',
                    projectclass: '',
                    deadline: '',
                    user_id: '',
                    csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
                },
                errors: null
            }
        },
        computed: {
            currentUser() {
                return this.$store.getters.currentUser;
            }
        },
        methods: {
            add() {
                this.errors = null;
                const errors = validate(this.$data.project);
                if(errors) {
                    this.errors = errors;
                    return;
                }
                axios.post('/api/projects/new', this.$data.project)
                .then((response) => {
                    this.$router.push('/projects');
                });
            }
        }
    }
</script>

【问题讨论】:

    标签: laravel vue.js


    【解决方案1】:

    发生这种情况是因为 this.$data.project 中的 user_id 没有得到更新。

    您可以这样做,而不是隐藏输入

    add() {
       this.errors = null;
       const errors = validate(Object.assign(this.$data.project, {user_id: this.currentUser.id}));
       if(errors) {
          this.errors = errors;
          return;
       }
       axios.post('/api/projects/new', Object.assign(this.$data.project, {user_id: this.currentUser.id}))
         .then((response) => {
             this.$router.push('/projects');
          });
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      • 2016-08-07
      • 2010-10-20
      • 2011-03-12
      • 2011-12-14
      相关资源
      最近更新 更多