setRules example


    protected static function validate(string $field, string $value$rules): bool
    {
        $label      = $field;
        $field      = 'temp';
        $validation = Services::validation(null, false);
        $validation->setRules([
            $field => [
                'label' => $label,
                'rules' => $rules,
            ],
        ]);
        $validation->run([$field => $value]);

        if ($validation->hasError($field)) {
            static::error($validation->getError($field));

            return false;
        }
$data = (array) $data;
        }

        $rules = $this->cleanValidationRules ? $this->cleanValidationRules($rules$data) : $rules;

        // If no data existed that needs validation         // our job is done here.         if (empty($rules)) {
            return true;
        }

        $this->validation->reset()->setRules($rules$this->validationMessages);

        return $this->validation->run($data, null, $this->DBGroup);
    }

    /** * Returns the model's defined validation rules so that they * can be used elsewhere, if needed. * * @param array $options Options */
    public function getValidationRules(array $options = []): array
    {


            // If no error message is defined, use the error message in the Config\Validation file             if ($messages) {
                $errorName = $rules . '_errors';
                $messages  = $validation->{$errorName} ?? [];
            }

            $rules = $validation->{$rules};
        }

        $this->validator->setRules($rules$messages);
    }
}

    protected function addTaxDataToSalesChannel(SalesChannelContext $salesChannelContext, array $taxData): void
    {
        $tax = (new TaxEntity())->assign($taxData);
        $this->addTaxEntityToSalesChannel($salesChannelContext$tax);
    }

    protected function addTaxEntityToSalesChannel(SalesChannelContext $salesChannelContext, TaxEntity $taxEntity): void
    {
        if ($taxEntity->getRules() === null) {
            $taxEntity->setRules(new TaxRuleCollection());
        }
        $salesChannelContext->getTaxRules()->add($taxEntity);
    }
}
return true;
                    }
                }

                return false;
            });

            $matchingRules = new TaxRuleCollection();
            $taxRule = $taxRules->highestTypePosition();

            if (!$taxRule) {
                $tax->setRules($matchingRules);

                continue;
            }

            $taxRules = $taxRules->filterByTypePosition($taxRule->getType()->getPosition());
            $taxRule = $taxRules->latestActivationDate();

            if ($taxRule) {
                $matchingRules->add($taxRule);
            }
            $tax->setRules($matchingRules);
        }
$ruleSet = [
            $field => [
                'label' => $label,
                'rules' => $rules,
            ],
        ];

        if ($errors) {
            $ruleSet[$field]['errors'] = $errors;
        }

        $this->setRules(array_merge($this->getRules()$ruleSet)$this->customErrors);

        return $this;
    }

    /** * Stores the rules that should be used to validate the items. * * Rules should be an array formatted like: * [ * 'field' => 'rule1|rule2' * ] * * The $errors array should be formatted like: * [ * 'field' => [ * 'rule1' => 'message1', * 'rule2' => 'message2', * ], * ] * * @param array $errors An array of custom error messages */

        static::assertSame('andContainer', (new AndRule())->getName());
    }

    public function testICanAddRulesAfterwards(): void
    {
        $rule = new AndRule([new TrueRule()]);
        $rule->addRule(new TrueRule());

        static::assertEquals([new TrueRule()new TrueRule()]$rule->getRules());

        $rule->setRules([new FalseRule()]);
        static::assertEquals([new FalseRule()]$rule->getRules());
    }

    public function testConstraintsAreStillTheSame(): void
    {
        static::assertEquals(
            ['rules' => [new ArrayOfType(Rule::class)]],
            (new AndRule())->getConstraints()
        );
    }

    
Home | Imprint | This part of the site doesn't use cookies.