0
votes

I am trying to upload multiple files and everything is working except i cant figure out how to rename validation attribute of array request

Now i am getting (x can be any number as user can pick any number of files to upload )

The documents.x must be a file of type: png, gif, jpeg, txt, pdf, doc.

I would like to get (x starts with 0 so it has to be x + 1 )

The document x must be a file of type: png, gif, jpeg, txt, pdf, doc.

My Request

class CreatePersonRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }
    public function rules()
    {

        $rules = [ 'name' => 'required|unique:people' ];

        $docs = $this->file( 'documents' );

        if ( !empty( $docs ) )
        {

            foreach ( $docs as $key => $doc )
            {
                 $rules += ['documents.'.$key => 'mimes:png,gif,jpeg,txt,pdf,doc'] ;

            }
        } 

        return $rules;
    }

    public function attributes(){ //this isn't working

    return [
        'documents.*' => 'document/s', 
    ];
    }
}

I read that i have to do this in attributes but i cant figure out how. Any help is appricated

1

1 Answers

0
votes

I have made it work, while it might not be the best solution but its working for me

class CreatePersonRequest extends FormRequest
{

private $docNumber;

private function setNumber($number) {

$this->docNumber = $number;

}


public function authorize()
{
    return true;
}

public function rules()
{

    $i = 0;

    $rules = [ 'name' => 'required|unique:people' ];

    $docs = $this->file( 'documents' );

    if ( !empty( $docs ) )
    {

        foreach ( $docs as $key => $doc )
        {
             $rules += ['documents.'.$key => 'mimes:png,gif,jpeg,txt,pdf,doc'] ;
            $i++;


        }
    } 

    $this->setNumber($i);

    return $rules;
}

public function attributes(){

 $i = $this->docNumber;

    $attr = [];   

    for ($y = 0; $y < $i; $y++){

         $attr += ['documents.'.$y=> "document " . ($y + 1) ];

    }

      return $attr;

}
}