dounielong7728 2017-01-18 05:36
浏览 68

laravel仅在存在另一个值时才会生效

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 lammps拉伸应力应变曲线分析
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试,帮帮忙吧
    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建