2

I want to optimize the code to make it more efficient and scalable.

So I want to merge this part into one:

if($request->input('logintype') == 'register'){
    $validator = Validator::make($request->all(), [
        'option.*' => 'required|integer',
        'quantity.*' => 'required|integer',
        'conditions' => 'required',
        'comission' => 'required',
    ],[
        'option.integer' => 'Debe introducir una opción válida',
        'quantity.required' => 'Introduzca una cantidad a comprar',
        'quantity.integer'  => 'Debe introducir una cantidad válida',
        'quantity.*.max'  => 'Se ha superado el límite máximo de tickets por persona',
        'conditions.required'  => 'Debe aceptar los Términos y Condiciones',
        'comission.required'  => 'Debe seleccionar el método de pago',
    ]);

}
else{
    $validator = Validator::make($request->all(), [
        'option.*' => 'integer',
        'quantity.*' => 'required|integer',
        'comission' => 'required',
    ],[
        'option.integer' => 'Debe introducir una opción válida',
        'quantity.required' => 'Introduzca una cantidad a comprar',
        'quantity.integer'  => 'Debe introducir una cantidad válida',
        'quantity.*.max'  => 'Se ha superado el límite máximo de tickets por persona',
        'comission.required'  => 'Debe seleccionar el método de pago',

    ]);

}

I've check that this is possible with this code:

$validator->sometimes('conditions', 'required', function($request){
   return $request->input('logintype') == 'register';
});

But I'm unsure how to deal with the custom error messages.

1
  • 3
    Extract this to FormRequest, your controller will thank you. You can do all sort of magic there, array merging based on ifs etcetera and it will look dope. Commented May 9, 2018 at 7:48

3 Answers 3

3

You can achieve this by adding the extra comment to the validator.
Just do like this

$validator = Validator::make($request->all(), [
    'option.*' => 'required|integer',
    'quantity.*' => 'required|integer',
    'comission' => 'required',
],[
    'option.integer' => 'Debe introducir una opción válida',
    'quantity.required' => 'Introduzca una cantidad a comprar',
    'quantity.integer'  => 'Debe introducir una cantidad válida',
    'quantity.*.max'  => 'Se ha superado el límite máximo de tickets por persona',
    'conditions.required'  => 'Debe aceptar los Términos y Condiciones',
    'comission.required'  => 'Debe seleccionar el método de pago',
]);
$validator->sometimes('conditions', 'required', function($request){
    return $request->input('logintype') == 'register';
});

It will validated your input fields and give your define error message check thisenter image description here

And you can also add multiple field by array

$validator->sometimes(['conditions','option'], 'required', function($request){
    return $request->input('logintype') == 'register';
});

Check this https://laravel.com/docs/5.5/validation#conditionally-adding-rules

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

1 Comment

your solution it's the quickest, easiest and shortest one
1

As Kyslik said, adding this logic to a Request would make things a little neater in your controller.

I tend to go with the following style of a Request containing the validation rules and custom validation messages. In your case it could look something like this:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Validator;

class LoginRequest extends FormRequest
{
  /**
   * Determine if the user is authorized to make this request.
   *
   * @return bool
   */
  public function authorize()
  {
    return true;
  }

  /**
   * Get the validation rules that apply to the request.
   *
   * @return array
   */
  public function rules()
  {
    if ($this->input('logintype') == 'register') {
      return [
        'option.*' => 'required|integer',
        'quantity.*' => 'required|integer',
        'conditions' => 'required',
        'comission' => 'required',
      ];
    }

    return [
      'option.*' => 'integer',
      'quantity.*' => 'required|integer',
      'comission' => 'required',
    ];
  }

  public function messages()
  {
    if ($this->input('logintype') == 'register') {
      return [
        'option.integer' => 'Debe introducir una opción válida',
        'quantity.required' => 'Introduzca una cantidad a comprar',
        'quantity.integer'  => 'Debe introducir una cantidad válida',
        'quantity.*.max'  => 'Se ha superado el límite máximo de tickets por persona',
        'conditions.required'  => 'Debe aceptar los Términos y Condiciones',
        'comission.required'  => 'Debe seleccionar el método de pago',
      ];
    }

    return [
      'option.integer' => 'Debe introducir una opción válida',
      'quantity.required' => 'Introduzca una cantidad a comprar',
      'quantity.integer'  => 'Debe introducir una cantidad válida',
      'quantity.*.max'  => 'Se ha superado el límite máximo de tickets por persona',
      'comission.required'  => 'Debe seleccionar el método de pago',
    ];
  }

  public function validate()
  {
    return Validator::make(parent::all(), $this->rules(), $this->messages());
  }
}

And in the controller use like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Requests\LoginRequest;

class LoginController extends Controller
{
  public function search(LoginRequest $request)
  {
    $status_code = 200;
    $response = [];

    try {
      $validator = $request->validate();
      if ($validator->fails()) {
        // throw new ValidationException or something similar
      }

    } catch (Exception $e) {
      // Deal with it
    } finally {
      return response()->json($response, $status_code);
    }
  }
}

As you can see it makes the controller tidier.

In your LoginRequest you can customise it heavily, change rules depending on some of the inputs or the HTTP method used, e.g. different between POST and GET, etc.

Hope this helps.

Comments

0

You may use required_if to conditionally add the required rule. In this scenario required_if rule is added to the fields option and conditions.

FormRequest is used for separating validation logic from controller.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class YourRequestClassName extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function messages()
    {
        return [
                'option.integer' => 'Debe introducir una opción válida',
                'quantity.required' => 'Introduzca una cantidad a comprar',
                'quantity.integer'  => 'Debe introducir una cantidad válida',
                'quantity.*.max'  => 'Se ha superado el límite máximo de tickets por persona',
                'conditions.required_if'  => 'Debe aceptar los Términos y Condiciones',
                'comission.required'  => 'Debe seleccionar el método de pago',
            ];
    }

    public function rules()
    {

        return [
            'option.*'      =>  [
                                    'required_if:logintype,register',
                                    'nullable',
                                    'integer',
                                ],
            'quantity.*'    =>  [
                                    'required',
                                    'nullable',
                                    'integer',
                                ],
            'conditions'    =>  [
                                    'required_if:logintype,register',
                                    'nullable',
                                ],
            'comission'     =>  [
                                    'required',
                                ],
        ];
    }
}

In your controller, you can inject the App\Http\Requests\YourRequestClassName to the method.

use App\Http\Requests\YourRequestClassName;

public function registerAction(YourRequestClassName $request)
    {
        //rest of the controller code

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.