4

I have a customer request as follows:

<textarea name="intro[en]"></textarea>
<textarea name="intro[fr]"></textarea>
<textarea name="intro[de]"></textarea>

I am validating it with a custom request:

class UpdateProfileRequest extends Request
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
         return [
             'intro.*'           => 'required|max:100'
         ];
    }
}

The validator is not working. I think this is because the .* only works for numbered arrays, rather than associative arrays?

I'm not sure how to go about doing this.

Is there a way to do it with a custom request like this? If so what is the syntax?

Otherwise, what should I do. I already wrote some custom code inside the controller method like this:

    $hasIntro = false;
    $hasBio = false;

    foreach($request->get('intro') as $language => $localIntro)
    {
        if(!empty($request->get('intro')[$language]))
        {
            $hasIntro = true;
        }
    }

    if(!$hasIntro or !$hasBio)
    {
        return redirect()->back()->withErrors('You must enter at least 1 Bio and 1 Intro');
    }

Which I think might be one manual way of going about this. Though I believe withErrors requires a validator, so I'm back to the same problem... Though perhaps there is a way to do this manually?

My ideal solution is to find the associative array syntax, if that indeed exists?

1
  • use foreach loop with rules Commented Jan 17, 2017 at 13:05

3 Answers 3

1

I'm not sure about the right way but my idea is something like this

public function rules($inputs)
{
    $rules = [];
    foreach ($inputs as $key => $val) {
        if ( strpos($key, "intro") === 0 ){
            $rules[$key] = 'required|max:100';
        }
    }
    return $rules;
}
Sign up to request clarification or add additional context in comments.

Comments

1
class UpdateProfileRequest extends Request
    {
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            return true;
        }

        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */
        public function rules()
        {
            return [
                'intro.*'               => 'required|max:100'
            ];
        }


        /**
         * @param Validator $validator
         *
         * @return mixed
         */
        protected function formatErrors(Validator $validator)
        {
            return $validator->errors()->all();
        }
    }

You have below same name so make sure it's different or remove one, change name.

<textarea name="intro[fr]"></textarea>
<textarea name="intro[fr]"></textarea>

1 Comment

This is pretty much what I posted?
1
public function rules()
{
        $rules = [];

        $intro = $this->request->get('intro');

        if (!empty($intro)) {
            foreach ($intro as $index => $doc) {
                $rules[sprintf('intro.%d', $index)] = 'required|max:100';
            }
        }

        return $rules;
    }

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.