在Laravel

时间:2016-06-23 15:21:37

标签: php json validation laravel-5.2

所以我目前通过以下方式验证表单:

    $this->validate($request, [
        'title'    => 'required',
        'content'  => 'required|min:3'
    ]);

如何处理这些验证错误并通过JSON将其推回? $this->validate(...) Uninstall-Package Microsoft.AspNet.WebPages.OAuth Uninstall-Package DotNetOpenAuth.AspNet Uninstall-Package DotNetOpenAuth.OAuth.Consumer Uninstall-Package DotNetOpenAuth.OAuth.Core Uninstall-Package DotNetOpenAuth.OpenId.RelyingParty Uninstall-Package DotNetOpenAuth.OpenId.Core Uninstall-Package DotNetOpenAuth.core Uninstall-Package Microsoft.AspNet.WebPages.WebData Install-Package Microsoft.AspNet.WebPages.WebData Install-Package DotNetOpenAuth.core Install-Package DotNetOpenAuth.OAuth.Core Install-Package DotNetOpenAuth.OpenId.RelyingParty Install-Package DotNetOpenAuth.OpenId.Core Install-Package DotNetOpenAuth.OAuth.Consumer Install-Package DotNetOpenAuth.AspNet Install-Package Microsoft.AspNet.WebPages.OAuth 状态@FormUrlEncoded @PUT("/api/register") Call<AuthRegisterUserModel> getStatus( @Field("username") String username, @Field("email") String email, @Field("password") String password, @Field("fbID") String fbID, @Field("gmailID") String gmailID, @Field("twitID") String twitID, @Field("gender") String gender, @Field("birthDate") String birthDate, @Field("location") String location, @Field("longitude") String longitude, @Field("latitude") String latitude, @Field("profileImage") String profileImage); 如果为false将重定向并允许您显示错误...

如何验证服务器端的API请求?

1 个答案:

答案 0 :(得分:0)

您需要使用 Validator :: make ,然后在数组或错误的json中返回响应。

$validator = Validator::make(Input::all(), $rules);
    if ($validator->fails())
    {
        return Response::json(['errors' => $validator->getMessageBag()->toArray()], 200);
    }

请注意,您还需要设置 $ rules 变量 例如:

$rules = ['email' => 'required|email'];

希望它会有所帮助。