public function testLabel() { $object = new FromArray('label'); $data = array('test field' => array('label' => 'Test field', 'rules' => array('required', 'minLength' => 12))); $validator = \Mockery::mock('Fuel\\Validation\\Validator'); // Ensure the field gets added $validator->shouldReceive('addField')->once()->with('test field', 'Test field'); // Create some expected rules $requiredRule = new Required(); $minLengthRule = new MinLength(12); // Make sure the mocked object knows that the rules need to be created $validator->shouldReceive('createRuleInstance')->with('required', null)->once()->andReturn($requiredRule); $validator->shouldReceive('createRuleInstance')->with('minLength', 12)->once()->andReturn($minLengthRule); // Finally make sure the addRule function is called $validator->shouldReceive('addRule')->with('test field', $requiredRule)->once(); $validator->shouldReceive('addRule')->with('test field', $minLengthRule)->once(); $object->setData($data); $object->populateValidator($validator); }
/** * Generates a rule set that the parent FromArray can parse for fields * * @param $element * * @return array */ protected function processRules($element) { // If this is a container (Form or Fieldset) loop through each of the fields if ($element instanceof Form or $element instanceof Fieldset) { $result = []; foreach ($element as $field) { $result += $this->processRules($field); } return $result; } $metaData = $element->getMetaContainer(); if (isset($metaData['validation'])) { $label = $element->getLabel(); if (is_null($label)) { $label = $element->getName(); } return [$element->getName() => [$this->ruleProvider->getRuleKey() => $metaData['validation'], $this->ruleProvider->getLabelKey() => $label]]; } return [$element->getName() => []]; }
/** * {@inheritdocs} */ public function populateValidator(Validator $validator) { $generator = new FromArray($this->labelKey, $this->ruleKey); $generator->setData($this->struct)->populateValidator($validator); return $validator; }