public function testForm() { $form = new \Gajus\Dora\Form([], 'Gajus\\Dora\\Template\\Test'); $input = $form->input('foo'); $input = '' . $input; $this->assertSame('test', $input); }
public function testForm() { $form = new \Gajus\Dora\Form([], null); $input = $form->input('foo'); $input = '' . $input; $this->assertSame('<input name="foo" type="text" value="">', $input); }
public function testDefault() { $form = new \Gajus\Dora\Form([], 'Gajus\\Dora\\Template\\Traditional'); $input = $form->input('foo', ['id' => 'test']); $input = $input->toString(); // https://gist.github.com/anonymous/220b2cec6d51ea5bc728 $this->assertSame(preg_replace('/[^a-z]/', '', ' <div class="dora-input"> <label for="test">Foo</label> <input id="test" name="foo" type="text" value=""> </div> '), preg_replace('/[^a-z]/', '', $input)); }
</label> <?php echo $input; ?> <?php /*if ($tags):?> <ul class="demo-tags"> <li><?=implode('</li><li>', $tags)?></li> </ul> <?php endif;*/ ?> </div> <?php return ob_get_clean(); }); ?> <form action="" method="post"> <?php echo $label->input('tag_input'); ?> <?php echo $form->input('action[submit]', ['type' => 'submit', 'value' => 'Submit']); ?> </form> <?php if ($form->isSubmitted()) { foreach (array_filter(explode(',', $_POST['tag_input'])) as $tag) { // Append new "tag-list" value to the input[name="tag_input"] dataset. $form->send('tag_input', 'tag-list', htmlspecialchars($tag)); } }
<?php // Custom template must extend Gajus\Dora\Template class MyTemplate extends \Gajus\Dora\Template { public function toString() { $input = $this->getInput(); ob_start(); ?> <div class="dora-input custom"> <label for="<?php echo $input->getAttribute('id'); ?> "><?php echo $input->getProperty('name'); ?> </label> <?php echo $input; ?> </div> <?php return ob_get_clean(); } } $form = new \Gajus\Dora\Form(null, 'MyTemplate'); echo $form->input('baz', null, ['name' => 'Baz Custom Name']); echo $form->input('qux', null, ['name' => 'Qux Custom Name']); // You can change individual input template too. echo $form->input('qux', null, ['name' => 'Qux Custom Name'], 'Gajus\\Dora\\Template\\Traditional');
$form = new \Gajus\Dora\Form($_POST); // Note that "bar" input has a default value: // $form->input('bar', ['value' => mt_rand(1000,9999)]) // However, when you submit the form, this value is overwritten. // Form signature is used to generate UID and CSRF tokens. // UID is used to identify instance of the form that's submitted. // Form signature is generated using $form->sign(). if ($form->isSubmitted()) { header('Location: ' . $_SERVER['REQUEST_URI']); exit; } ?> <form action="" method="post"> <?php echo $form->input('foo'); ?> <?php echo $form->input('bar', ['value' => mt_rand(1000, 9999)]); ?> <div class="button-group"> <?php echo $form->input('action[submit]', ['type' => 'submit', 'value' => 'Submit'], null, null); ?> </div> <?php echo $form->sign(); ?> </form>
<?php // This example demonstrates how to generate basic input. // Values passed the Form constructor are mapped to the generated Input. // This example is using the default input template. // Therefore, input comes wraped in <div class="dora-input"></div> container // with dynamically generated input name property. /** * @param array $data Data used to populate Input generated using an instance of this Form. * @param null|string $template Template class name. */ $form = new \Gajus\Dora\Form(['foo' => 'Heeeere\'s...Johnny!', 'bar' => 'Yada, yada, yada.', 'baz' => 0]); /** * Create Input associated with the Form instance data. * * @param string $name * @param array $attributes * @param array $properties * @param string $template * @return \Gajus\Dora\Input */ echo $form->input('foo'); echo $form->input('bar', ['type' => 'textarea', 'class' => 'test']); echo $form->input('baz', null, ['options' => ['Knock, knock...', 'Come in.']]);
/** * @expectedException Gajus\Dora\Exception\InvalidArgumentException * @expectedExceptionMessage Input instantiated using Form::input() method cannot explicitly define "value" property. */ public function testFormInputCannotSetValueProperty() { $form = new \Gajus\Dora\Form(); $form->input('test', null, ['value' => 'test']); }
<?php $form = new \Gajus\Dora\Form(); // Attibutes are defined using the second Input parameter. echo $form->input('foo', ['data-foo' => 'bar']); // If Input "id" attribute is not defined, and it is requested before Input is stringified, // then a new random (semi-persistent) ID will be generated. $input = $form->input('foo'); ?> <dl> <dt>Id</dt> <dd><?php echo $input->getAttribute('id'); ?> </dd> </dl>
<?php $form = new \Gajus\Dora\Form(['mu' => ['lt' => ['id' => ['im' => ['en' => ['si' => ['on' => ['al' => 'Multidimensional Data']]]]]]], 'array' => ['', 'second'], 'checkbox_multiple' => ['c', 'd'], 'radio' => 'b']); // Multidimensional data echo $form->input('mu[lt][id][im][en][si][on][al]'); // Array data echo $form->input('array[]'); echo $form->input('array[]'); echo $form->input('array[]'); // Passing "options" parameter implies that the input type is "select". echo $form->input('select_multiple[]', ['multiple' => 'multiple'], ['options' => ['Quick!', 'To the Batmobile!', 'Da-na-na-na', 'Na-na-na-na']]); echo $form->input('checkbox_multiple[]', ['type' => 'checkbox', 'value' => 'a']); echo $form->input('checkbox_multiple[]', ['type' => 'checkbox', 'value' => 'b']); echo $form->input('checkbox_multiple[]', ['type' => 'checkbox', 'value' => 'c']); echo $form->input('checkbox_multiple[]', ['type' => 'checkbox', 'value' => 'd']); echo $form->input('radio', ['type' => 'radio', 'value' => 'a']); echo $form->input('radio', ['type' => 'radio', 'value' => 'b']);
<?php $form = new \Gajus\Dora\Form([], null); // "input" method returns an instance of Gajus\Dora\Input. // Use "name" property to give Input a human-readable name. $input = $form->input('foo', null, ['name' => 'Foo Label Property']); ?> <div class="dora-input"> <label><?php echo $input->getProperty('name'); ?> </label> <?php echo $input; ?> </div> <?php // When "name" property is not provided, Dora will derive name from the input "name" attribute. $input = $form->input('bar[baz_qux][]'); ?> <dl> <dt>bar[baz_qux][]</dt> <dd><?php echo $input->getProperty('name'); ?> </dd> </dl>