Laravel 4.2自定义验证器,用于组合required_if或required_without

时间:2016-12-02 21:21:28

标签: php validation required laravel-4.2

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

<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">

我想根据textbox-field上选择的值验证checkbox-fieldcolors。问题是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_without?

1 个答案:

答案 0 :(得分:0)

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

/**
 * 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将新类添加到类加载器。

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