0

Laravelでは、

プロジェクトでカスタム検証を使用して、最小領域の値が最大領域よりも小さいかどうかを確認しています。バリデーターライブラリで検証を作成しました

public function validate_less_than($attribute, $value, $parameters)
{
            $other_value = $this->attributes[$parameters[0]];
            if(!empty($value) && !empty($other_value))
                return $value <= $other_value;
            else    
                return true;    
}

言語ファイルに「less_than」の検証メッセージを追加しました。

'less_than'     =>  "The :attribute must be less than :other value",

:otherプレースホルダーを置き換えるために、バリデーターライブラリにreplaceplaceholder関数を追加しました

protected function replace_less_than($message, $attribute, $rule, $parameters)
    {
                return str_replace(':other', $parameters[0], $message);
    }

私のフィールド名は「min_area」、「max_area」のようなものなので、検証メッセージにこのフィールド名を含めたくないので、言語ファイルにこれらのフィールドのわかりやすい名前を追加しました。ただし、「:other」プレースホルダーは、検証言語ファイルで指定されているフレンドリ名を使用しません。「:attribute」プレースホルダーでのみ可能ですか?

4

1 に答える 1

1

これは、validator.phpの856行目あたりにハードコーディングされています(現在のバージョン3.2.12)。

あなたはタフなあなたの代替品からそれを実行することができます。

protected function replace_less_than($message, $attribute, $rule, $parameters)
{
    return str_replace(':other', $this->attribute($parameters[0]), $message);
}

に注意してください$this->attribute()

于 2012-12-06T14:05:04.757 に答える