【问题标题】:Laravel 5 to use existing validation to AngularJSLaravel 5 使用现有的 AngularJS 验证
【发布时间】:2015-10-20 10:45:01
【问题描述】:

您好,我在 laravel 5 现有验证中遇到问题。

在我的控制器和请求中。

class PostEmailRequest extends Request{
/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'email' => 'required|max:255',
    ];
}}

它将返回电子邮件的验证作为示例。我想用我的 angularjs 做的是我想捕捉验证并在我的前端表单上获取它。它不会重新加载页面。你对这个有什么想法吗?

我的应用程序中的表单将通过 angularjs 发送数据。 $http post url "app/postPage" 如果上面有错误,我的代码将 $validation->messages() 将发送到我的 angularjs。

【问题讨论】:

  • catch and fetch 是什么意思?问题不是很清楚。请更具体地说明您到底想要做什么。

标签: angularjs laravel angularjs-service laravel-5.1 laravel-validation


【解决方案1】:

您可以执行以下操作。

  • 制作一个POST 路由进行验证,比如说myapp.com/validateEmail
  • 使用 anuglarJS 的$http 请求将要验证的数据发送到此路由。让我们说 Email
  • 验证数据并将验证对象作为 JSON 返回。您可以像这样发送 JSON 响应。 JSON Responses from docs
  • 在 angularjs 控制器的范围模型中阅读此 JSON。在以下代码的success()中。

    $http.post('/someUrl', {msg:'hello word!'}).
      success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
     }).
    error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
    });
    
  • 根据HTML 中的范围显示适当的错误。

【讨论】:

  • 这是我的问题,我将如何将验证数据发送到我的 angularjs 代码。
猜你喜欢
  • 2015-04-25
  • 1970-01-01
  • 2017-09-06
  • 1970-01-01
  • 1970-01-01
  • 2018-07-30
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
相关资源
最近更新 更多