public function bindFromRequest(ServerRequestInterface $request, bool $trimData = true) : self { if ('POST' === $request->getMethod()) { $data = Data::fromNestedArray($request->getParsedBody()); } else { $data = Data::fromNestedArray($request->getQueryParams()); } if ($trimData) { $data = $data->transform(new TrimTransformer()); } return $this->bind($data); }
public function testBindAppliesConstraintsToValidResult() { $data = Data::fromNestedArray(['foo' => ['bar' => ['baz']]]); $wrappedMapping = $this->prophesize(MappingInterface::class); $wrappedMapping->withPrefixAndRelativeKey('foo[bar]', '0')->willReturn($wrappedMapping->reveal()); $wrappedMapping->bind($data)->willReturn(BindResult::fromValue('baz')); $constraint = $this->prophesize(ConstraintInterface::class); $constraint->__invoke(['baz'])->willReturn(new ValidationResult(new ValidationError('bar', [], '0'))); $mapping = (new RepeatedMapping($wrappedMapping->reveal()))->withPrefixAndRelativeKey('foo', 'bar')->verifying($constraint->reveal()); $bindResult = $mapping->bind($data); $this->assertFalse($bindResult->isSuccess()); $this->assertSame('bar', $bindResult->getFormErrorSequence()->getIterator()->current()->getMessage()); $this->assertSame('foo[bar][0]', $bindResult->getFormErrorSequence()->getIterator()->current()->getKey()); }
public function testGetIndexes() { $data = Data::fromNestedArray(['foo' => ['bar', 'baz' => 'bat', ['foo', 'bar']]]); $this->assertSame(['0', 'baz', '1'], $data->getIndexes('foo')); }
public function testMultipleSelectedValues() { $helper = new Select(); $this->assertSame("<select multiple id=\"input.foo\" name=\"foo[]\"><option value=\"foo\" selected>bar</option>\n" . "<option value=\"baz\" selected>bat</option>\n" . "<option value=\"a\">b</option></select>", $helper(new Field('foo', '', new FormErrorSequence(), Data::fromNestedArray(['foo' => ['foo', 'baz']])), ['foo' => 'bar', 'baz' => 'bat', 'a' => 'b'], ['multiple' => 'multiple'])); }