CRUDlex\EntityValidator::validate PHP Метод

validate() публичный Метод

Validates the entity against the definition.
public validate ( AbstractData $data, integer $expectedVersion ) : array
$data AbstractData the data access instance used for counting things
$expectedVersion integer the version to perform the optimistic locking check on
Результат array an array with the fields "valid" and "errors"; valid provides a quick check whether the given entity passes the validation and errors is an array with all errored fields as keys and arrays as values; this field arrays contains the actual errors on the field: "boolean", "floating", "integer", "dateTime" (for dates and datetime fields), "inSet", "reference", "required", "unique", "value" (only for the version field, set if the optimistic locking failed).
    public function validate(AbstractData $data, $expectedVersion)
    {
        $validator = new Validator();
        $validator->addValidator('unique', new UniqueValidator());
        $validator->addValidator('reference', new ReferenceValidator());
        $validator->addValidator('many', new ManyValidator());
        $rules = $this->buildUpRules($data, $validator);
        $toValidate = $this->buildUpData();
        $rules['version'] = [['value', $expectedVersion]];
        $toValidate['version'] = $this->entity->get('version');
        $validation = $validator->isValid($rules, $toValidate);
        return $validation;
    }

Usage Example

Пример #1
0
 /**
  * Validates and saves the new or updated entity and returns the appropriate HTTP
  * response.
  *
  * @param Application $app
  * the current application
  * @param AbstractData $crudData
  * the data instance of the entity
  * @param Entity $instance
  * the entity
  * @param string $entity
  * the name of the entity
  * @param boolean $edit
  * whether to edit (true) or to create (false) the entity
  *
  * @return Response
  * the HTTP response of this modification
  */
 protected function modifyEntity(Application $app, AbstractData $crudData, Entity $instance, $entity, $edit)
 {
     $fieldErrors = [];
     $mode = $edit ? 'edit' : 'create';
     $request = $app['request_stack']->getCurrentRequest();
     if ($request->getMethod() == 'POST') {
         $instance->populateViaRequest($request);
         $validator = new EntityValidator($instance);
         $validation = $validator->validate($crudData, intval($request->get('version')));
         $fieldErrors = $validation['errors'];
         if (!$validation['valid']) {
             $optimisticLocking = isset($fieldErrors['version']);
             $this->setValidationFailedFlashes($app, $optimisticLocking, $mode);
         } else {
             $modified = $edit ? $crudData->update($instance) : $crudData->create($instance);
             $response = $modified ? $this->modifyFilesAndSetFlashBag($app, $crudData, $instance, $entity, $mode) : false;
             if ($response) {
                 return $response;
             }
             $app['session']->getFlashBag()->add('danger', $app['translator']->trans('crudlex.' . $mode . '.failed'));
         }
     }
     return $app['twig']->render($app['crud']->getTemplate($app, 'template', 'form', $entity), ['crudEntity' => $entity, 'crudData' => $crudData, 'entity' => $instance, 'mode' => $mode, 'fieldErrors' => $fieldErrors, 'layout' => $app['crud']->getTemplate($app, 'layout', $mode, $entity)]);
 }
All Usage Examples Of CRUDlex\EntityValidator::validate