0

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)

3
  • 1
    The laravel documentation states, that you need to use the dot-syntax in ` Validator::make() ` . The bug must be somewhere else. Commented Aug 11, 2017 at 14:36
  • As you can see, i tried dot -syntax, but vaidator cant find it Commented Aug 11, 2017 at 15:15
  • @xcy7e웃 Youre comment helps me to find array_set function thanks Commented Aug 11, 2017 at 15:38

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

As i wrote - The rewriting keys using underscore doesn't accept(patient_firstName). Sorry but its unacceptable
@ColohaninNik i didn't mean to do it Patient_firstName, i will edit my answer
In save action everything works correctly, $this->validate($request, $this->validationRules()); corectly validate save request. Problem was in show page, where i dont have POST,GET, but i have to show user that some of his data is not valid
@ColohaninNik i see, i'm sorry, i don't get the question. my mistake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.