在Springboot(2.x.x)应用程序中,在使用REST Controller时,需要处理两种类型的故障
自定义验证失败,这里我可以根据我的需要(即我需要的任何字段)生成任何Json响应结构。
Springboot的默认验证失败(下面给出的示例很少,还有更多)。在这里,它生成5xx或4xx,默认为json响应中的几个字段。如何在应用层覆盖这一功能并与我的自定义失败JSON响应相匹配?
MissingServletRequestPartException.class, //it will be thrown when one of form
我知道在使用自定义身份验证提供程序时可以返回自定义响应,如下所示:
从ServiceStack身份验证返回自定义身份验证响应对象
我只是想知道是否有一种方法可以返回自定义的HTTP响应代码。
例如,当身份验证失败时,我希望发送另一个HTTP响应代码,以提供有关失败的更多详细信息,而不是401未授权错误。例如,当帐户被锁定时,我将发送错误代码XYZ!
public class MyBasicAuthProvider : BasicAuthProvider
{
public override object Authenticate(ServiceStack.ServiceInterface
如果验证在FormRequest中失败,我需要添加自定义标记作为响应。
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreMessage extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(
我正在构建一个API,我想用表单请求验证输入字段。
我不知道如何将消息和规则作为json在控制器中的存储方法返回
我希望当字段是重复的,或者是空的,被验证。
当返回$message和$rules json时,return response()->json($message)给出错误并说:TypeErrors : Arguments 2 passed , must be of the type array , object given
我检查了这个链接,但它并没有帮助
我的StoreCategoryRequest:
public function rules()
{
我在Laravel5.4中测试一个API。我现在正在通过API存储记录的一部分。在如何使用请求::Input()或Input::get()验证POST数据方面,我遇到了一些问题。
LessonsController.php
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Lesson;
use App\Acme\Transformers\LessonTransformer;
//use Illuminate\Http\Request;
us
是否可以在我的函数中创建动态FormRequest验证?参见下面的示例代码。
public function store(Request $request)
{
Model::create($request->all());
return redirect(url('/'));
}
我的意思是将“请求”参数更改为变量$formRequest。
我的目标是为单个模型的动态数据集创建不同的验证规则。
如果我能用其他方法做到这一点,请告诉我。谢谢!
编辑:
示例场景:我有一个表单,其字段为“名”、“名”和“姓”。
第一条规则:
public function r
我在试着用laravel来验证表格。我的表单中有一个输入文本字段,名为“类别”,字段名为“cat”,简称为“cat”。
我定义了这样的验证规则。
public static $rules=array(
"name"=>"required|min:3",
"cat"=>"required"
);
当验证失败时,我会收到如下错误消息
The name field is required.
The cat field is required.
但我想把它显示为“类别
当验证失败时,我想显示一条闪存消息。目前验证工作,但闪存消息没有。我怀疑我的条件是错误的
public function update_avatar(Request $request,$username){
$this->validate($request,[
'avatar' => 'mimes:jpeg,bmp,png|required'
]);
if($request['avatar']==null){
\Session::flash('message','Please cho
我是Struts2的新手,尝试在表单中使用带有<s:select>标记的addFieldError。我有一个表单,我必须从下拉列表中选择一个值。第一次显示此表单时,响应来自Action类,我在其中创建了一个列表。在我表单中,我使用<s:select>标签在下拉列表中打印该列表,如下所示:
<s:form action="clientselect">
<h2>Select Client to add the case :</h2>
<table>
<s:select list=