public function testShouldReturnOldContext() { $mock = new MockLayout(); $resource = $this->getMockBuilder(MyResource::class)->enableProxyingToOriginalMethods()->setConstructorArgs([null])->getMock(); $resource->expects($this->once())->method('getErrorFor'); $form = Form::create('id', function ($f) { $f->textfield('foo'); }, ['layout' => $mock]); $this->assertCount(1, $mock->field); $this->assertInstanceOf('NitroXy\\PHPForms\\FormInput', $mock->field['foo']); $this->assertEquals('field error', $mock->field['foo']->getError($resource)); }
public function testGroup() { $mock = new MockLayout(); $form = Form::create('id', function ($f) { $f->textField('c', null); $f->group("group1", function ($f) { $f->textField('a', null); }); $f->group("group2", function ($f) { $f->textField('b', null); }); }, ['layout' => $mock]); $this->assertEquals(1, $mock->opened, "Layout preamble must be opened exactly once"); $this->assertEquals(1, $mock->closed, "Layout postamble must be closed exactly once"); $this->assertArrayHasKey('group1', $mock->group); $this->assertArrayHasKey('group2', $mock->group); $this->assertArrayHasKey('a', $mock->group['group1']); $this->assertArrayHasKey('b', $mock->group['group2']); }
public function testUploadField() { $mock = new MockLayout(); $form = Form::create('id', function ($f) { $f->uploadField('foo', 'Label'); $f->uploadField('bar', 'Label', ['remove' => true]); $f->uploadField('baz', 'Label', ['current' => 'Preview']); }, ['layout' => $mock]); $this->assertArrayHasKey('foo', $mock->field); $this->assertArrayNotHasKey('foo_remove', $mock->field); $this->assertArrayNotHasKey('foo_current', $mock->field); $this->assertArrayHasKey('bar', $mock->field); $this->assertArrayHasKey('bar_remove', $mock->field); $this->assertArrayNotHasKey('bar_current', $mock->field); $this->assertArrayHasKey('baz', $mock->field); $this->assertArrayNotHasKey('baz_remove', $mock->field); $this->assertArrayHasKey('baz_current', $mock->field); $this->assertEquals('file', $mock->field['foo']->attribute('type')); }
<?php /*~*/ use NitroXy\PHPForms\Form; global $layout; $data = ['a' => 'Value 1', 'b' => 'Value 2', 'c' => 'Value 3', 'd' => 'Value 4']; Form::fromArray('example_prefix_suffix', $data, function ($f) { $f->textField('a', 'Text 1'); $f->textField('b', 'Text 2', ['prefix' => 'Prefix']); $f->textField('c', 'Text 3', ['suffix' => 'Suffix']); $f->textField('d', 'Text 4', ['prefix' => 'Prefix', 'suffix' => 'Suffix']); }, ['layout' => $layout]);
<?php /*~*/ use NitroXy\PHPForms\Form; global $layout; $data = ['a' => 'Value 1', 'b' => 'Value 2', 'c' => 'Value 3']; Form::fromArray('example5', $data, function ($f) { $f->textField('a', 'Text 1'); $f->textField('b', 'Text 2'); $f->textField('c', 'Text 3', ['required' => true, 'hint' => 'Required field']); }, ['layout' => $layout]);
public function testSubmitName() { $mock = new MockLayout(); $form = Form::create('id', function ($f) { $f->submit('label', 'foo'); }, ['layout' => $mock]); $this->assertCount(1, $mock->field); $this->assertInstanceOf('NitroXy\\PHPForms\\FormButton', $mock->field['foo']); $this->assertEquals('<button type="submit" name="foo" id="id_foo">label</button>', $mock->field['foo']->getContent()); }
<?php /*~*/ use NitroXy\PHPForms\Form; global $layout; $data = ['a' => '1', 'e' => '2', 'i' => '3']; Form::fromArray('example5', $data, function ($f) { $f->group('Row 1', function ($f) { $f->textField('a', 'Text 1', ['style' => 'width: 50px;']); $f->textField('b', 'Text 2', ['style' => 'width: 50px;']); $f->textField('c', 'Text 3', ['style' => 'width: 50px;']); }); $f->group('Row 2', function ($f) { $f->textField('d', 'Text 1', ['style' => 'width: 50px;']); $f->textField('e', 'Text 2', ['style' => 'width: 50px;']); $f->textField('f', 'Text 3', ['style' => 'width: 50px;']); }); $f->group('Row 3', function ($f) { $f->textField('g', 'Text 1', ['style' => 'width: 50px;']); $f->textField('h', 'Text 2', ['style' => 'width: 50px;']); $f->textField('i', 'Text 3', ['style' => 'width: 50px;']); }); $f->group(false, function ($f) { $f->button('Action 1'); $f->button('Action 2'); }); }, ['layout' => $layout]);
public function testNoExtraAttr() { $mock = new MockLayout(); $form = Form::create('id', function ($f) { }, ['layout' => $mock]); $expected = ['id', 'class', 'method', 'action']; $this->assertEquals($expected, array_keys($mock->form_attr)); /* id is not passed via attribute but should also be present */ $this->assertEquals('id', $mock->form_id); }
/** * @expectedException PHPUnit_Framework_Error */ public function testInvalidLabel() { $mock = new MockLayout(); $form = Form::create('id', function ($f) { $f->select('foo', 5, FormOptions::fromArray([])); }, ['layout' => $mock]); }
<?php /*~*/ use NitroXy\PHPForms\Form; use NitroXy\PHPForms\FormSelect; class MyClassA { } class MyClassB { } $a = new MyClassA(); $a->name = 'Name A'; $b = new MyClassB(); $b->name = 'Name B'; Form::create('example6', function ($f) use($a, $b) { $f->fieldsFor('A', $a, function ($f) { $f->textField('name', 'Name'); }); $f->fieldsFor('B', $b, function ($f) { $f->textField('name', 'Name'); }); }, ['layout' => 'bootstrap']);
<?php /*~*/ use NitroXy\PHPForms\Form; Form::create("example_method", function ($f) { /* ... */ }, ['method' => 'patch']);
<?php /*~*/ use NitroXy\PHPForms\Form; global $layout; $data = ['a' => '1', 'e' => '2', 'i' => '3']; Form::fromArray('example5', $data, function ($f) { $f->textarea('foo', 'Regular textarea', ['hint' => 'Lorem ipsum']); $f->textarea('bar', '"tworow"', ['tworow' => true, 'hint' => 'Lorem ipsum']); $f->textarea('baz', '"tworow" + "fill"', ['tworow' => true, 'fill' => true, 'hint' => 'Lorem ipsum']); }, ['layout' => $layout]);
<?php /*~*/ use NitroXy\PHPForms\Form; global $layout; $data = ['a' => 'Value 1', 'b' => 'Value 2', 'c' => 'Value 3', 'd' => 'Value 4', 'e' => 'Value 5']; Form::fromArray('example5', $data, function ($f) { $f->textField('a', 'Text 1'); $f->fieldset('Fieldset A', function ($f) { $f->textField('b', 'Text 2'); $f->textField('c', 'Text 3'); }); $f->fieldset('Fieldset B', function ($f) { $f->textField('d', 'Text 4'); $f->textField('e', 'Text 5'); }); }, ['layout' => $layout]);
<?php /*~*/ use NitroXy\PHPForms\Form; global $layout; $data = ['a' => '1', 'd' => '2', 'g' => '3']; Form::fromArray('example5', $data, function ($f) { $f->group('Row 1', function ($f) { $f->textField('a', 'Text 1'); $f->textField('b', 'Text 2'); $f->textField('c', 'Text 3'); }); $f->group('Row 2', function ($f) { $f->textField('d', 'Text 1', ['class' => 'col-xs-8']); $f->textField('e', 'Text 2', ['class' => 'col-xs-4']); }); $f->group('Row 3', function ($f) { $f->textField('f', 'Text 1', ['class' => 'col-xs-4']); $f->textField('g', 'Text 2', ['class' => 'col-xs-8']); }); $f->group(false, function ($f) { $f->button('Action 1'); $f->button('Action 2'); }); }, ['layout' => $layout]);
<?php /*~*/ use NitroXy\PHPForms\Form; use NitroXy\PHPForms\FormOptions; class MyCustomClass extends stdClass { } $myObject = new MyCustomClass(); $myObject->name = 'John Doe'; $myObject->age = 46; $myObject->role = 2; /* frobnicator */ $myObject->description = 'Lorem ipsum dot sit amet.'; Form::fromObject($myObject, function ($f) { $f->textField('name', 'Name'); $f->textField('age', 'Age', ['type' => 'number', 'min' => 1]); $f->select('role', 'Role', FormOptions::fromArray(['', 'Manager', 'Frobnicator', 'Developer'])); $f->textarea('description', 'Description'); }, ['layout' => 'bootstrap']);
<?php /*~*/ use NitroXy\PHPForms\Form; global $layout; $data = ['a' => '1', 'e' => '2', 'i' => '3']; Form::fromArray('example5', $data, function ($f) { $f->hint('Row-level hints offers a hint that spans an entire row (similar to a paragraph).', false); $f->hint('Field-level hints has a similar look as a static field.', 'Field'); $f->textField('a', 'Text 1', ['hint' => 'Field hints describe a specific field']); }, ['layout' => $layout]);
<?php /*~*/ use NitroXy\PHPForms\Form; Form::create('example1', function ($f) { $f->textField('name', 'My field name'); });
public function testUnbufferedObject() { $obj = (object) ['id' => 7, 'foo' => 'spam']; ob_start(); Form::fromObject($obj, function ($f) { $f->textField('foo', 'Test field', ['type' => 'email']); }, ['layout' => 'unbuffered']); $this->html = ob_get_contents(); ob_end_clean(); $this->validate([['form_start'], ['input', ['type' => 'hidden', 'name' => 'stdClass[id]', 'value' => 7]], ['label_start', ['for' => 'stdClass_7_foo']], ['content', 'Test field'], ['label_end'], ['input', ['type' => 'email', 'name' => 'stdClass[foo]', 'id' => 'stdClass_7_foo']], ['form_end']]); }
use NitroXy\PHPForms\Form; use NitroXy\PHPForms\FormOptions; global $layout; Form::create('smoketest', function ($f) { $f->hiddenField('name', 'Hidden (this wont show at all)', ['value' => '8']); $f->textField('text_field', 'Text', ['hint' => 'Use the "type" option to use custom type such as number.']); $f->passwordField('password_field', 'Password', ['hint' => 'Passwords are not persistent if autocomplete is off', 'autocomplete' => 'off']); $f->customField('custom_field', 'email', 'Email', ['hint' => 'Another way to add custom input fields (same as using textField with type).', 'placeholder' => '*****@*****.**']); $f->textarea('textarea', 'Textarea'); $f->select('select', 'Select', FormOptions::fromArray(['A', 'B', 'C'])); $f->staticValue('static', 'Static text'); $f->link('link', 'https://example.net', 'Static link'); $f->hint('static', 'Lorem ipsum dot sit amet.'); $f->uploadField('upload', 'File upload', ['remove' => true]); $f->checkbox('checkbox', 'Checkbox'); $f->manual('manual', 'Manual', '<em>Custom html</em>', false); $f->submit('Submit button'); $f->button('Generic button', false, ['class' => 'btn-success']); /* groups allows you to put multiple fields inline (label is optional) */ $f->group('Inline group', function ($f) { $f->button('Button 1', false, ['class' => 'btn-default']); $f->button('Button 2', false, ['class' => 'btn-danger']); $f->button('Button 3'); }); /* fieldsets */ $f->fieldset('Fieldset', function ($f) { $f->textField('text1', 'Input 1'); $f->textField('text2', 'Input 2'); $f->textField('text3', 'Input 3'); }); }, ['layout' => $layout]);
<?php /*~*/ use NitroXy\PHPForms\Form; global $layout; $data = []; Form::fromArray('checkboxes', $data, function ($f) { $f->checkbox('a', 'Text 1', null, ['hint' => 'Checkbox using regular label']); $f->checkbox('b', 'Text 2', false, ['hint' => 'Checkbox using inline label']); $f->group('Label', function ($f) { $f->checkbox('c', 'Text 3'); $f->checkbox('d', 'Text 4'); }, ['hint' => 'Checkbox using group']); }, ['layout' => $layout]);
<?php /*~*/ use NitroXy\PHPForms\Form; Form::create("example2-{$layout}", function ($f) { $f->textField('name', 'My field name'); }, ['layout' => $layout]);