I want to validate a field only if another field is present.
This is my current implementation that can illustrate what I want to do. I need to perform unique validation on campaign_lead_id only if campaign_id is present. Other wise, there will be query exception thrown because $params['campaign_id']
is empty.
$params = [
'campaign_id' => '',
'campaign_lead_id' => '23'
];
$customCampaignLeadRule = (empty($params['campaign_id'])
? ''
: '|unique:leads,campaign_lead_id,null,id,campaign_id,' . $params['campaign_id']
);
$rules = [
'campaign_id' => 'required',
'campaign_lead_id' => 'bail|required' . $customCampaignLeadRule,
];
$validator = $this->validator->make($params, $rules);
P.S. I am aware of bail
and required_if
. What I need is not required_if
because I always require campaign_lead_id
not only when other field is required. My implementation works for me but I think it is not the most correct way.
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2izFeJA
via IFTTT
Aucun commentaire:
Enregistrer un commentaire