vendredi 21 juillet 2017

Laravel append rules on custom validation rule

In request I got attribute that defines validation rules and flow. Let's say it is account_type: business, personal. Each value changes validation flow and requires different attributes to be present ir request.

Let's say I have following custom rules methods:

public function validateAccountTypeBusiness($attribute, $value, $parameters, Validator $validator)
{
   // check is present:
   // Company address
   // VAT number
}

public function validateAccountTypePersonal($attribute, $value, $parameters, Validator $validator)
{
   // check is present:
   // User mobile phone
}

Since each rule requires more than single attribute to be presented in this request depending on account type ( vat number, company name, etc ) returning false would be not informative to user because generated response notices that account type is invalid without any details which actual attributes is missing.

The question is: how I can append more rules to be validated in this validation custom rule?

Following not working:

public function validateAccountTypeBusiness($attribute, $value, $parameters, Validator $validator)
{
    $validator->addRules([
        'company_address'   =>  'required|string',
        'vat_number'        =>  'required',
    ]);
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2vI4NKS
via IFTTT

Aucun commentaire:

Enregistrer un commentaire