lundi 26 août 2019

Laravel/PHP: How to Validate Rendered Fields?

I have three forms, and one submit button. I only render two of the forms at any given point based on some predicate but when I submit the form, I would like the validate only the fields that exist in the currently rendered forms

Here is what the validation rules look

public function rules($request) {
    return [
        'name' => 'required|max:255',
        'firm' => 'required|max:255',
        'contactnumber' => 'required|numeric',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:'.Spark::minimumPasswordLength(),
        'vat_id' => 'nullable|max:50|vat_id',
        'terms' => 'required|accepted',
        'accountHolderEmail' => 'required|email|max:255',
        'accountHolder' => 'required|max:255',
        'cardNumber' => 'required|numeric|digits:16',
        'cvc' => 'required|numeric|digits:3',
        'expiry_month' => array('required', 'regex:/0[1-9]|1[0-2]/'),
        'expiry_year' => 'required|numeric|digits:4|date_format:Y|after:'. date('Y', strtotime('-1 years'))

    ];
}

I would like to validate the last six fields only when I have rendered the form that contains those fields, but I just cannot seem to get it right.

I have tried adding sometimes but that just makes the fields completely optional and that is not the desired behavior because when the form requiring those fields is rendered, we actually need the fields to be mandatory.

I have also tried using Validator::make and passing those last six fields there and that does not do it as well.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire