Home > Software design >  laravael 9 Validation Segment with some condition
laravael 9 Validation Segment with some condition

Time:10-04

I am working on the online application. it has several sections. some of the sections are shown in some conditions only. some of the validation rules also apply in the dynamic section .how do i segment the validation rule based on the condition.I try following the way and it does not work for me.

$validatedData = $request->validate([
            'titel_id' => ['required'],
            'initials' => ['required','regex:/^[A-Za-z.] $/'], 
            'name_denoted_by_initials' => 'required',
            'last_name' => 'required|alpha',
            'phone_no' => 'max:12',

            /********************************* */
            

        ],[
            'titel_id.required' => 'the title required',
            'initials.required' => 'the name initials required',
            'initials.regex' => 'initials only can include letters and dots',
            'name_denoted_by_initials.required' => 'the initials denoted name required',
            'last_name.required' => 'the last name required',
            'phone_no.max' => 'you entered phone number invalid',
            

        ]);

        if($vacancy->main_category_id == 46){

        $validatedData = $request->validate([
            'index_no' => 'required',
            'year' => 'required|max:'.(date('Y')),

        ],[
            'index_no.required' => 'O/l Exam number is required',
            'year.required' => 'O/l Exam year is required',

        ]);
     }

this only shows the without condition validation rule only. how do I manage it?

CodePudding user response:

You can use sometimes() which helps to create Complex Conditional Validation

$validator = Validator::make($request->all(), [
    # default validations
]);

$validator->sometimes('index_no', 'required', function ($vacancy) {
    return $vacancy->main_category_id === 46;
});

$validator->sometimes('year', 'required|max:'.(date('Y')), function ($vacancy) {
    return $vacancy->main_category_id === 46;
});

Check how to use Manually Creating Validators


Update

Saw in the old Laravel doc. If both fields use the same validation, you can merge them.

$validator->sometimes(['index_no', 'year'], 'required', function ($vacancy) {
    return $vacancy->main_category_id === 46;
});

Method 02

Create formRequest and validate

php artisan make:request VecancyRequest

In VecancyRequest.php

class VecancyRequest extends FormRequest
{
    /**
     * 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()
    {
        if ($this->get('main_category_id') === 46) {
            $rules['index_no'] = 'required';
            $rules['year'] = 'required|max:' . (date('Y'));
        }
        $rules['titel_id'] = 'required';
        $rules['initials'] = 'required|regex:/^[A-Za-z.] $/';
        # other validations

        return $rules;
    }

    public function messages()
    {
        return [
            'index_no.required' => 'O/l Exam number is required',
            'year.required' => 'O/l Exam year is required',
            'titel_id.required' => 'the title required',
            'initials.required' => 'the name initials required',
            'initials.regex' => 'initials only can include letters and dots',
            'name_denoted_by_initials.required' => 'the initials denoted name required',
            'last_name.required' => 'the last name required',
            'phone_no.max' => 'you entered phone number invalid',
        ];
    }
}

In controller

public function store(VecancyRequest $request)

Remove all validations from the controller. Form Request do the trick

  • Related