jeudi 26 octobre 2017

Merge 2 rules FormFequest for validate an update and store action in laravel5.5

I have 2 FormRequest Class (ReadersFormRequest, SocialMediaFormRequest) and i wana store and update a Reader how reader have 0 or many social media account so it's necessarily to validate the request

RedersFormRequest

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',
        ];
    }
}

SocialMediaFormRequest

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

So i wana merge the ScialMediaFormRequest rules in RedersFormRequest rules

I found a solution:

  • let SocialMediaFormRequest rules a static methode and merge it in SocialMediaFormRequest rules

SocialMediaFormRequest

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

ReadersFormRequet

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',
        ]);
    }

I think the merge is correctly do but in update controller when i call this ReadersFormRequest, I don't know whats happen

 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));
    }

When i PUT the reader form this controller not called.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2i5oPty
via IFTTT

Aucun commentaire:

Enregistrer un commentaire