【问题标题】:Laravel validation: rules in required_ifLaravel 验证:required_if 中的规则
【发布时间】:2019-03-21 17:58:40
【问题描述】:

我有一个选择列表,其中第一个选项被禁用,这就是当用户没有选择有效选项时,选择列表的结果不会出现在请求中。

在验证中,必填字段,如果其他字段的值为例如:1, in case not 1, the field is not required

代码:

'city_id' => [
'required',
'integer',
Rule::in(City::availableCities()),
],
'district_id' => new DistrictValidation(request('city_id')),

我怎么能做到这一点,district_id 每次都会抛出验证,不管它是否在请求中。

感谢您的回答,

更新: 也许你看清楚了,如果 DistrictValidation 规则在这里:

class DistrictValidation implements Rule
{
    protected $city;
    private $messages;

    /**
     * Create a new rule instance.
     *
     * @param $cityId
     */
    public function __construct($cityId)
    {
        $this->city = City::find($cityId);
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        dd('here');
        if (!$this->city) {
            return false;
        }

        if (!$this->city->hasDistrict) {
            return true;
        }

        $validator = Validator::make([$attribute => $value], [
            $attribute => [
                'required',
                'integer',
                Rule::in(District::availableDistricts()),
            ]
        ]);

        $this->messages = $validator->messages();

        return $validator->passes();
    }
    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
       return optional($this->messages)->first('district_id');
    }
}

【问题讨论】:

  • 您能出示完整的验证码吗?

标签: php laravel


【解决方案1】:

试试这个:

'city_id' => [
  'nullable',
  'numeric',
  Rule::in(City::availableCities())
],
'district_id' => new DistrictValidation(request('city_id')),

【讨论】:

【解决方案2】:

试试这个:

$myValidations = [
    "city_id" => [
        "required",
        "integer"
    ]
]

// if city_id exists in availableCities so add some rules 
if(collect(city::availableCities)->contains(request("city_id"))){
    $myValidations["district_id"] = new DistrictValidation(request('city_id'))
}

// validate request fields with $myValidations variable

【讨论】:

  • 好的,但是如果请求中不存在'district_id',则不会进行验证。
  • @LaszloP 为什么?如果我的意思是对的,如果 city_id 存在于 availableCities 中,您想验证 District_id
  • 没有。我想要什么:例如,如果 city_id = 1,那么 District_id => 'required',如果 city_id = 2,那么 District_id => 'nullable'
【解决方案3】:

尝试使用required_if 验证,如果另一个字段字段等于任何值,则验证中的字段必须存在且不能为空。

required_if:field,value,...

像这样使用它:

$request->validate([ 'city_id' => 'required|integer|Rule::in(City::availableCities())', 'district_id' => 'required_if:city_id,1', ]);

尝试阅读更多laravel验证here

【讨论】:

  • 虽然这可能会回答作者的问题,但它缺少一些解释性文字和文档链接。如果没有围绕它的一些短语,原始代码 sn-ps 并不是很有帮助。您可能还会发现 how to write a good answer 非常有帮助。请编辑您的答案。
【解决方案4】:

你可以使用 laravel 验证中定义的 required_if 条件 这是正确文档的链接Laravel Validation

Validator::make($data, [
    'city_id' => [
        'required',
        'integer',
        Rule::in(City::availableCities()),
    ],
    'district_id'=>[
         'required_with:city_id,',
    ]
]);

【讨论】:

  • 对不起,我不明白这个。如果您考虑过“required_if:city_id == 1”,那没关系,它可以工作,但我不喜欢这样。我想是这样的:'required_id:city_id = new DistrictValidation...但是,当然,它不起作用...
  • "验证规则 required_if 至少需要 2 个参数。"
  • 好的,但是我还不明白,districtValidation 规则是如何运行的?
猜你喜欢
  • 2020-05-25
  • 2017-07-08
  • 1970-01-01
  • 2017-06-13
  • 2021-03-06
  • 2018-11-05
  • 2014-04-15
  • 2016-10-13
  • 1970-01-01
相关资源
最近更新 更多