Home > Software design >  Laravel 9 validation - distinct not working
Laravel 9 validation - distinct not working

Time:07-28

Hello i'm trying to use the distinct rule in validation to avoid duplicated values of the array. I didn't really get how it works by the documentation. My validation is not working.

Basically i need to check the values of 'codice_articolo' that mustn't be duplicated

    $row = 9;  
    while ($worksheet->getCell("A$row")->getValue() !== NULL) {
        $newEntry = [];    
        $newEntry['codice_articolo'] = $worksheet->getCell("A$row")->getValue();  
        $newEntry['quantita'] = $worksheet->getCell("B$row")->getValue();       
        $newEntry['prezzo'] = $worksheet->getCell("C$row")->getValue();
        $data_detail[] = $newEntry;  
        $row  ;
    }
    dump($newEntry);
    dd($data_detail);

enter image description here

        $validator_detail = Validator::make($newEntry, [
        'codice_articolo.*' => 'required|distinct|max:25',
        'quantita' => 'required|max:10',
        'prezzo' => 'required|numeric',
        
    ], [
        'max' => 'Massimo :max caratteri permessi per ":Attribute"',
        'required' => ':Attribute è obbligatorio!',
        'distinct' => ':Attribute non deve essere duplicato!',
        'numeric' => 'Il prezzo deve essere in formato numerico'
    ]);

CodePudding user response:

You have used array in request. so for array validation use like this :

$validator = Validator::make($newEntry, [
        '*.codice_articolo' => 'required|distinct|max:25',
        '*.quantita' => 'required|max:10|numeric',
        '*.prezzo' => 'required|numeric',

    ], [
        'max' => 'Massimo :max caratteri permessi per ":Attribute"',
        'required' => ':Attribute è obbligatorio!',
        'distinct' => ':Attribute non deve essere duplicato!',
        'numeric' => 'Il prezzo deve essere in formato numerico'
    ]);

may it helps.

CodePudding user response:

I don't see any nested attributes of codice_articolo. Try changing 'codice_articolo.*' to just 'codice_articolo' in your validator.

CodePudding user response:

try 'codice_articolo'

instead of 'codice_articolo.*'

//or use unique:xxxx

  • Related