I got such problem.
What I want to do – is to make 1 place for validation rules for a user data. These data consists of Patient, Address and other objects.
So I created rules:
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',
];
}
In form i have inputs like
<input id="firstName" type="text" class="form-control" name="Patient[firstName]" value="{{ $user->getFirstName() }}" required autofocus placeholder="First Name">
And on save everything works correctly.
The code
$this->validate($request, $this->validationRules());
Performs validation very well. BUT….
On another place, when I want to show that some information is missing in the user profile, I perform such validation and its failed:
$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());
As you can see, i tried different variations of naming Patient->lastName key in data array. But i still have error that last name is required.
When i print validator i can see such structure:
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"
]
}
As I understand, the validator has rules for “Patient.lastName” but in data array Laravel transform this key to object and Validator can’t find this key in data bag. In result, I have error – > patient last name required(for testing purposes, I removed other rules)
So there is my question. Does anyone know, how to set data array in “dot” sintancs? How should i name “Patient.lastName” in data array(first parameter in Validator::make())?
The rewriting keys using underscore doesn’t accept(patient_firstName)
try this:
$this->validate($request->get('Patient'), $this->validationRules());
form input:
all name changes it to :
<input id="firstName" type="text" class="form-control" name="Patient[firstName]" value="{{ $user->getFirstName() }}" required autofocus placeholder="First Name">
<input id="mobileArea" type="text" class="form-control" name="Patient[mobileArea]" value="{{ $user->mobileArea() }}" required autofocus placeholder="mobileArea"
>
and change all validationrules using:
protected function validationRules()
{
return [
'Patient.firstName' => 'required|string|min:2',
'Patient.lastName' => 'required|string',
'Patient.sex' => 'required|string',
'Patient.city' => 'required|string',
'Patient.states' => 'required|string',
'Patient.address1' => 'required|string|min:2',
'Patient.zip' => 'required|string|min:2',
'Patient.mobileArea' => 'string|min:3|required_with:Patient.mobilePhone',
'Patient.mobilePhone' => 'string|min:7|required_with:Patient.mobileArea',
'Patient.homePhone' => 'string|min:7|required_with:Patient.homeArea',
'Patient.homeArea' => 'string|min:3|required_with:Patient.homePhone',
];
}
Hope it help
Suddenly i found that laravel helper have array_set helper for such arrays.
So in result:
$data = [
'Patient[firstName]' => $user->getFirstName(),
'Patient[lastName]' => $user->getLastName(),
'Patient["lastName"]' => $user->getLastName(),
'Patient.lastName' => $user->getLastName(),
'Patient->lastName' => $user->getLastName(),
'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(),
];
Fails validation, but if you add elements throw array_set helper it helps
array_set
array_set($data, 'Patient.lastName' , $user->getLastName());
After that, validation by lastName is no longer fails.
Hope it helps somebody