douan3019 2015-12-09 22:24
浏览 26
已采纳

通过php函数生成时不捕获ng-model值

So I have a function that generates honeypot form fields in Laravel.

The issue is when I submit the form with Angular, the javascript object doesn't contain the my_time or my_name fields.

public function generate(array $honey_name, array $honey_time)
{
    $honey_time_encrypted = parent::getEncryptedTime();
    // Encrypt the current time

    $html = '<div id="' . $honey_name['name'] . '_wrap" style="display:none;">' .
                '<input type="text" ' . $this->attributes($honey_name) . ' id="' . $honey_name['name'] . '" />' .
                '<input type="text" ' . $this->attributes($honey_time) . ' value="' . $honey_time_encrypted . '" />' .
            '</div>';

    echo $html;
}

Any thoughts why?

UPDATE:

Blade Template

<form ng-submit="submitForm(formData)">
    <div class="form-group form-group-lg">
        {!! Form::text('name', null, ['class' => 'form-control', 'placeholder' => 'Name', 'ng-model' => 'formData.name']) !!}
        {!! Honeypot::generate(['name' => 'my_name', 'ng-model' => 'formData.my_name'], ['name' => 'my_time', 'ng-model' => 'formData.my_time']) !!}
    </div>
    <div class="form-group form-group-lg">
        {!! Form::email('email', null, ['class' => 'form-control', 'placeholder' => 'Email', 'ng-model' => 'formData.email']) !!}
    </div>
    <div class="form-group form-group-lg">
        {!! Form::text('subject', null, ['class' => 'form-control', 'placeholder' => 'Subject','ng-model' => 'formData.subject']) !!}
    </div>
    <div class="form-group">
        {!! Form::textarea('message', null, ['class' => 'form-control', 'rows' => '5', 'placeholder' => 'Message', 'ng-model' => 'formData.message']) !!}
    </div>
    <div class="form-group">
        {!! Form::submit('Send', ['class' => 'btn btn-default center-block']) !!}
    </div>
</form>

Submitted form data:

$scope.submitForm = function(formData) {
    console.log(formData);
    Contact.submit(formData).then(function(results) {
        SweetAlert.swal({
            title: "Success!",
            text: "Thanks for getting in touch! I usually respond within 12 hours.",
            type: "success",
        });
    });
};


// Output of console.log();
Object {name: "Daniel", email: "abc@123.com", subject: "asdsadsafas fasfaf", message: "asjfasf aalhfsfa flaskfh aklsflakf a"}

So it's missing the my_time and my_name models, which causes validation to fail.

  • 写回答

2条回答 默认 最新

  • doutuoben6908 2015-12-10 13:56
    关注

    Okay, I figured it out. Angular doesn't post empty form fields, so that was part of the problem.

    The second issue had to do with formData.my_time. If you plan to set the input value server side, then you must use ng-init="foo = 'bar'" ng-model="foo".

    So once I added ng-init="formData.my_time = \'' . $honey_time_encrypted . '\'" I was good to go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思