我遇到了這樣的問題。 我想要做的是爲用戶數據的驗證規則製作1個地方。這些數據由患者,地址和其他對象組成。 所以我創建的規則:PHP Laravel驗證數據密鑰名稱
protected function validationRules()
{
return [
'Patient.firstName' => 'required|string|min:2',
'Patient.lastName' => 'required|string',
'Patient.sex' => 'required|string',
'Address.city' => 'required|string',
'Address.states' => 'required|string',
'Address.address1' => 'required|string|min:2',
'Address.zip' => 'required|string|min:2',
'Phone.mobileArea' => 'string|min:3|required_with:Phone.mobilePhone',
'Phone.mobilePhone' => 'string|min:7|required_with:Phone.mobileArea',
'Phone.homePhone' => 'string|min:7|required_with:Phone.homeArea',
'Phone.homeArea' => 'string|min:3|required_with:Phone.homePhone',
];
}
形式I具有輸入,比如
<input id="firstName" type="text" class="form-control" name="Patient[firstName]" value="{{ $user->getFirstName() }}" required autofocus placeholder="First Name">
而且在節省一切正常。 代碼 $this->validate($request, $this->validationRules());
執行驗證非常好。但....
在另外一個地方,當我想表明,一些信息在用戶配置文件丟失,我執行這樣的驗證和它的失敗:
$validator = Validator::make([
'Patient[firstName]' => $user->getFirstName(),
'Patient[lastName]' => $user->getLastName(),
'Patient.lastName' => $user->getLastName(),
'Patient->lastName' => $user->getLastName(),
'Patient.sex' => $user->getSex(),
'Address.city' => $address->getCity(),
'Address.states' => $address->getState(),
'Address.address1' => $address->getStreet1(),
'Address.zip' => $address->getZip(),
'Phone.mobileArea' => $mobilePhone->getArea(),
'Phone.mobilePhone' => $mobilePhone->getNumber(),
'Phone.homePhone' => $homePhone->getArea(),
'Phone.homeArea' => $homePhone->getNumber(),
], $this->validationRules());
正如你所看到的,我試過在數據數組中命名Patient-> lastName鍵的不同變體。但我仍然有錯誤,姓氏是必需的。 當我打印驗證我可以看到這樣的結構:
Validator {#300 ▼
#data: array:12 [▼
"Patient[firstName]" => ""
"Patient[lastName]" => "Colohanin"
"Patient->lastName" => "Colohanin"
"Patient->sex" => "female"
"Address->city" => "GOSHEN2"
"Address->states" => "NY"
"Address->address1" => "Aleco Russo 59/1 a.68"
"Address->zip" => "109242"
"Phone->mobileArea" => "793"
"Phone->mobilePhone" => "906990"
"Phone->homePhone" => "022"
"Phone->homeArea" => "3322278"
]
#initialRules: array:1 [▼
"Patient.lastName" => "required|string"
]
}
據我瞭解,驗證是否有「Patient.lastName」,但在數據陣列Laravel改造這個關鍵對象,並驗證找不到這個關鍵規則在數據包中。在結果,我有錯誤 - >所需的患者姓氏(用於測試目的,我刪除了其他規則)
所以有我的問題。有誰知道,如何在「dot」sintancs中設置數據數組?我應該如何命名數據數組中的「Patient.lastName」(Validator :: make()中的第一個參數)?使用下劃線不接受
重寫鍵(patient_firstName)
的laravel文檔狀態,你需要使用點語法'驗證::使()'。該錯誤必須在其他地方。 –
正如你所看到的,我試過dot -syntax,但vaidator不能找到它 –
@ xcy7e웃你的評論幫助我找到array_set函數感謝 –