Support the ongoing development of Laravel.io →
Laravel Validation
Last updated by @tvbeek 1 year ago.
0
moderator

Hello @prithvidev1

I think you need to create some custom logic to get it working, I suspect you could start with the Rule:forEach as described here: https://laravel.com/docs/9.x/validation#accessing-nested-array...

sidenote: I have updated your post to make the code block more readable.

prithvidev1 liked this reply

1

Thank you, for coming up with the suggestion. I believe Rule:forEach is not available in previous versions of laravel. I tried to use Rule::when as well which also is absent in my laravel version.

0

@prithvidev1 - How did you fix this issue? I am working on something similar. Please guide us.

0
moderator

@churas232 why do you link in this thread to this thread?

0

To achieve this, you can define a custom validation rule in Laravel and use it in the validation rules for the "correct_answer" field.

Here's an example of how you can define a custom validation rule: use Illuminate\Contracts\Validation\Rule;

class CorrectAnswerRule implements Rule { protected $questionType;

public function __construct($questionType)
{
    $this->questionType = $questionType;
}

public function passes($attribute, $value)
{
    if ($this->questionType == 'Single Answer') {
        // define the regular expression for Single Answer question type
        $regex = '/^[A-D]$/'; // example
        return preg_match($regex, $value);
    }
    // for other question types, validation passes automatically
    return true;
}

public function message()
{
    return 'The :attribute is invalid for the selected question type.';
}

} In this example, we define a custom validation rule named "CorrectAnswerRule". The constructor takes the value of "question_type" as a parameter, which will be used to determine the regular expression for "correct_answer". The "passes" method defines the validation logic based on the question type, and the "message" method returns the error message.

Now you can use this custom rule in your validation rules like this: $validator = Validator::make($rows->toArray(), [ '.question_categories' => 'required', '.question' => 'required', '.question_type' => 'required', '.mark' => 'required', '.correct_answer' => [ new CorrectAnswerRule(request()->input('[].question_type')), 'required_if:.question_type,Single Answer', ], ])->validate(); Here, we use the custom rule instead of a regular expression for the "correct_answer" field. We pass the value of "question_type" as a parameter to the custom rule using "request()->input('[].question_type')", which will be used to determine the validation logic.

I hope this helps! Let me know if you have any further questions.

prithvidev1 liked this reply

1

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.