Create a rule.

@param string $field
@param string $rules

@return string
497440
protected function createRules($field, $rules)
    {
        $rule = str_replace(['(', ')', ';'], [':', '', ','], implode('|', $rules));

        if ($this->scaffold) {
            $rule = 'required';
        }

        return "\t\t\t'{$field}' => '".$rule."',".PHP_EOL;
    }
744890
function(field, rule) {
            var
              $field       = module.get.field(field.identifier),
              type         = rule.type,
              value        = $field.val(),
              isValid      = true,
              ancillary    = module.get.ancillaryValue(rule),
              ruleName     = module.get.ruleName(rule),
              ruleFunction = settings.rules[ruleName]
            ;
            if( !$.isFunction(ruleFunction) ) {
              module.error(error.noRule, ruleName);
              return;
            }
            // cast to string avoiding encoding special values
            value = (value === undefined || value === '' || value === null)
              ? ''
              : $.trim(value + '')
            ;
            return ruleFunction.call($field, value, ancillary);
          }
1159886
public function offsetSet($field, $rules)
    {
        if (!$rules instanceof ValidationSet) {
            $set = new ValidationSet();
            foreach ((array)$rules as $name => $rule) {
                $set->add($name, $rule);
            }
        }
        $this->_fields[$field] = $rules;
    }
513981
public function addRules(array $rules)
    {
        foreach ($rules as $character => $replacement) {
            $this->addRule($character, $replacement);
        }

        return $this;
    }
833782
public function rule($field, $rules = [])
    {
        $defaults = [
            'message' => null,
            'required' => true,
            'skipEmpty' => false,
            'format' => 'any',
            'not' => false,
            'on' => null
        ];

        $rules = $rules ? (array) $rules : [];

        foreach ($rules as $name => $options) {
            if (is_numeric($name)) {
                $name = $options;
                $options = [];
            }
            if (is_string($options)) {
                $options = ['message' => $options];
            }
            $options += $defaults;
            $this->_rules[$field][$name] = $options;
        }
    }
42995
public static Rule getRule(final String field, final String pattern) {
    try {
        return new LikeRule(field, Pattern.compile(pattern, Pattern.CASE_INSENSITIVE));
    } catch (PatternSyntaxException e) {
        throw new IllegalArgumentException(
                "Invalid LIKE rule - " + e.getMessage());
    }
  }
992446
protected function createRule($ruleName, $definition)
    {
        echo "    > create rule $ruleName ...";
        $time = microtime(true);

        if (is_array($definition)) {
            $definition['name'] = $ruleName;
        } else {
            $definition = [
                'class' => $definition,
                'name' => $ruleName,
            ];
        }

        /** @var Rule $rule */
        $rule = \Yii::createObject($definition);

        $this->authManager->add($rule);
        echo ' done (time: ' . sprintf('%.3f', microtime(true) - $time) . "s)\n";

        return $rule;
    }
565308
public function addRule($field, $rule, $message, $options = array()) {
        if (empty($this->_fields[$field])) {
            throw new InvalidArgumentException(sprintf('Field %s does not exist', $field));
        }

        if (isset($this->_messages[$rule])) {
            $message = $message ?: $this->_messages[$rule];
        } else {
            $this->_messages[$rule] = $message;
        }

        $this->_rules[$field][$rule] = array(
            'message' => $message,
            'options' => (array) $options
        );

        return $this;
    }
1179020
protected function getRule($attribute, $rules)
    {
        if (! array_key_exists($attribute, $this->rules)) {
            return;
        }

        $rules = (array) $rules;

        foreach ($this->rules[$attribute] as $rule) {
            [$rule, $parameters] = ValidationRuleParser::parse($rule);

            if (in_array($rule, $rules)) {
                return [$rule, $parameters];
            }
        }
    }
557017
private function validateField($field, $data, $rules) {
		foreach ($rules as $rule) {
			list($m, $ops, $msg) = $rule;
			$valid_m = 'v_' . $m;
			if (method_exists($this, $valid_m)) {
				if ($msg) {
					$msg = preg_replace('#\{\d+?\}#', '%s', $msg);
				}
				$valid = $this->$valid_m ($field, $ops, $data, $msg);
			} else {
				return _t('notsupportmethod@validator', $field, $m);
			}
			if ($valid !== true) {
				return $valid;
			}
		}

		return true;
	}
