Skip to content

Commit

Permalink
renderer defaults to an id-prefix of "form" (because e.g. radio/check…
Browse files Browse the repository at this point in the history
…box groups aren't possible without an id)
  • Loading branch information
Rasmus Schultz committed May 18, 2016
1 parent 6cae08c commit ee02a68
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/InputRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class InputRenderer
* @param string|string[]|null $collection_name collection name(s) for inputs, e.g. 'myform' or ['myform', '123'] etc.
* @param string|null $id_prefix base id for inputs, e.g. 'myform' or 'myform-123', etc.
*/
public function __construct($model = null, $collection_name = null, $id_prefix = null)
public function __construct($model = null, $collection_name = null, $id_prefix = "form")
{
$this->model = InputModel::create($model);
$this->collection_name = $collection_name;
Expand Down
62 changes: 31 additions & 31 deletions tests/unit/FieldRenderCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,38 @@ public function renderTextFields(UnitTester $I)
$model = $form->model;
$field = new TextField('value');

$I->assertSame('<input class="form-control" name="value" type="text"/>', $form->render($field),
$I->assertSame('<input class="form-control" id="form-value" name="value" type="text"/>', $form->render($field),
'basic input with no value-attribute');

$field->setValue($model, 'Hello World');

$I->assertSame('<input class="form-control" name="value" type="text" value="Hello World"/>',
$I->assertSame('<input class="form-control" id="form-value" name="value" type="text" value="Hello World"/>',
$form->render($field), 'basic input with value-attribute');

$field->max_length = 50;

$I->assertSame('<input class="form-control" maxlength="50" name="value" type="text" value="Hello World"/>',
$I->assertSame('<input class="form-control" id="form-value" maxlength="50" name="value" type="text" value="Hello World"/>',
$form->render($field), 'input with value and maxlength-attribute');

$field->setPlaceholder('hello');

$I->assertSame('<input class="form-control" maxlength="50" name="value" placeholder="hello" type="text" value="Hello World"/>',
$I->assertSame('<input class="form-control" id="form-value" maxlength="50" name="value" placeholder="hello" type="text" value="Hello World"/>',
$form->render($field), 'input with value, maxlength and placeholder-attributes');
$I->assertSame('<input class="form-control" data-foo="bar" maxlength="50" name="value" placeholder="hello" type="text" value="Hello World"/>',
$I->assertSame('<input class="form-control" data-foo="bar" id="form-value" maxlength="50" name="value" placeholder="hello" type="text" value="Hello World"/>',
$form->render($field, ['data-foo' => 'bar']), 'input with custom data-attribute overridden');
$I->assertSame('<input class="form-control" maxlength="50" name="value" placeholder="override" type="text" value="Hello World"/>',
$I->assertSame('<input class="form-control" id="form-value" maxlength="50" name="value" placeholder="override" type="text" value="Hello World"/>',
$form->render($field, ['placeholder' => 'override']), 'input with placeholder-attribute overridden');

$field->setValue($model, 'this & that');

$I->assertSame('<input class="form-control" maxlength="50" name="value" placeholder="hello" type="text" value="this &amp; that"/>',
$I->assertSame('<input class="form-control" id="form-value" maxlength="50" name="value" placeholder="hello" type="text" value="this &amp; that"/>',
$form->render($field), 'input with value-attribute escaped as HTML');

$field = new TextField("value");

$field->setPattern('[A-Z]{2}', 'Enter two-letter ISO language code');

$I->assertSame('<input class="form-control" data-pattern-error="Enter two-letter ISO language code" name="value" pattern="[A-Z]{2}" type="text" value="this &amp; that"/>',
$I->assertSame('<input class="form-control" data-pattern-error="Enter two-letter ISO language code" id="form-value" name="value" pattern="[A-Z]{2}" type="text" value="this &amp; that"/>',
$form->render($field));
}

Expand All @@ -69,7 +69,7 @@ public function renderPasswordField(UnitTester $I)

$field->setValue($model, 'supersecret');

$I->assertSame('<input class="form-control" name="value" type="password" value=""/>', $form->render($field),
$I->assertSame('<input class="form-control" id="form-value" name="value" type="password" value=""/>', $form->render($field),
'input with type=password');
}

Expand All @@ -93,7 +93,7 @@ public function renderEmailField(UnitTester $I)

$field->setValue($model, '[email protected]');

$I->assertSame('<input class="form-control" name="value" type="email" value="[email protected]"/>',
$I->assertSame('<input class="form-control" id="form-value" name="value" type="email" value="[email protected]"/>',
$form->render($field), 'input with type=email (html5)');
}

Expand All @@ -105,7 +105,7 @@ public function renderTextArea(UnitTester $I)

$field->setValue($model, 'this & that');

$I->assertSame('<textarea class="form-control" name="value">this &amp; that</textarea>', $form->render($field),
$I->assertSame('<textarea class="form-control" id="form-value" name="value">this &amp; that</textarea>', $form->render($field),
'simple textarea with content');
}

Expand Down Expand Up @@ -154,22 +154,22 @@ public function renderSelectTags(UnitTester $I)
2 => 'Option Two',
]);

$I->assertSame('<select class="form-control" name="value"><option value="1">Option One</option><option value="2">Option Two</option></select>',
$I->assertSame('<select class="form-control" id="form-value" name="value"><option value="1">Option One</option><option value="2">Option Two</option></select>',
$form->render($field));

$field->setValue($form->model, 1);

$I->assertSame('<select class="form-control" name="value"><option selected value="1">Option One</option><option value="2">Option Two</option></select>',
$I->assertSame('<select class="form-control" id="form-value" name="value"><option selected value="1">Option One</option><option value="2">Option Two</option></select>',
$form->render($field));

$field->disabled = 'Please select';

$I->assertSame('<select class="form-control" name="value"><option disabled>Please select</option><option selected value="1">Option One</option><option value="2">Option Two</option></select>',
$I->assertSame('<select class="form-control" id="form-value" name="value"><option disabled>Please select</option><option selected value="1">Option One</option><option value="2">Option Two</option></select>',
$form->render($field));

$field->setValue($form->model, null);

$I->assertSame('<select class="form-control" name="value"><option disabled selected>Please select</option><option value="1">Option One</option><option value="2">Option Two</option></select>',
$I->assertSame('<select class="form-control" id="form-value" name="value"><option disabled selected>Please select</option><option value="1">Option One</option><option value="2">Option Two</option></select>',
$form->render($field));
}

Expand Down Expand Up @@ -204,16 +204,16 @@ public function renderIntField(UnitTester $I)

$field = new IntField("value");

$I->assertSame('<input class="form-control" name="value" pattern="-?\d*" type="number"/>', $form->render($field));
$I->assertSame('<input class="form-control" id="form-value" name="value" pattern="-?\d*" type="number"/>', $form->render($field));

$field->min_value = -1;

$I->assertSame('<input class="form-control" min="-1" name="value" pattern="-?\d*" type="number"/>', $form->render($field));
$I->assertSame('<input class="form-control" id="form-value" min="-1" name="value" pattern="-?\d*" type="number"/>', $form->render($field));

$field->min_value = 1;
$field->max_value = 99;

$I->assertSame('<input class="form-control" max="99" min="1" name="value" pattern="\d*" type="number"/>', $form->render($field));
$I->assertSame('<input class="form-control" id="form-value" max="99" min="1" name="value" pattern="\d*" type="number"/>', $form->render($field));
}

public function renderFloatField(UnitTester $I)
Expand All @@ -222,16 +222,16 @@ public function renderFloatField(UnitTester $I)

$field = new FloatField("value");

$I->assertSame('<input class="form-control" name="value" pattern="-?\d*(\.(?=\d))?\d*" type="number"/>', $form->render($field));
$I->assertSame('<input class="form-control" id="form-value" name="value" pattern="-?\d*(\.(?=\d))?\d*" type="number"/>', $form->render($field));

$field->min_value = -1;

$I->assertSame('<input class="form-control" min="-1" name="value" pattern="-?\d*(\.(?=\d))?\d*" type="number"/>', $form->render($field));
$I->assertSame('<input class="form-control" id="form-value" min="-1" name="value" pattern="-?\d*(\.(?=\d))?\d*" type="number"/>', $form->render($field));

$field->min_value = 1;
$field->max_value = 99;

$I->assertSame('<input class="form-control" max="99" min="1" name="value" pattern="\d*(\.(?=\d))?\d*" type="number"/>', $form->render($field));
$I->assertSame('<input class="form-control" id="form-value" max="99" min="1" name="value" pattern="\d*(\.(?=\d))?\d*" type="number"/>', $form->render($field));
}

public function renderDateTimeField(UnitTester $I)
Expand All @@ -240,7 +240,7 @@ public function renderDateTimeField(UnitTester $I)
$field = new DateTimeField('value', 'Europe/Copenhagen', 'Y-m-d H:i:s', ['readonly' => true]);
$field->setValue($form->model, 173919600);

$I->assertSame('<input class="form-control" name="value" readonly type="text" value="1975-07-07 00:00:00"/>',
$I->assertSame('<input class="form-control" id="form-value" name="value" readonly type="text" value="1975-07-07 00:00:00"/>',
$form->render($field));
}

Expand Down Expand Up @@ -269,17 +269,17 @@ public function renderDateSelector(UnitTester $I)

$I->expectParts(
$form->render($field), [
'<select class="form-control day" name="value[day]">',
'<select class="form-control day" id="form-value-day" name="value[day]">',
'<option value="1">1</option>',
'<option selected value="7">7</option>',
'<option value="31">31</option>',
'</select>',
'<select class="form-control month" name="value[month]">',
'<select class="form-control month" id="form-value-month" name="value[month]">',
'<option value="1">January</option>',
'<option selected value="7">July</option>',
'<option value="12">December</option>',
'</select>',
'<select class="form-control year" name="value[year]">',
'<select class="form-control year" id="form-value-year" name="value[year]">',
'<option value="1974">1974</option>',
'<option selected value="1975">1975</option>',
'<option value="1976">1976</option>',
Expand All @@ -291,11 +291,11 @@ public function renderDateSelector(UnitTester $I)

$I->expectParts(
$form->render($field), [
'<select class="form-control day" name="value[day]">',
'<select class="form-control day" id="form-value-day" name="value[day]">',
'<option disabled>Day</option>',
'<select class="form-control month" name="value[month]">',
'<select class="form-control month" id="form-value-month" name="value[month]">',
'<option disabled>Month</option>',
'<select class="form-control year" name="value[year]">',
'<select class="form-control year" id="form-value-year" name="value[year]">',
'<option disabled>Year</option>',
]
);
Expand All @@ -304,11 +304,11 @@ public function renderDateSelector(UnitTester $I)

$I->expectParts(
$form->render($field), [
'<select class="form-control day" name="value[day]">',
'<select class="form-control day" id="form-value-day" name="value[day]">',
'<option disabled selected>Day</option>',
'<select class="form-control month" name="value[month]">',
'<select class="form-control month" id="form-value-month" name="value[month]">',
'<option disabled selected>Month</option>',
'<select class="form-control year" name="value[year]">',
'<select class="form-control year" id="form-value-year" name="value[year]">',
'<option disabled selected>Year</option>',
]
);
Expand Down
23 changes: 13 additions & 10 deletions tests/unit/InputRendererCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,24 @@ public function handleAttributes(UnitTester $I)
$form = new InputRenderer();
$field = new TextField('text');

$I->assertSame('<input class="form-control foo bar" name="text" type="text"/>',
$I->assertSame('<input class="form-control foo bar" id="form-text" name="text" type="text"/>',
$form->render($field, ['class' => ['foo', 'bar']]), 'folds multi-valued class attribute');
$I->assertSame('<input class="form-control" name="text" readonly type="text"/>',
$I->assertSame('<input class="form-control" id="form-text" name="text" readonly type="text"/>',
$form->render($field, ['readonly' => true]), 'handles boolean TRUE attribute value');
$I->assertSame('<input class="form-control" name="text" type="text"/>',
$I->assertSame('<input class="form-control" id="form-text" name="text" type="text"/>',
$form->render($field, ['readonly' => false]), 'handles boolean FALSE attribute value');
$I->assertSame('<input class="form-control" name="text" type="text"/>',
$I->assertSame('<input class="form-control" id="form-text" name="text" type="text"/>',
$form->render($field, ['foo' => null]), 'filters NULL-value attributes');

$form->xhtml = true;
$I->assertSame('<input class="form-control" name="text" readonly="readonly" type="text"/>',
$I->assertSame('<input class="form-control" id="form-text" name="text" readonly="readonly" type="text"/>',
$form->render($field, ['readonly' => true]), 'renders value-less attributes as valid XHTML');
$form->xhtml = false;

$I->assertSame('text', $form->getName($field), 'name without prefix');

$form->id_prefix = null;

$I->assertSame(null, $form->getId($field), 'no id attribute when $id_prefix is NULL');

$form->collection_name = 'form';
Expand Down Expand Up @@ -126,7 +129,7 @@ public function renderContainerTags(UnitTester $I)
$I->assertSame('<div data-foo="bar">foo &amp; bar</div>',
$form->divFor($field, 'foo &amp; bar', ['data-foo' => 'bar']));

$I->assertSame('<div data-div="bar"><input class="form-control" data-input="foo" name="text" type="text"/></div>',
$I->assertSame('<div data-div="bar"><input class="form-control" data-input="foo" id="form-text" name="text" type="text"/></div>',
$form->renderDiv($field, ['data-input' => 'foo'], ['data-div' => 'bar']));

$field->setRequired(true);
Expand All @@ -136,7 +139,7 @@ public function renderContainerTags(UnitTester $I)
$I->assertSame('<div class="required has-error" data-foo="bar">foo &amp; bar</div>',
$form->divFor($field, 'foo &amp; bar', ['data-foo' => 'bar']));

$I->assertSame('<div class="required has-error foo bar"><input class="form-control foo" name="text" required type="text"/></div>',
$I->assertSame('<div class="required has-error foo bar"><input class="form-control foo" id="form-text" name="text" required type="text"/></div>',
$form->renderDiv($field, ['class' => 'foo'], ['class' => ['foo', 'bar']]));
}

Expand Down Expand Up @@ -166,7 +169,7 @@ public function renderInputGroups(UnitTester $I)

public function renderLabelTags(UnitTester $I)
{
$form = new InputRenderer();
$form = new InputRenderer(null, null, null);
$field = new TextField('text');

$I->assertException(
Expand Down Expand Up @@ -225,7 +228,7 @@ public function overridePlaceholders(UnitTester $I)

$form->setPlaceholder($field, "Hello");

$I->assertSame('<input class="form-control" name="test" placeholder="Hello" type="text"/>', $form->render($field));
$I->assertSame('<input class="form-control" id="form-test" name="test" placeholder="Hello" type="text"/>', $form->render($field));
}

public function overrideRequired(UnitTester $I)
Expand All @@ -236,7 +239,7 @@ public function overrideRequired(UnitTester $I)

$form->setRequired($field);

$I->assertSame('<div class="required"><input class="form-control" name="test" required type="text"/></div>', $form->renderDiv($field));
$I->assertSame('<div class="required"><input class="form-control" id="form-test" name="test" required type="text"/></div>', $form->renderDiv($field));
}

public function buildInput(UnitTester $I)
Expand Down

0 comments on commit ee02a68

Please sign in to comment.