2

I have an array that looks like this:

array:2 [
  0 => "text/csv"
  1 => "text/plain"
]

I want to use this array in my validation rule like this:

return [
    'file' => 'mimetypes:' . $array,
];

But this is not working, do I need to encode it to a string or something?

1

3 Answers 3

6

You want to implode the array. It should look like this:

return [
    'file' => 'mimetypes:' . implode(',', $array),
];

Imploding will take all of the values of the array and glue them together using the first argument, in this case ,, giving you a string of values from the array separated by the , character.

Sign up to request clarification or add additional context in comments.

Comments

2

Use this

return [
    'file' => 'mimetypes:' . implode(',', $array)
];

Comments

1

You can this kind of Simple code :

 $file_rules = ["text/csv","text/plain"];

    $rules = [
        'username' => 'required',
        'city'      =>  'required',
        'profile_image' => 'mimes:$file_rules' // otherwise  'mimes:'.implode(',', $file_rules)
    ];
    $messages = [
        'username'    => 'The :attribute shoud be Required.',
        'city'    => 'The :attribute should be required.',
        'profile_image' => 'The :attribute should be Required.',
    ];

    $validator = Validator::make($request->all(), $rules, $messages);

it's working try this one.....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.