658510
private function formatRule($rules)
    {
        if (is_array($rules)) {
            foreach ($rules as $rule) {
                $formatted[] = $this->formatRule($rule);
            }

            return $formatted ?? [];
        }

        if (is_object($rules) || is_callable($rules)) {
            return $rules;
        }

        if (Str::contains($rules, 'required_if') && preg_match('/(.*):(.*),(.*)/', $rules, $matches)) {
            $column = Str::startsWith($matches[2], '*.') ? $matches[2] : '*.' . $matches[2];

            return $matches[1] . ':' . $column . ',' . $matches[3];
        }

        return $rules;
    }
533847
public function retrieveRule($field, $name = null)
    {
        $key = $this->generateKey(self::DYNAMIC_RULE_KEY, $field);
        $allRules = self::storage()->get($key, []);
        if (empty($name)) {
            return $allRules;
        }

        return isset($allRules[$name]) ? $allRules[$name] : null;
    }
695735
public function storeRule($field, $name, $rule = null)
    {
        self::validateFieldName($field);
        if (is_array($name)) {
            foreach ($name as $k => $v) {
                $this->storeRule($field, $k, $v);
            }

            return;
        }
        if ($rule === null && is_string($name)) {
            $rule = $name;
            $name = null;
        }
        if (empty($name) || !is_string($name)) {
            $name = Util::pathOfUrl(URL::current(), function ($e) use ($field) {
                throw new LaravelSmsException("Expected a name for the dynamic rule which belongs to field `$field`.");
            });
        }
        $allRules = $this->retrieveRules($field);
        $allRules[$name] = $rule;
        $key = $this->generateKey(self::DYNAMIC_RULE_KEY, $field);
        self::storage()->set($key, $allRules);
    }
122077
public function get_rule( array $rules ) {
		$logical = static::LOGICAL_AND;

		if ( static::is_group_rules( $rules ) ) {
			list( $logical, $rules ) = $this->parse_group_logical( $rules );
		}

		return $this->create_rule( $rules, $logical );
	}
674458
public function buildRules(RulesChecker $rules)
    {
        // check max instances limit
        $rules->addCreate(function ($instance, $options) {
            $info = (array)$instance->info();
            if (isset($info['maxInstances']) && $info['maxInstances'] > 0) {
                if (!$instance->get('eav_attribute')) {
                    return false;
                }

                $count = $this
                    ->find()
                    ->select(['FieldInstances.id', 'FieldInstances.handler', 'EavAttribute.id', 'EavAttribute.table_alias'])
                    ->contain(['EavAttribute'])
                    ->where([
                        'EavAttribute.table_alias' => $instance->get('eav_attribute')->get('table_alias'),
                        'FieldInstances.handler' => $instance->get('handler'),
                    ])
                    ->count();

                return ($count <= (intval($info['maxInstances']) - 1));
            }

            return true;
        }, 'maxInstances', [
            'errorField' => 'label',
            'message' => __d('field', 'No more instances of this field can be attached, limit reached.'),
        ]);

        return $rules;
    }
325970
protected function generateRules($rules)
    {
        return collect($rules)->mapWithKeys(function ($rules, $key) {
            return [$this->attribute . '.' . $key => $rules];
        })->toArray();
    }
523704
public function create_rule( array $rules, $logical = 'and' ) {
		if ( empty( $rules ) ) {
			return null;
		}

		if ( static::is_proposition( $rules ) ) {
			$rules = [ $rules ];
		}

		if ( ! $props = $this->build_propositions( $rules ) ) {
			return null;
		}

		// Correct the "or" logical in case we have only one prop.
		if ( static::LOGICAL_OR === $logical && count( $props ) === 1 ) {
			$logical = static::LOGICAL_AND;
		}

		$logical = static::$logicals[ $logical ];

		return new Rule( new $logical( $props ) );
	}