該賞金過期4天。此問題的答案有資格獲得 200聲望獎勵。 Elias正在從信譽良好的來源尋找答案。
我有兩個欄位:Email和Telephone
我想創建一個驗證,其中需要兩個欄位之一,如果設定了一個或兩個欄位,它應該是正確的格式。
我試過這個,但它不起作用,雖然我需要兩者
public static array $createValidationRules = [
'email' => 'required_without:telephone|email:rfc',
'telephone' => 'required_without:email|numeric|regex:/^\d{5,15}$/',
];
uj5u.com熱心網友回復:
如果兩個欄位required_without都為空,則兩個欄位都會產生錯誤訊息是正確的。此錯誤訊息清楚地表明,如果其他欄位沒有填寫,則必須填寫該欄位。如果需要,您可以更改訊息:
$messages = [
'email.required_without' => 'foo',
'telephone.required_without' => 'bar',
];
但是,您必須添加nullable規則,因此當欄位為空時格式規則不適用:
$rules = [
'email' => ['required_without:telephone', 'nullable', 'email:rfc'],
'telephone' => ['required_without:email', 'nullable', 'numeric', 'regex:/^\d{5,15}$/'],
];
此外:建議將規則撰寫為陣列,尤其是在使用regex.
uj5u.com熱心網友回復:
你使用了錯誤的規則。您應該使用required_with而不是required_without.
通過檔案:
required_with -檔案
The field under validation must be present and not empty only if any of the other specified fields are present and not empty.
required_without -檔案
The field under validation must be present and not empty only when any of the other specified fields are empty or not present.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/339780.html
