Laravel 5将现有的验证用于AngularJS

Kuy*_*a A 6 laravel angularjs angularjs-service laravel-validation laravel-5.1

嗨我在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',
    ];
}}
Run Code Online (Sandbox Code Playgroud)

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

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

hhs*_*diq 4

您可以执行以下操作。

  • 制定一条POST验证路线,例如 myapp.com/validateEmail
  • 使用 anuglarJS 的请求将要验证的数据发送到此路由$http。可以说 Email
  • 验证数据并以 JSON 形式返回验证对象。您可以像这样发送 JSON 响应。来自文档的 JSON 响应
  • JSON在 angularjs 控制器的作用域模型中阅读此内容。在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.
    });
    
    Run Code Online (Sandbox Code Playgroud)
  • 根据 中的范围显示适当的错误HTML