public function testOffsetExists() { $builder = new Builder(); $form = $builder->form([$builder->field('username')]); $this->assertTrue(isset($form['username'])); $this->assertFalse(isset($form['password'])); }
private function createNestedForm() { $builder = new Builder(); return $builder->form([$builder->field('username'), $builder->field('password'), $builder->field('address', [$builder->field('city'), $builder->field('street')], function ($city, $street) { return new Address($city, $street); })->optional()], function ($username, $password, Address $address = null) { return new User($username, $password, $address); }); }
public function test_required_boolean() { $builder = new Builder(); $form = $builder->form([$builder->field('accept')->required()->boolean()]); $form->bind([]); $errors = $form->getErrorsFlat(); $this->assertSame(2, count($errors)); $this->assertSame('formz.error.required', $errors[0]->getMessage()); $this->assertSame('formz.error.boolean', $errors[1]->getMessage()); }
public function test_nested_form_unapplied_from_array() { $builder = new Builder(); $form = $builder->form([$builder->field('username'), $builder->field('password'), $builder->field('address', [$builder->field('city'), $builder->field('street')])]); $form->fill(['username' => 'dennis84', 'address' => ['street' => 'Foostreet']]); $this->assertSame('dennis84', $form['username']->getValue()); $this->assertSame(null, $form['password']->getValue()); $this->assertSame(null, $form['address']['city']->getValue()); $this->assertSame('Foostreet', $form['address']['street']->getValue()); }
public function testRender() { $loader = new \Twig_Loader_String(); $twig = new \Twig_Environment($loader); $builder = new Builder(); $form = $builder->form([$builder->field('name'), $builder->field('address', [$builder->field('street'), $builder->field('city')])]); $form->fill(['name' => 'foobar']); $this->assertSame('address[street]', $twig->render('{{ form.address.street.name }}', ['form' => $form])); $this->assertSame('address[city]', $twig->render('{{ form.address.city.getName }}', ['form' => $form])); }
public function test_form_values() { $builder = new Builder(); $form = $builder->form([$builder->field('username')->nonEmptyText(), $builder->field('password')->nonEmptyText(), $builder->field('address', [$builder->field('city')->nonEmptyText(), $builder->field('street')->nonEmptyText()])]); $form->bind(['username' => 'dennis84', 'password' => '', 'address' => ['city' => 'foo']]); $this->assertSame('dennis84', $form['username']->getValue()); $this->assertSame('', $form['password']->getValue()); $this->assertSame('foo', $form['address']['city']->getValue()); $this->assertSame(null, $form['address']['street']->getValue()); }
public function test_transform_and_apply_order() { $test = $this; $builder = new Builder(); $form = $builder->form([$builder->field('foo')->integer()], function ($foo) use($test) { // The apply must come after transformation. $test->assertSame(420, $foo); }); $form->transform(new NonsenseTransformer()); $form->bind(['foo' => '42.2']); }
public function testCallUserFunc() { $builder = new Builder(); $user = new User('dennis84', 'demo123'); $formHandler = $this->getMock('Formz\\Tests\\Fixtures\\UserFormHandler', ['apply', 'unapply']); $formHandler->expects($this->once())->method('apply')->with($this->equalTo('foo'), $this->equalTo('bar')); $formHandler->expects($this->once())->method('unapply')->with($this->equalTo($user)); $form = $builder->form([$builder->field('username'), $builder->field('password')], [$formHandler, 'apply'], [$formHandler, 'unapply']); $form->fill($user); $form->bind(['username' => 'foo', 'password' => 'bar']); }
public function test_flat_form_applied_to_object() { $builder = new Builder(); $form = $builder->form([$builder->field('username'), $builder->field('password')], function ($username, $password) { return new User($username, $password); }); $data = ['foo' => ['username' => 'dennis84', 'password' => 'demo123']]; $form->bind($data); $formData = $form->getData(); $this->assertSame(null, $formData->username); $this->assertSame(null, $formData->password); }
public function test_nested_form_applied_to_object() { $builder = new Builder(); $form = $builder->form([$builder->field('username'), $builder->field('password'), $builder->field('address', [$builder->field('city'), $builder->field('street')], function ($city, $street) { return new Address($city, $street); })->required()], function ($username, $password, Address $address) { return new User($username, $password, $address); }); $data = ['username' => 'dennis84', 'password' => 'demo123']; $form->bind($data); $this->assertFalse($form->isValid()); }
public function test_verify_simple_form() { $builder = new Builder(); $form = $builder->form([$builder->field('username'), $builder->field('password'), $builder->field('password2')])->verifying('Invalid password or username.', function ($username, $password, $password2) { return $password === $password2; })->verifying('Username taken.', function ($username, $password, $password2) { return 'dennis84' !== $username; }); $form->bind(['username' => 'dennis84', 'password' => 'demo123', 'password2' => 'demo']); $this->assertSame('Invalid password or username.', $form->getErrorsFlat()[0]->getMessage()); $this->assertSame('Username taken.', $form->getErrorsFlat()[1]->getMessage()); }
public function test_get_data_from_unbound_form() { $builder = new Builder(); $form = $builder->form([$builder->field('username'), $builder->field('password')], function ($username, $password) { return new User($username, $password); }, function (User $user) { return ['username' => $user->username, 'password' => $user->password]; }); $user = new User('dennis', 'demo'); $form->fill($user); // Should the field throw an exception here? $this->assertNull($form->getData()); }
public function test_transform_single_field() { $builder = new Builder(); $form = $builder->form([$builder->field('birthday', [], function ($value) { return new \DateTime($value); }, function (\DateTime $date) { return $date->format('Y-m-d'); })]); $form->bind(['birthday' => '1984-01-01']); $data = $form->getData(); $this->assertInstanceOf('DateTime', $data['birthday']); $form->fill(['birthday' => new \DateTime('1984-01-01')]); $this->assertSame('1984-01-01', $form['birthday']->getValue()); }
public function testGetName() { $builder = new Builder(); $form = $builder->form([$builder->field('username'), $builder->field('password'), $builder->field('address', [$builder->field('street')]), $builder->field('choices', [$builder->field('key'), $builder->field('value')])->multiple()]); $this->assertSame('', $form->getName()); $this->assertSame('username', $form['username']->getName()); $this->assertSame('address[street]', $form['address']['street']->getName()); $this->assertSame('choices[]', $form['choices']->getName()); $form->bind(['choices' => [['key' => 'foo', 'value' => 'bar'], ['key' => 'bla', 'value' => 'blubb']]]); $this->assertSame('choices[0][key]', $form['choices']['0']['key']->getName()); $this->assertSame('choices[0][value]', $form['choices']['0']['value']->getName()); $this->assertSame('choices[1][key]', $form['choices']['1']['key']->getName()); $this->assertSame('choices[1][value]', $form['choices']['1']['value']->getName()); }
public function test_nested_form_applied_to_object() { $builder = new Builder(); $form = $builder->form([$builder->field('username'), $builder->field('password'), $builder->field('address', [$builder->field('city'), $builder->field('street')], function ($city, $street) { return new Address($city, $street); })], function ($username, $password, Address $address) { return new User($username, $password, $address); }); $data = ['username' => 'dennis84', 'password' => 'demo123', 'address' => ['city' => 'Footown', 'street' => 'Foostreet 12']]; $form->bind($data); $formData = $form->getData(); $this->assertInstanceOf('Formz\\Tests\\Fixtures\\User', $formData); $this->assertSame('dennis84', $formData->username); $this->assertSame('demo123', $formData->password); $this->assertInstanceOf('Formz\\Tests\\Fixtures\\Address', $formData->address); $this->assertSame('Footown', $formData->address->city); $this->assertSame('Foostreet 12', $formData->address->street); }
public function test_with_annotation_asserts() { $builder = new Builder([new \Formz\Extension\Symfonify($this->createValidator())]); $form = $builder->form([$builder->field('username'), $builder->field('password'), $builder->field('firstName'), $builder->field('last_name'), $builder->field('address', [$builder->field('city'), $builder->field('street')], function ($city, $street) { return new Address($city, $street); })], function ($username, $password, $address) { return new User($username, $password, $address); }); $request = Request::create('/', 'POST', ['username' => 'dennis', 'password' => 'demo', 'firstName' => '', 'last_name' => '', 'address' => ['city' => 'Foo', 'street' => 'Foostreet 12']]); $form->bindFromRequest($request); $formData = $form->getData(); $this->assertCount(1, $form['password']->getErrors()); $this->assertCount(1, $form['address']['city']->getErrors()); $this->assertCount(1, $form['firstName']->getErrors()); $this->assertCount(1, $form['last_name']->getErrors()); $this->assertCount(1, $form->getErrors()); $this->assertSame('passwordValid', $form->getErrors()[0]->getField()); $this->assertSame('foo', $form->getErrors()[0]->getMessage()); }
public function test_nested_form_unapplied_from_object() { $builder = new Builder(); $location = new Location('50', '8'); $address = new Address('Footown', 'Foostreet', $location); $user = new User('dennis84', 'demo123', $address); $form = $builder->form([$builder->field('username'), $builder->field('password'), $builder->field('address', [$builder->field('city'), $builder->field('street'), $builder->field('location', [$builder->field('lat'), $builder->field('lng')], null, function (Location $location) { return ['lat' => $location->lat, 'lng' => $location->lng]; })], null, function (Address $address) { return ['city' => $address->city, 'street' => $address->street, 'location' => $address->location]; })], null, function (User $user) { return ['username' => $user->username, 'password' => $user->password, 'address' => $user->address]; }); $form->fill($user); $this->assertSame('dennis84', $form['username']->getValue()); $this->assertSame('demo123', $form['password']->getValue()); $this->assertSame(['city' => 'Footown', 'street' => 'Foostreet', 'location' => ['lat' => '50', 'lng' => '8']], $form['address']->getValue()); $this->assertSame('Footown', $form['address']['city']->getValue()); $this->assertSame('Foostreet', $form['address']['street']->getValue()); $this->assertSame('50', $form['address']['location']['lat']->getValue()); $this->assertSame('8', $form['address']['location']['lng']->getValue()); }
public function test_fill_and_bind_and_increase_field() { $builder = new Builder(); $form = $builder->form([$builder->field('choices')->multiple()]); $form->fill(['choices' => ['foo', 'bar']]); $this->assertSame('foo', $form['choices']['0']->getValue()); $this->assertSame('bar', $form['choices']['1']->getValue()); $form->bind(['choices' => ['foo', 'bar', 'baz']]); $this->assertSame($form->getData(), ['choices' => ['foo', 'bar', 'baz']]); }