【问题标题】:Laravel 4.2 custom validator to combine required_if OR required_withoutLaravel 4.2 自定义验证器结合 required_if 或 required_without
【发布时间】:2017-04-17 21:12:18
【问题描述】:

让我描述一下我的场景。这是一个类似于我的表格:

<select name="colors">
  <option value="1">Dark red</option>
  <option value="2">Light red</option>
  <option value="3">Dark green</option>
  <option value="4">Light green</option>
</select>
<input type="text" name="textbox-field">
<input type="checkbox" name="checkbox-field" value="1">

我想根据在 colors 上选择的值来验证 textbox-fieldcheckbox-field。问题是textbox-fieldcheckbox-field 是互斥的,这意味着选择了指定的colors 值后,只需填写其中一个即可。

如果我有一个字段,我可以像这样使用内置的 required_if 规则:

'textbox-field' => "string|required_if:colors,3,4"

但是,我想要实现的是这样的想法,但在最后两个规则之间有一个 OR 运算符:

'textbox-field'  => "string|required_if:colors,3,4|required_without:checkbox-field"
'checkbox-field' => "numeric|required_if:colors,3,4|required_without:textbox-field"

是否可以在不使用 OR 逻辑运算符的情况下创建将 required_if 和 required_ 结合起来的自定义验证规则?

【问题讨论】:

  • 我知道规则与 and 结合,所以您可以编写自己的验证规则。
  • 确实如此,有关如何为这种情况实施自定义验证的任何帮助?是否可以在自定义规则中使用内置验证规则?
  • 只是从上面回答我自己的问题,是的,可以在新的自定义验证器中使用内置规则,只要它在扩展 Illuminate\Validation\Validator 的类中实现。这样它就可以访问实现所有内置规则的受保护函数,如validateRequiredIf()validateRequiredWithout。查看下面的代码以获取特定示例。

标签: php validation required laravel-4.2


【解决方案1】:

通过实现以下自定义验证器,我能够解决这个问题:

/**
 * Class CustomValidator
 * Extends the Illuminate\Validation\Validator class instead of using Validator::extend,
 * it requires access to protected properties of the Validator parent class
 *
 * Note: This needs to be registered in app/start/global.php
 */
class CustomValidator extends Illuminate\Validation\Validator
{

/**
 * The validation rules that imply the field is required.
 *
 * @var array
 */
protected $implicitRules = array(
    'Required',
    'RequiredWith',
    'RequiredWithAll',
    'RequiredWithout',
    'RequiredWithoutAll',
    'RequiredIf',
    'Accepted',
    'RequiredIfValueSelectedOrWithoutField'
);
/**
 * Validate mutually exclusive fields when specific options have been selected in select input
 * This uses the built-in rules and apply them using the OR logical operator
 *
 * @param $attribute
 * @param $value
 * @param $parameters
 *
 * @return bool
 */
public function validateRequiredIfValueSelectedOrWithoutField($attribute, $value, $parameters) {

    // use the required_if bult-in rule to check if the required options are selected in select input
    $valueSelectedParams = array_merge(['my_select_input'],Model::getArrayWithTheSpecifiedOptions());
    $isValidForSelect = $this->validateRequiredIf($attribute, $value, $valueSelectedParams);

    // use the required_without to check if the mutually exclusive field in parameters[0] has a value
    $isValidForMutualExclusiveField = $this->validateRequiredWithout($attribute, $value, [$parameters[0]]);

    // returns the result by applying OR between the required_if and the required_without rules
    return ($isValidForSelect || $isValidForMutualExclusiveField);
}

/**
 * Replace the :other placeholder with the select input localized literal to display correctly in error messages
 *
 * @param $message
 *
 * @return mixed
 */
protected function replaceDangerousEkaOrField($message)
{
    return str_replace(':other', Lang::get('model.my_select_input'), $message);
}

我把它放在app/helpers/CustomValidator.php 并在我的app/start/global.php 末尾注册:

/*
|--------------------------------------------------------------------------
| Register Custom Validator extending Illuminate\Validation\Validator
|--------------------------------------------------------------------------
*/

Validator::resolver(function($translator, $data, $rules, $messages)
{
    return new CustomValidator($translator, $data, $rules, $messages);
});

还需要运行composer dump-autoload 才能将新类添加到类加载器。

我希望这对将来处于类似位置的任何人有所帮助。干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 2022-01-19
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 2021-12-09
    相关资源
    最近更新 更多