4

Below is my solution for using the one form validation request for the create/store and edit/update methods on a controller (Laravel 5).

It is a cumbersome and ugly hack (i.e. it reads the URI segments to get the id of the record being edited). Is there a better way to determine if the request is receiving information from a edit/update request? (without directly passing the $id and a flag to the request)

/**
 * Sets the basic validation rules that apply to the request.
 *
 * @return array
 */
protected $rules = [    
        'trading_name' => 'required|min:3|max:50|unique:companies',
        'legal_name' => 'required|min:3|max:50|unique:companies',
        'legal_identifier' => 'required|min:3|max:50|unique:companies',
        'status' => 'required',
        'website' => 'active_url',
        'type' => 'required',
        'payment_terms' => 'required|integer|min:0|max:180',
        'credit_limit' => 'required|integer|min:0|max:500000',
        'notes' => 'max:2000',
];

/**
 * Sets addition validation rules depending on the request type (i.e. edit & update).
 * 
 * 
 * @return array
 */
public function rules()
{
    $rules = $this->rules;
    if ($this->is('companies/*') == true) #Add a ignore self-unique rule as this is a edit&update request
    {
        $id = $this->segment(2);
        $rules['trading_name'] = $rules['trading_name'].',trading_name,'.$id;
        $rules['legal_name'] = $rules['legal_name'].',legal_name,'.$id;
        $rules['legal_identifier'] = $rules['legal_identifier'].',legal_identifier,'.$id;
    }
    return $rules;
}
Matthew Malone
  • 463
  • 1
  • 9
  • 26
  • Actually you can directly call the name of the segment. What is your route for this controller look like? I will show you how to call it. – mininoz Apr 04 '15 at 08:20
  • The controller is a laravel "resource" i.e. Route::resource('companies', 'CompaniesController'); @mininoz – Matthew Malone Apr 04 '15 at 10:30

2 Answers2

3

If you are following the correct restful routing technqiues - you could use the request type instead - providing a generic solution for any validation

public function rules()
{
    if($request()->isMethod('put'))
    {
         // Update rules here
    }

    return $this->rules;
}
Laurence
  • 58,936
  • 21
  • 171
  • 212
  • 1
    Awesome! Using $this->isMethod('patch') solves the first problem - any ideas on how to get the $id? (from somewhere other than the URL segment) – Matthew Malone Apr 04 '15 at 10:56
  • I *think* you can just do `$request()->id` - because it is bound in the route? – Laurence Apr 04 '15 at 11:14
  • I just read your comment above. Because it is a resource controller - it might be `$request()->companiesId` or `$this->companiesId` – Laurence Apr 04 '15 at 11:17
  • I have already tried all of the above and they return null - the controller is not passing the id to the request? – Matthew Malone Apr 04 '15 at 11:36
2

Ok, I have solved my own question with google; there is a good resource about the topic here https://laracasts.com/discuss/channels/requests/laravel-5-validation-request-how-to-handle-validation-on-update

Any of the following will work (replaces the segment id line from the above original code)

  • $id = $this->route()->parameters()["companies"];
  • $id = $this->route()->getParameter('companies');
  • $id = $this->route('companies');
  • $id = $this->companies
Matthew Malone
  • 463
  • 1
  • 9
  • 26