4
$rules = [
            'user_id'           => 'required|exists:users,id',
            'preparat_id'       => 'required|exists:preparats,id',
            'zoom'              => 'required|numeric',
            'comment'           => '',
            'type'              => 'in:' . Annotation::ANN_RECTANGLE . ',' . Annotation::ANN_CIRCLE . ',' . Annotation::ANN_POLYGON . ',' . Annotation::ANN_PIN,
            'point_x'           => 'array|required|numeric',
            'point_y'           => 'array|required|numeric',
        ];

        $this->validate($request, $rules);

point_x and point_y is an array input.

My rule is :

point_x and point_y must be exist.

How I send data :

  • point_x[0] = 123;
  • point_y[0] = 123;

TRUE


  • point_x[0] = 123;
  • point_y[0] = 123;
  • point_x[1] = 123;
  • point_y[1] = 123;
  • point_x[2] = 123;
  • point_y[2] = 123;

TRUE


point_x[0] = 123; point_y[0] = "SO";

WRONG


point_y[0] = 123;

WRONG


  • point_x[0] = 123;
  • point_y[0] = 123;
  • point_x[1] = 123;
  • point_y[1] = "Taadaa";
  • point_x[2] = 123;
  • point_y[2] = 123;

WRONG

My Laravel version is 5.4

How should I write a rule for check like above. I tried to array parameter, but it does not work.

1

2 Answers 2

2

Try following rules for arrays point_x and point_y only,

$this->validate($request, [
    'point_x' => 'required',
    'point_y' => 'required',
    'point_x.*' => 'numeric',
    'point_y.*' => 'numeric',
], 
     $messages = [

     ]
);

See docs Array Input Validation

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

Comments

0
  public function rules($array)
    {
      $rules = [  
            'user_id'           => 'required|exists:users,id',
            'preparat_id'       => 'required|exists:preparats,id',
            'zoom'              => 'required|numeric',
            'comment'           => '',
            'type'              => 'in:' . Annotation::ANN_RECTANGLE . ',' . Annotation::ANN_CIRCLE . ',' . Annotation::ANN_POLYGON . ',' . Annotation::ANN_PIN,];

      foreach($array as $key => $val)
      {
        $rules[$key] = 'required|numeric';
      }
      return $rules;
    }

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.