Laravel 5.2 Walidator



  • Good afternoon, comrades. Indicate who knows how to add more than one unique field to the validator class in laravel. If my unique fields are allowed to be used in connection with (e.g. series and map number).

     return Validator::make($data, [
                    'serial'     => 'required|min:3|unique:cards,serial',
                    'number'     => 'required|min:8|max:8|regex:/[0-9]{8,8}/',
                    'date_close' => 'required',
                ]);
    

    I've been through this but it doesn't help, it's still making a mistake when the first serialman's arrival, and it doesn't even get to the number check. And the idea is to fight only when both lines match.

    return Validator::make($data, [
                'serial'     => 'required|min:3|unique:cards,serial',
                'number'     => 'required|min:8|max:8|unique:cards,number|regex:/[0-9]{8,8}/',
                'date_close' => 'required',
            ]);
    


  • There's such a regular opportunity:

    public function rules()
    {
        $currentId = $this->route()->parameter('table_name') ?: 'NULL';
        $serial = (int)$this->serial ?: 'NULL';
        return [
            'number' => 'unique:table_name,number,'
                // Для текущей записи проверку не делаем
                . $currentId . ',id,'
                // Уникальность в рамках серии
                . 'serial,' . $serial,
        ];
    }
    

    If for some reason it doesn't work, you can try. https://github.com/felixkiss/uniquewith-validator and rule:

    'serial' => 'unique_with:your_table,cards'
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2