0

我有 2 个FormRequest类(ReadersFormRequest, SocialMediaFormRequest),我想存储和更新一个Reader. AReader可能有 0 个或多个社交媒体帐户,因此有必要验证请求。

读者表格请求

class ReadersFormRequest extends FormRequest
{
    public function rules()
    {
        return [
            'first_name'=>'required',
            'last_name'=>'required',
            'birthday'=>'required',
            'region'=>'required',
            'photo_url'=>'required',
            'support'=>'required',
            'riwayas_id'=>'required',
            'description'=>'required',
        ];
    }
}

社交媒体表格请求

 public function rules()
 {
    return [
        'url'=>'required|url',
        'title'=>'required'
    ];
 }

所以我想在SocialMediaFormRequest规则中合并ReadersFormRequest规则

我找到了一个解决方案:

  • 使 SocialMediaFormRequest 规则成为静态方法并将其合并到 SocialMediaFormRequest 规则中

社交媒体表格请求

public static function rules()
{
    return [
        'url'=>'required|url',
        'title'=>'required'
    ];
}

读者表格请求

public function rules()
{
    return array_merge(SocialMediaFormRequest::rules(),[
        'first_name'=>'required',
        'last_name'=>'required',
        'birthday'=>'required',
        'region'=>'required',
        'photo_url'=>'required',
        'support'=>'required',
        'riwayas_id'=>'required',
        'description'=>'required',
    ]);
}

我认为合并已正确完成,但是当我调用此更新控制器时ReadersFormRequest,我不知道会发生什么。

public function update(ReadersFormRequest $request, Readers $reader)
{
    // valid and update reader
    Readers::whereId($reader->id)->update([
        'first_name' => $request->validated()['first_name'],
        'last_name' => $request->validated()['last_name'],
        'photo_url' => $request->validated()['photo_url'],
        'birthday' => $request->validated()['birthday'],
        'region' => $request->validated()['region'],
        'support' => $request->validated()['support'],
        'riwayas_id' => $request->validated()['riwayas_id'],
        'description' => $request->validated()['description']
    ]);
    // For update their social media account links 
    foreach ($request->validated()['url'] as $key => $url)
    {

    }

    return redirect(route('readers.show',$reader));
}

当我将阅读器放入表单时,不会调用此控制器。

4

2 回答 2

2

将您的班级更改为:

class ReadersFormRequest extends FormRequest
{
    public function rules()
    {
        return [
            'first_name'=>'required',
            'last_name'=>'required',
            'birthday'=>'required',
            'region'=>'required',
            'photo_url'=>'required',
            'support'=>'required',
            'riwayas_id'=>'required',
            'description'=>'required',
            'url'=>'required|url',
            'title'=>'required'
        ];
    }
}

或者如果你真的想使用一个类:

class ReadersFormRequest extends FormRequest
{
    public function rules(SocialMediaFormRequest $social)
    { 
        $mediaRules = $social->rules();
        $rules = [
            'first_name'=>'required',
            'last_name'=>'required',
            'birthday'=>'required',
            'region'=>'required',
            'photo_url'=>'required',
            'support'=>'required',
            'riwayas_id'=>'required',
            'description'=>'required',
        ];
        return array_merge($rules,$mediaRules);
    }
}
于 2017-10-26T11:22:35.770 回答
0

这就是我在类似情况下的处理方式:

public static function combineValidations($rules1, $rules2)
{
    if(!is_array($rules2)) return $rules1;

    foreach($rules2 as $key => $item)
    {
        if(!isset($rules1[$key]))
        {
            $rules1[$key] = $item;
        }else
        {
            $rules1[$key] .= '|'.$item;
        }
    }

    return $rules1;
}
于 2020-06-09T05:39:09.343 回答