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;
}
2
  • 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. Commented Apr 4, 2015 at 8:20
  • The controller is a laravel "resource" i.e. Route::resource('companies', 'CompaniesController'); @mininoz Commented Apr 4, 2015 at 10:30

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Awesome! Using $this->isMethod('patch') solves the first problem - any ideas on how to get the $id? (from somewhere other than the URL segment)
I think you can just do $request()->id - because it is bound in the route?
I just read your comment above. Because it is a resource controller - it might be $request()->companiesId or $this->companiesId
I have already tried all of the above and they return null - the controller is not passing the id to the request?
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.