How to check if an array has an attribute?

Asked

Viewed 599 times

1

I’m using Laravel 5.4 in an application, and a validator method receives a array.

That one array can have the attributes:

[
    'nome' => $request->nome,
    'ddd'  => $request->ddd,
    'fone' => $request->fone
]

Or just:

[
    'nome' => $request->nome
]

How do I check if it comes with the attributes ddd and fone, since if it doesn’t come with these attributes it comes back Undefined index.

obs. I know I can create a new method and validate them separately, but I want to use only one method.

Function for validation:

private function validaComTelefone($data)
{
    $regras = [
        'nome' => 'required',
        'ddd' => 'required|min:2|max:2',
        'fone' => 'required|min:8|max:9'
    ];

    $mensagens = [
        'nome.required' => 'Campo nome é obrigatório',
        'ddd.required' => 'Campo ddd é obrigatório',
        'ddd.min' => 'Campo ddd deve ter 2 dígitos',
        'ddd.max' => 'Campo ddd deve ter 2 dígitos',
        'fone.required' => 'Campo telefone é obrigatório',
        'fone.min' => 'Campo telefone deve ter ao menos 8 dígitos',
        'fone.max' => 'Campo telefone deve ter no máximo 9 dígitos'
    ];

    return Validator::make($data, $regras, $mensagens);
}
  • If you are putting "required", the indexes are mandatory, if they do not exist, the Validator passes the values to Messagebag, it should not be your validation that is cussing the Undefined Index, can you put the full log (line and sample number) or even the fragment that is using its function in the question? and to check if there is you can use the array_key_exists

  • is that if ddd then ddd and headset are required, I believe I will have to change my logic. So I need to know how to check if there is such an attribute in the array

  • Ah, the Laravel has the right filters for this, like the required_with, I’ll give you an organized response.

  • @Pliavi solved with array_key_exists, I will put an answer with it, but I await your solution :)

1 answer

2


After chat chat, it was verified that the case was to force the user to have the phone if the DDD was informed.

For this, it is possible to use the validation rule required_with which makes a field mandatory if anyone is informed in the rule be present.

As in the example using the question author’s own code:

$regras = [
    'nome' => 'required',
    'ddd' => 'required_with:fone|min:2|max:2',
    'fone' => 'required_with:ddd|min:8|max:9'
];

What is being checked now?

The name remains mandatory, but headset only passes be mandatory if ddd be informed and vice versa.

In response to the question title, to verify the existence of an index within an array it is possible to use the PHP method array_key_exists:

If the index exists, returns TRUE

$valores = [
    'nome' => $request->nome,
    'ddd'  => $request->ddd,
    'fone' => $request->fone
]

echo array_key_exists('ddd', $valores) // True

Otherwise, FALSE

$valores = [
    'nome' => $request->nome,
]

echo array_key_exists('ddd', $valores) // False
  • Haha, had already solved with the array_key_exists(), but his answer was more complete

  • @Felipepaetzold O array_key_exists breaks a branch, but is already there in case you have more forward a large form, where you have a greater interdependence of the fields, because it starts to suck the stop :v because there are several interesting rules for validation that may be useful to you in the future

  • 1

    It is, actually I am developing this application only to assemble a course, normally I would do more elaborate this validation, and also validate with javascript, however it is a very simple course, for beginners in the framework ^^.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.