0

From This answer I am trying to update department data. Code as below :

$id = Crypt::decrypt($id); 
        $rules = Department::$rules;

        $rules['name']              = $rules['name'] . ',id,' . $id;
        $rules['department_code']   = $rules['department_code'] . ',id,' . $id;

        dump($rules);

        $validator = Validator::make($data = $request->all(), $rules);
        if ($validator->fails()) return Redirect::back()->withErrors($validator)->withInput();

        $department = Department::findOrFail($id);

But the validator says :

The department code has already been taken.

The name has already been taken.

So whats wrong ?

My rules array is:

public static $rules = [
        'name'              =>  'required|unique:departments|max:255', 
        'department_code'   =>  'required|unique:departments|max:127',
    ];
Community
  • 1
  • 1
Nitish
  • 2,695
  • 9
  • 53
  • 88

1 Answers1

1

Change your $rules array as:

public static $rules = [
        'name'              =>  'required|max:255|unique:departments', 
        'department_code'   =>  'required|max:127|unique:departments',
    ];

Then you can use it to append id in the rules.

Amit Gupta
  • 17,072
  • 4
  • 41
  • 53
  • I have changed rules to `public static $rules = [ 'name' => 'required|max:255|unique:departments', 'department_code' => 'required|max:127|unique:departments', ];` . Now `name` rules works fine but still getting message `The department code has already been taken.` – Nitish Dec 14 '16 at 07:45
  • What is the name of the column for `department_code` in your table? If it is other than `department_code` then you have to provide in the rule as `unique:departments,code`. [Docs](https://laravel.com/docs/5.3/validation#rule-unique) – Amit Gupta Dec 14 '16 at 13:53