2

I am new to learning Laravel and still trying to learn beautiful coding.

I have a code like this that validates the input fields upon submitting the update form and it has many unnecessary codes that can be modified to make it short and better.

So my question is, what is an approach or technique should I take to rewrite my codes to make it short and beautiful.

public function update(Request $request, $id)
    {
        $product = Product::find($id);

        if ($product->name != $request->name AND $product->sku != $request->sku AND $product->description != $request->description) {
            $this->validate($request, [
                'name' => 'required|unique:products,name',
                'sku' => 'required|unique:products,sku',
                'description' => 'required'
            ]);
        } elseif ($product->name != $request->name AND $product->sku != $request->sku) {
            $this->validate($request, [
                'name' => 'required|unique:products,name',
                'sku' => 'required|unique:products,sku'
            ]);
        } elseif ($product->name != $request->name AND $product->description != $request->description) {
            $this->validate($request, [
                'name' => 'required|unique:products,name',
                'description' => 'required'
            ]);
        } elseif ($product->description != $request->description AND $product->sku != $request->sku) {
            $this->validate($request, [
                'description' => 'required',
                'sku' => 'required|unique:products,sku'
            ]);
        } elseif ($product->name != $request->name) {
            $this->validate($request, [
                'name' => 'required|unique:products,name'
            ]);
        } elseif ($product->description != $request->description) {
            $this->validate($request, [
                'description' => 'required'
            ]);
        } elseif ($product->sku != $request->sku) {
            $this->validate($request, [
                'sku' => 'required|unique:products,sku'
            ]); 
        } else {
            return redirect('products/' . $product->id)->with('info', 'Product does not changed!'); 
        }

    }
2
  • 1
    Use FormRequests instead. Good Luck! Commented Sep 18, 2018 at 9:14
  • I do got it working using laravel form request, but my related problem to that is how can I make the html codes shorter because as you can see, I am using conditional if else that handles the html input conditions. Commented Sep 19, 2018 at 7:17

1 Answer 1

1

Looks like you have two issues that you need to address to shorten your code:

  • 1) The product name and sku fields are required but you want to avoid collisions with the values that exist on the product that you plan to update.
  • 2) The description field isn't actually required but when it is present it must be not be validated. (shouldn't there be a min:1 validation or something?)

So to solve #1 you need to write your own unique rule to ignore the current $product which you are trying to update when determining a unique value. See Forcing A Unique Rule To Ignore A Given ID -- Laravel Docs

And to solve #2 you can use the sometimes validation Conditionally Adding Rules -- Laravel Docs

Combining those should allow you to use a single validation statement like this:

use Illuminate\Validation\Rule;

...

$this->validate($request, [
    'name' =>  [
         'required',
         Rule::unique('products')->ignore($product->id)
    ],
    'sku' => [
         'required',
         Rule::unique('products')->ignore($product->id)
    ],
    'description' => Rule::sometimes('description', 'required', function($input){
        return $input->description !== $product->description;
    });
]);

If this is not correct please, update this answer for the next person who stumbles by. Hope this helps.

[Tip:] It seems like a long shot but if the description field is a WYSIWYG field on the client interface I have had them hit the max length of a text field as they can convert images into base64, so even although it's unlikely for most situations I try to incorporate max:65535 in the validation so that it does gracefully fail. (65535 is the max length of a MySQL text field other databases are different sizes)

Sign up to request clarification or add additional context in comments.

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.