Example #1
0
 /**
  * Checks if the input request is doable
  *
  * @param array $fields
  * @param       $moduleId
  *
  * @return true|MessageBag
  */
 private function validateNewFields(array $fields = array(), $moduleId)
 {
     $customBag = new MessageBag();
     // Main bag
     $validatorBags = array();
     // Bags of different validators
     $takenColumnNames = array();
     // Column names that already exist
     // Check if column name already exists
     $tmpField = new Field();
     $rules = array_merge($tmpField->getRules(), array('column_name' => 'unique:fields,column_name,NULL,id,module_id,' . $moduleId));
     // Go through all fields and validate them
     foreach ($fields as $field) {
         $photonField = new Field($field);
         $formField = FieldFactory::make($photonField);
         $formField->setRow($field);
         $validated = $formField->validate();
         if ($validated instanceof MessageBag) {
             $validatorBags[] = $validated;
         }
         /* @var $validator \Illuminate\Validation\Validator */
         $validator = \Validator::make($field, $rules);
         if ($validator->fails()) {
             $validatorBags[] = $validator->getMessageBag();
         }
         if (in_array($field['column_name'], $takenColumnNames)) {
             $customBag->add('new_field', 'Column name `' . $field['column_name'] . '` cannot be assigned to multiple fields');
         }
         $takenColumnNames[] = $field['column_name'];
     }
     // Merge all bags
     /* @var $bag MessageBag */
     foreach ($validatorBags as $bag) {
         $customBag->merge($bag->getMessages());
     }
     // Return the bag if there are no errors, otherwise return true
     return $customBag->isEmpty() ? true : $customBag;
 }