1

So i have a model named Customer. The db the Customer looks like this:

id, name, lastName, personal, address, zip, location, phones, emails updated_at, created_at

Emails and Phones is special rows because they are store as an json object example

['john@doe.com', 'some@othermail.com', 'more@mails.com']

I use the Customer Model to store the validation rules and custom messages like this

<?php
class Customer extends BaseModel
{
    public function validationRules()
    {
        return array(
            'name' => 'required|max:255',
            'lastName' =>'max:255',
            'personal'=> 'integer',
            'location' => 'max:255',
            'address' => 'max:255',
            'zip' => 'required|integer',
            'phones' => 'betweenOrArray:8,10|required_without:emails',
            'emails' => 'emailOrArray'
        );
    }

    public function validationMessages()
    {
            // returns Validation Messages (its too much to write down)
    }
}

The OrArray Rule is found here https://stackoverflow.com/a/18163546/1430587

I call them through my controller like this

public function store()
{
    $customer = new Customer;

    $messages = $customer->validationMessages();
    $rules = $customer->validationRules();

    $input['name'] = Input::get('name');
    $input['lastName'] = Input::get('lastName');
    $input['personal'] = preg_replace("/[^0-9]/", "", Input::get('personal'));
    $input['location'] = Input::get('location');
    $input['address'] = Input::get('address');
    $input['zip'] = Input::get('zip');
    $input['emails'] = Input::get('emails');
    $input['phones'] = Input::get('phones');

    foreach($input['phones'] as $i => $value)
    {
        $input['phones'][$i] = preg_replace("/[^0-9]/", "", $value);
    }


    $validator = Validator::make($input, $rules, $messages);
}

This all works just fine, but I want to be able to PUT/PATCH request to update a single row. But the validationRules has Required on certain fields so when its not present i cant update that single row. Without getting an error that the other fields (witch I'm not posting) is required.

How would I best approach this ?

Community
  • 1
  • 1
oBo
  • 992
  • 2
  • 13
  • 28

2 Answers2

1

You should get that instance of the model that represent the row you want to edit, that's why the resource controller's update method has a parameter that is the resource you want to edit.

public function update($resourceId) {
    $customer = Customer::where('id', '=', $resourceId);
}

Now this customer has all the attributes you set before, so you can access them like:

$customer->name;
$customer->lastName;

So when you valide the values you can use the existing values in your validator where the input is empty:

$input['name'] = (Input::get('name')) ? (Input::get('name')) : $customer->name;

Or a prettier solution with the elvis operator:

$input['name'] = (Input::get('name')) ?: $customer->name;
totymedli
  • 29,531
  • 22
  • 131
  • 165
  • Thats a smart idea. Thanks ill try that one – oBo Feb 18 '14 at 09:13
  • The problem with this is that it just looks awful, and I'm not really updating one single row. It works though. – oBo Feb 18 '14 at 09:22
  • @oBo Resource controller's edit should change only one instance of the model that the controller represents. If you want to edit more, create a new function and pass those rows that you want to edit then you can validate each row in a loop. – totymedli Feb 18 '14 at 09:28
0

I came up with another solution to this problem that works very well and its much cleaner.

$customer = Customer::find($id);
$input = Input::except('_method', '_token');

$customer->fill($input);
oBo
  • 992
  • 2
  • 13
  • 28