Performance: Conditional validation
Conditional validation affects server-side request processing speed and user experience by controlling when validation rules run.
use Illuminate\Validation\Rule;
public function rules() {
return [
'email' => 'required|email',
'phone' => Rule::requiredIf($this->input('contact_method') === 'phone'),
'address' => 'required',
];
}public function rules() {
return [
'email' => 'required|email',
'phone' => 'required',
'address' => 'required',
];
}| Pattern | Validation Checks | Server CPU Time | User Experience | Verdict |
|---|---|---|---|---|
| Unconditional validation | All fields always checked | High | Slower response | [X] Bad |
| Conditional validation | Only relevant fields checked | Lower | Faster response | [OK] Good |
'phone' => 'required_if:contact_method,phone|digits:10'
contact_method is set to email and phone is empty?'age' => 'required_if:status,active|integer|min:18'
status is inactive. What is the likely problem?password field is required only if the user_type is 'admin'. Also, if password is present, it must be at least 8 characters. Which validation rule set correctly implements this?Pass the test and this pattern moves unknown