From 95b602bc6fa3906c12fc0c74680699e299d1dda1 Mon Sep 17 00:00:00 2001 From: Rudie Dirkx Date: Mon, 21 Mar 2022 17:54:26 +0100 Subject: [PATCH] fixes #679: Removed CheckableType, ChoiceType, EntityType. Added CheckboxType, CheckboxesType, RadiosType, DatalistType --- src/Kris/LaravelFormBuilder/Field.php | 7 +- .../{CheckableType.php => CheckboxType.php} | 4 +- .../Fields/CheckboxesType.php | 22 +++ .../LaravelFormBuilder/Fields/ChoiceType.php | 173 ------------------ .../Fields/DatalistType.php | 15 ++ .../LaravelFormBuilder/Fields/EntityType.php | 119 ------------ .../LaravelFormBuilder/Fields/FormField.php | 7 +- .../LaravelFormBuilder/Fields/RadiosType.php | 24 +++ .../LaravelFormBuilder/Fields/SelectType.php | 2 +- src/Kris/LaravelFormBuilder/FormHelper.php | 14 +- src/config/config.php | 10 +- src/views/checkboxes.php | 32 ++++ src/views/choice.php | 27 --- src/views/datalist.php | 7 + src/views/radios.php | 32 ++++ tests/Fields/CheckableTypeTest.php | 14 +- tests/Fields/ChoiceTypeTest.php | 109 ----------- tests/Fields/CollectionTypeTest.php | 2 +- tests/Fields/EntityTypeTest.php | 103 ----------- tests/FormBuilderTestCase.php | 2 +- tests/FormHelperTest.php | 12 +- tests/FormTest.php | 60 ++---- 22 files changed, 175 insertions(+), 622 deletions(-) rename src/Kris/LaravelFormBuilder/Fields/{CheckableType.php => CheckboxType.php} (90%) create mode 100644 src/Kris/LaravelFormBuilder/Fields/CheckboxesType.php delete mode 100644 src/Kris/LaravelFormBuilder/Fields/ChoiceType.php create mode 100644 src/Kris/LaravelFormBuilder/Fields/DatalistType.php delete mode 100644 src/Kris/LaravelFormBuilder/Fields/EntityType.php create mode 100644 src/Kris/LaravelFormBuilder/Fields/RadiosType.php create mode 100644 src/views/checkboxes.php delete mode 100644 src/views/choice.php create mode 100644 src/views/datalist.php create mode 100644 src/views/radios.php delete mode 100644 tests/Fields/ChoiceTypeTest.php delete mode 100644 tests/Fields/EntityTypeTest.php diff --git a/src/Kris/LaravelFormBuilder/Field.php b/src/Kris/LaravelFormBuilder/Field.php index bc93a64..54ea719 100644 --- a/src/Kris/LaravelFormBuilder/Field.php +++ b/src/Kris/LaravelFormBuilder/Field.php @@ -2,16 +2,16 @@ namespace Kris\LaravelFormBuilder; - class Field { // Simple fields const TEXT = 'text'; const TEXTAREA = 'textarea'; const SELECT = 'select'; - const CHOICE = 'choice'; const CHECKBOX = 'checkbox'; - const RADIO = 'radio'; + const CHECKBOXES = 'checkboxes'; + const RADIOS = 'radios'; + const DATALIST = 'datalist'; const PASSWORD = 'password'; const HIDDEN = 'hidden'; const FILE = 'file'; @@ -31,7 +31,6 @@ class Field const TEL = 'tel'; const NUMBER = 'number'; const RANGE = 'range'; - const ENTITY = 'entity'; const FORM = 'form'; //Buttons const BUTTON_SUBMIT = 'submit'; diff --git a/src/Kris/LaravelFormBuilder/Fields/CheckableType.php b/src/Kris/LaravelFormBuilder/Fields/CheckboxType.php similarity index 90% rename from src/Kris/LaravelFormBuilder/Fields/CheckableType.php rename to src/Kris/LaravelFormBuilder/Fields/CheckboxType.php index daa7474..8eb8541 100644 --- a/src/Kris/LaravelFormBuilder/Fields/CheckableType.php +++ b/src/Kris/LaravelFormBuilder/Fields/CheckboxType.php @@ -2,7 +2,7 @@ namespace Kris\LaravelFormBuilder\Fields; -class CheckableType extends FormField +class CheckboxType extends FormField { const DEFAULT_VALUE = 1; @@ -17,7 +17,7 @@ class CheckableType extends FormField */ protected function getTemplate() { - return $this->type; + return 'checkbox'; } /** diff --git a/src/Kris/LaravelFormBuilder/Fields/CheckboxesType.php b/src/Kris/LaravelFormBuilder/Fields/CheckboxesType.php new file mode 100644 index 0000000..9366dcd --- /dev/null +++ b/src/Kris/LaravelFormBuilder/Fields/CheckboxesType.php @@ -0,0 +1,22 @@ +modelKeys(); + } + + parent::setValue($value); + } +} diff --git a/src/Kris/LaravelFormBuilder/Fields/ChoiceType.php b/src/Kris/LaravelFormBuilder/Fields/ChoiceType.php deleted file mode 100644 index 6b85dc1..0000000 --- a/src/Kris/LaravelFormBuilder/Fields/ChoiceType.php +++ /dev/null @@ -1,173 +0,0 @@ -options['expanded']; - $multiple = $this->options['multiple']; - - if ($multiple) { - $this->options['attr']['multiple'] = true; - } - - if ($expanded && !$multiple) { - return $this->choiceType = 'radio'; - } - - if ($expanded && $multiple) { - return $this->choiceType = 'checkbox'; - } - - return $this->choiceType = 'select'; - } - - /** - * @inheritdoc - */ - protected function getDefaults() - { - return [ - 'choices' => null, - 'selected' => null, - 'expanded' => false, - 'multiple' => false, - 'choice_options' => [ - 'wrapper' => false, - 'is_child' => true - ] - ]; - } - - /** - * Create children depending on choice type. - * - * @return void - */ - protected function createChildren() - { - if (($data_override = $this->getOption('data_override')) && $data_override instanceof \Closure) { - $this->options['choices'] = $data_override($this->options['choices'], $this); - } - - $this->children = []; - $this->determineChoiceField(); - - $fieldType = $this->formHelper->getFieldType($this->choiceType); - - switch ($this->choiceType) { - case 'radio': - case 'checkbox': - $this->buildCheckableChildren($fieldType); - break; - default: - $this->buildSelect($fieldType); - break; - } - } - - /** - * Build checkable children fields from choice type. - * - * @param string $fieldType - * - * @return void - */ - protected function buildCheckableChildren($fieldType) - { - $multiple = $this->getOption('multiple') ? '[]' : ''; - - foreach ((array)$this->options['choices'] as $key => $choice) { - $id = str_replace('.', '_', $this->getNameKey()) . '_' . $key; - $options = $this->formHelper->mergeOptions( - $this->getOption('choice_options'), - [ - 'attr' => array_merge(['id' => $id], $this->options['option_attributes'][$key] ?? []), - 'label_attr' => ['for' => $id], - 'label' => $choice, - 'checked' => in_array($key, (array)$this->options[$this->valueProperty]), - 'value' => $key - ] - ); - $this->children[] = new $fieldType( - $this->name . $multiple, - $this->choiceType, - $this->parent, - $options - ); - } - } - - /** - * Build select field from choice. - * - * @param string $fieldType - */ - protected function buildSelect($fieldType) - { - $this->children[] = new $fieldType( - $this->name, - $this->choiceType, - $this->parent, - $this->formHelper->mergeOptions($this->options, ['is_child' => true]) - ); - } - - /** - * Creates default wrapper classes for the form element. - * - * @param array $options - * @return array - */ - protected function setDefaultClasses(array $options = []) - { - $defaults = parent::setDefaultClasses($options); - $choice_type = $this->determineChoiceField(); - - $wrapper_class = $this->formHelper->getConfig('defaults.' . $this->type . '.' . $choice_type . '_wrapper_class', ''); - if ($wrapper_class) { - $defaults['wrapper']['class'] = (isset($defaults['wrapper']['class']) ? $defaults['wrapper']['class'] . ' ' : '') . $wrapper_class; - } - - $choice_wrapper_class = $this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.wrapper_class', ''); - $choice_label_class = $this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.label_class', ''); - $choice_field_class = $this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.field_class', ''); - - if ($choice_wrapper_class) { - $defaults['choice_options']['wrapper']['class'] = $choice_wrapper_class; - } - if ($choice_label_class) { - $defaults['choice_options']['label_attr']['class'] = $choice_label_class; - } - if ($choice_field_class) { - $defaults['choice_options']['attr']['class'] = $choice_field_class; - } - - return $defaults; - } -} diff --git a/src/Kris/LaravelFormBuilder/Fields/DatalistType.php b/src/Kris/LaravelFormBuilder/Fields/DatalistType.php new file mode 100644 index 0000000..e24e827 --- /dev/null +++ b/src/Kris/LaravelFormBuilder/Fields/DatalistType.php @@ -0,0 +1,15 @@ + null, - 'query_builder' => null, - 'property' => 'name', - 'property_key' => null, - ]; - - return array_merge(parent::getDefaults(), $defaults); - } - - /** - * @inheritdoc - */ - protected function createChildren() - { - if ($this->getOption('choices')) { - return parent::createChildren(); - } - - $entity = $this->getOption('class'); - $queryBuilder = $this->getOption('query_builder'); - $key = $this->getOption('property_key'); - $value = $this->getOption('property'); - - if (!$entity || !class_exists($entity)) { - throw new \InvalidArgumentException(sprintf( - 'Please provide valid "class" option for entity field [%s] in form class [%s]', - $this->getRealName(), - get_class($this->parent) - )); - } - - $entity = new $entity(); - - if ($key === null) { - $key = $entity->getKeyName(); - } - - if ($queryBuilder instanceof \Closure) { - $data = $queryBuilder($entity, $this->parent); - } else { - $data = $entity; - } - - if ($value instanceof \Closure) { - $data = $this->get($data); - } else { - $data = $this->pluck($value, $key, $data); - } - - if ($data instanceof Collection) { - $data = $data->all(); - } - - if ($value instanceof \Closure) { - $part = []; - foreach ($data as $item) { - $part[$item->__get($key)] = $value($item); - } - - $data = $part; - } - - $this->options['choices'] = $data; - - return parent::createChildren(); - } - - /** - * Pluck data. - * - * @param string $value - * @param string $key - * @param mixed $data - * - * @return mixed - * */ - protected function pluck($value, $key, $data) - { - if (!is_object($data)) { - return $data; - } - - if (method_exists($data, 'pluck') || $data instanceof Model) { - //laravel 5.3.* - return $data->pluck($value, $key); - } elseif (method_exists($data, 'lists')) { - //laravel 5.2.* - return $data->lists($value, $key); - } - } - - protected function get($data) - { - if (!is_object($data)) { - return $data; - } - - if (method_exists($data, 'get') || $data instanceof Model) { - //laravel 5.3.* - return $data->get(); - } - } -} diff --git a/src/Kris/LaravelFormBuilder/Fields/FormField.php b/src/Kris/LaravelFormBuilder/Fields/FormField.php index fe173cb..48ea883 100644 --- a/src/Kris/LaravelFormBuilder/Fields/FormField.php +++ b/src/Kris/LaravelFormBuilder/Fields/FormField.php @@ -142,12 +142,7 @@ protected function setupValue() } if (($value === null || $value instanceof \Closure) && !$isChild) { - if ($this instanceof EntityType) { - $attributeName = $this->name; - } else { - $attributeName = $this->getOption('value_property', $this->name); - } - + $attributeName = $this->getOption('value_property', $this->name); $this->setValue($this->getModelValueAttribute($this->parent->getModel(), $attributeName)); } elseif (!$isChild) { $this->hasDefault = true; diff --git a/src/Kris/LaravelFormBuilder/Fields/RadiosType.php b/src/Kris/LaravelFormBuilder/Fields/RadiosType.php new file mode 100644 index 0000000..0c04eee --- /dev/null +++ b/src/Kris/LaravelFormBuilder/Fields/RadiosType.php @@ -0,0 +1,24 @@ + [], + 'option_attributes' => [], + 'selected' => null + ]; + } +} diff --git a/src/Kris/LaravelFormBuilder/Fields/SelectType.php b/src/Kris/LaravelFormBuilder/Fields/SelectType.php index 40f8271..75d00b6 100644 --- a/src/Kris/LaravelFormBuilder/Fields/SelectType.php +++ b/src/Kris/LaravelFormBuilder/Fields/SelectType.php @@ -1,6 +1,6 @@ 'ButtonGroupType', 'submit' => 'ButtonType', 'reset' => 'ButtonType', - 'radio' => 'CheckableType', - 'checkbox' => 'CheckableType', - 'choice' => 'ChoiceType', + 'radios' => 'RadiosType', + 'checkbox' => 'CheckboxType', + 'checkboxes' => 'CheckboxesType', 'form' => 'ChildFormType', - 'entity' => 'EntityType', + 'datalist' => 'DatalistType', 'collection' => 'CollectionType', 'repeated' => 'RepeatedType', - 'static' => 'StaticType' + 'static' => 'StaticType', ]; /** @@ -355,7 +355,7 @@ public function getBoolableFields(Form $form) { $fields = []; foreach ($form->getFields() as $name => $field) { - if ($field instanceof CheckableType && $field->getOption('value') == CheckableType::DEFAULT_VALUE) { + if ($field instanceof CheckboxType && $field->getOption('value') == CheckboxType::DEFAULT_VALUE) { $fields[] = $this->transformToDotSyntax($name); } } diff --git a/src/config/config.php b/src/config/config.php index a2d1df6..bb4c192 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -19,23 +19,19 @@ // 'label_class' => 'form-field-text-label', // 'field_class' => 'form-field-text-field', //] - //'radio' => [ - // 'choice_options' => [ - // 'wrapper' => ['class' => 'form-radio'], - // 'label' => ['class' => 'form-radio-label'], - // 'field' => ['class' => 'form-radio-field'], - //], ], // Templates + 'datalist' => 'laravel-form-builder::datalist', 'form' => 'laravel-form-builder::form', 'text' => 'laravel-form-builder::text', 'textarea' => 'laravel-form-builder::textarea', 'button' => 'laravel-form-builder::button', 'buttongroup' => 'laravel-form-builder::buttongroup', 'radio' => 'laravel-form-builder::radio', + 'radios' => 'laravel-form-builder::radios', 'checkbox' => 'laravel-form-builder::checkbox', + 'checkboxes' => 'laravel-form-builder::checkboxes', 'select' => 'laravel-form-builder::select', - 'choice' => 'laravel-form-builder::choice', 'repeated' => 'laravel-form-builder::repeated', 'child_form' => 'laravel-form-builder::child_form', 'collection' => 'laravel-form-builder::collection', diff --git a/src/views/checkboxes.php b/src/views/checkboxes.php new file mode 100644 index 0000000..534e3a5 --- /dev/null +++ b/src/views/checkboxes.php @@ -0,0 +1,32 @@ + + +
> + + + + + + + + +
+ $label): + $id = $name . '_' . $value; + ?> +
+ $id]); ?> + +
+ +
+ + + + + + + + +
+ + diff --git a/src/views/choice.php b/src/views/choice.php deleted file mode 100644 index 612e7ee..0000000 --- a/src/views/choice.php +++ /dev/null @@ -1,27 +0,0 @@ - - -
> - - - - - - - - - - render($options['choice_options'], true, true, false) ?> - - - - - - - - - - - -
- - diff --git a/src/views/datalist.php b/src/views/datalist.php new file mode 100644 index 0000000..7ea70c9 --- /dev/null +++ b/src/views/datalist.php @@ -0,0 +1,7 @@ + + $label): ?> + + + diff --git a/src/views/radios.php b/src/views/radios.php new file mode 100644 index 0000000..8ca2a8b --- /dev/null +++ b/src/views/radios.php @@ -0,0 +1,32 @@ + + +
> + + + + + + + + +
+ $label): + $id = $name . '_' . $value; + ?> +
+ $id]); ?> + +
+ +
+ + + + + + + + +
+ + diff --git a/tests/Fields/CheckableTypeTest.php b/tests/Fields/CheckableTypeTest.php index da1d9a7..8d5bcde 100644 --- a/tests/Fields/CheckableTypeTest.php +++ b/tests/Fields/CheckableTypeTest.php @@ -1,8 +1,8 @@ plainForm, $options); + $checkable = new CheckboxType('test', 'checkbox', $this->plainForm, $options); $checkable->render(); @@ -75,7 +75,7 @@ public function it_creates_radio_field(): void $expectedOptions += $defaultOptions; - $checkable = new CheckableType('test', 'radio', $this->plainForm, $options); + $checkable = new CheckboxType('test', 'radio', $this->plainForm, $options); $checkable->render(); @@ -91,7 +91,7 @@ public function it_handles_values(): void 'value' => $expectedValue, ]; $this->plainForm->setModel(null); - $checkable = new CheckableType('test', 'checkbox', $this->plainForm, $options); + $checkable = new CheckboxType('test', 'checkbox', $this->plainForm, $options); $this->assertSame($expectedValue, $checkable->getOption('value')); } @@ -103,9 +103,9 @@ public function it_handles_checked(): void 'checked' => true, ]; $this->plainForm->setModel(null); - $checkable = new CheckableType('test', 'checkbox', $this->plainForm, $options); + $checkable = new CheckboxType('test', 'checkbox', $this->plainForm, $options); $this->assertTrue($checkable->getValue()); $this->assertTrue($checkable->getOption('checked')); } -} \ No newline at end of file +} diff --git a/tests/Fields/ChoiceTypeTest.php b/tests/Fields/ChoiceTypeTest.php deleted file mode 100644 index 4bd1094..0000000 --- a/tests/Fields/ChoiceTypeTest.php +++ /dev/null @@ -1,109 +0,0 @@ - ['class' => 'choice-class'], - 'choices' => ['yes' => 'Yes', 'no' => 'No'], - 'selected' => 'yes' - ]; - - $choice = new ChoiceType('some_choice', 'choice', $this->plainForm, $options); - - $choice->render(); - - $this->assertEquals(1, count($choice->getChildren())); - - $this->assertEquals('yes', $choice->getOption('selected')); - } - - /** @test */ - public function it_creates_choice_as_checkbox_list() - { - $options = [ - 'attr' => ['class' => 'choice-class-something'], - 'choices' => [1 => 'monday', 2 => 'tuesday'], - 'selected' => 'tuesday', - 'multiple' => true, - 'expanded' => true - ]; - - $choice = new ChoiceType('some_choice', 'choice', $this->plainForm, $options); - - $choice->render(); - - $this->assertEquals(2, count($choice->getChildren())); - - $this->assertEquals('tuesday', $choice->getOption('selected')); - - $this->assertContainsOnlyInstancesOf('Kris\LaravelFormBuilder\Fields\CheckableType', $choice->getChildren()); - } - - /** @test */ - public function it_creates_choice_as_radio_buttons() - { - $options = [ - 'attr' => ['class' => 'choice-class-something'], - 'choices' => [1 => 'yes', 2 => 'no'], - 'selected' => 'no', - 'expanded' => true - ]; - - $choice = new ChoiceType('some_choice', 'choice', $this->plainForm, $options); - - $choice->render(); - - $this->assertEquals(2, count($choice->getChildren())); - - $this->assertInstanceOf( - 'Kris\LaravelFormBuilder\Fields\CheckableType', - $choice->getChild(1) - ); - - $this->assertEquals('no', $choice->getOption('selected')); - - $this->assertContainsOnlyInstancesOf('Kris\LaravelFormBuilder\Fields\CheckableType', $choice->getChildren()); - } - - /** @test */ - public function it_sets_proper_name_for_multiple() - { - $this->plainForm->add('users', 'select', [ - 'choices' => [1 => 'user1', 2 => 'user2'], - 'attr' => [ - 'multiple' => 'multple' - ] - ]); - - $this->plainForm->renderForm(); - - $this->assertEquals('users[]', $this->plainForm->users->getName()); - } - - /** @test */ - public function it_can_override_choices() - { - $options = [ - 'choices' => ['yes' => 'Yes', 'no' => 'No'], - 'selected' => 'test', - 'data_override' => function ($choices, $field) { - $choices['test'] = 'test'; - - return $choices; - } - ]; - - $choice = new ChoiceType('some_choice', 'choice', $this->plainForm, $options); - - $choice->render(); - - $this->assertEquals(3, count($choice->getOption('choices'))); - - $this->assertEquals('test', $choice->getOption('selected')); - } -} diff --git a/tests/Fields/CollectionTypeTest.php b/tests/Fields/CollectionTypeTest.php index f52c27f..06a6b77 100644 --- a/tests/Fields/CollectionTypeTest.php +++ b/tests/Fields/CollectionTypeTest.php @@ -84,7 +84,7 @@ public function it_creates_collection_with_child_form() $form = clone $this->plainForm; $form->add('name', 'text') - ->add('gender', 'choice', [ + ->add('gender', 'select', [ 'choices' => ['m' => 'male', 'f' => 'female'] ]) ->add('published', 'checkbox'); diff --git a/tests/Fields/EntityTypeTest.php b/tests/Fields/EntityTypeTest.php deleted file mode 100644 index e47de97..0000000 --- a/tests/Fields/EntityTypeTest.php +++ /dev/null @@ -1,103 +0,0 @@ - 'Yes', 'no' => 'No']; - $options = [ - 'attr' => ['class' => 'choice-class'], - 'choices' => $choices, - 'selected' => 'yes' - ]; - - $choice = new EntityType('some_entity', 'entity', $this->plainForm, $options); - $choice->render(); - - $this->assertEquals(1, count($choice->getChildren())); - $this->assertEquals($choices, $choice->getOption('choices')); - $this->assertEquals('yes', $choice->getOption('selected')); - } - - /** @test */ - public function it_uses_passed_class_model_to_fetch_all() - { - $mdl = new DummyModel(); - $options = [ - 'class' => 'DummyModel' - ]; - - $choice = new EntityType('entity_choice', 'entity', $this->plainForm, $options); - $choice->render(); - - $expected = [ - 1 => 'English', - 2 => 'French', - 3 => 'Serbian' - ];; - - $this->assertEquals($expected, $choice->getOption('choices')); - } - - /** - * @test - */ - public function it_throws_an_exception_if_model_class_not_provided() - { - $this->expectException(\InvalidArgumentException::class); - - $options = []; - - $choice = new EntityType('entity_choice', 'entity', $this->plainForm, $options); - } - - /** @test */ - public function it_uses_query_builder_to_filter_choices() - { - $mdl = new DummyModel(); - $options = [ - 'class' => 'DummyModel', - 'property' => 'short_name', - 'query_builder' => function (DummyModel $model) { - return $model->getData()->filter(function($val) { - return $val['id'] > 1; - }); - } - ]; - - $choice = new EntityType('entity_choice', 'entity', $this->plainForm, $options); - $choice->render(); - - $expected = [ - 2 => 'Fr', - 3 => 'Rs' - ]; - - $this->assertEquals($expected, $choice->getOption('choices')); - } - - /** @test */ - public function options_are_passed_to_the_children() - { - $mdl = new DummyModel(); - $options = [ - 'class' => 'DummyModel' - ]; - - $choice = new EntityType('entity_choice', 'entity', $this->plainForm, $options); - $choice->setOption('attr.data-key', 'value'); - - $field = $choice->render(); - - $expectedField = '
- - -
'; - - $this->assertXmlStringEqualsXmlString(trim($field), $expectedField); - } -} diff --git a/tests/FormBuilderTestCase.php b/tests/FormBuilderTestCase.php index 694360c..97a7a6e 100644 --- a/tests/FormBuilderTestCase.php +++ b/tests/FormBuilderTestCase.php @@ -84,7 +84,7 @@ public function buildForm() { $this->add('name', 'text'); - $this->add('options', 'choice', [ + $this->add('options', 'checkboxes', [ 'choices' => ['a' => 'Aaa', 'b' => 'Bbb'], 'expanded' => true, 'multiple' => true, diff --git a/tests/FormHelperTest.php b/tests/FormHelperTest.php index 286ac10..19aad90 100644 --- a/tests/FormHelperTest.php +++ b/tests/FormHelperTest.php @@ -46,13 +46,12 @@ public function it_gets_proper_class_for_specific_field_type() $submit = $this->formHelper->getFieldType('submit'); $reset = $this->formHelper->getFieldType('reset'); $button = $this->formHelper->getFieldType('button'); - $radio = $this->formHelper->getFieldType('radio'); + $radios = $this->formHelper->getFieldType('radios'); $checkbox = $this->formHelper->getFieldType('checkbox'); - $choice = $this->formHelper->getFieldType('choice'); + $checkboxes = $this->formHelper->getFieldType('checkboxes'); $repeated = $this->formHelper->getFieldType('repeated'); $collection = $this->formHelper->getFieldType('collection'); $static = $this->formHelper->getFieldType('static'); - $entity = $this->formHelper->getFieldType('entity'); $className = $this->formHelper->getFieldType('Kris\\LaravelFormBuilder\\Fields\\InputType'); $this->assertEquals('Kris\\LaravelFormBuilder\\Fields\\InputType', $input); @@ -61,13 +60,12 @@ public function it_gets_proper_class_for_specific_field_type() $this->assertEquals('Kris\\LaravelFormBuilder\\Fields\\ButtonType', $submit); $this->assertEquals('Kris\\LaravelFormBuilder\\Fields\\ButtonType', $reset); $this->assertEquals('Kris\\LaravelFormBuilder\\Fields\\ButtonType', $button); - $this->assertEquals('Kris\\LaravelFormBuilder\\Fields\\CheckableType', $radio); - $this->assertEquals('Kris\\LaravelFormBuilder\\Fields\\CheckableType', $checkbox); - $this->assertEquals('Kris\\LaravelFormBuilder\\Fields\\ChoiceType', $choice); + $this->assertEquals('Kris\\LaravelFormBuilder\\Fields\\RadiosType', $radios); + $this->assertEquals('Kris\\LaravelFormBuilder\\Fields\\CheckboxType', $checkbox); + $this->assertEquals('Kris\\LaravelFormBuilder\\Fields\\CheckboxesType', $checkboxes); $this->assertEquals('Kris\\LaravelFormBuilder\\Fields\\RepeatedType', $repeated); $this->assertEquals('Kris\\LaravelFormBuilder\\Fields\\CollectionType', $collection); $this->assertEquals('Kris\\LaravelFormBuilder\\Fields\\StaticType', $static); - $this->assertEquals('Kris\\LaravelFormBuilder\\Fields\\EntityType', $entity); $this->assertEquals('Kris\\LaravelFormBuilder\\Fields\\InputType', $className); } diff --git a/tests/FormTest.php b/tests/FormTest.php index 5ec0656..baac9e8 100644 --- a/tests/FormTest.php +++ b/tests/FormTest.php @@ -39,7 +39,7 @@ public function it_adds_fields() ); $this->assertInstanceOf( - 'Kris\LaravelFormBuilder\Fields\CheckableType', + 'Kris\LaravelFormBuilder\Fields\CheckboxType', $this->plainForm->getField('remember') ); @@ -521,16 +521,14 @@ public function it_can_modify_existing_fields() $this->assertArrayNotHasKey('expanded', $this->plainForm->category->getOptions()); - $this->plainForm->modify('category', 'choice', [ - 'expanded' => true - ], true); + $this->plainForm->modify('category', 'checkboxes', [], true); $this->assertNotEquals( [ 1 => 'category-1', 2 => 'category-2'], $this->plainForm->category->getOption('choices') ); - $this->assertTrue($this->plainForm->category->getOption('expanded')); + $this->assertInstanceOf('Kris\LaravelFormBuilder\Fields\CheckboxesType', $this->plainForm->category); } @@ -795,33 +793,13 @@ public function it_can_use_model_property_to_set_value() 'model' => $this->model, ]); - $form->add('alias_accessor', 'choice', [ + $form->add('alias_accessor', 'select', [ 'value_property' => 'accessor', ]); $this->assertEquals($form->alias_accessor->getValue(), $this->model->accessor); } - /** @test */ - public function it_sets_entity_field_value_to_the_entity_model_value() - { - $dummyModel = new DummyModel(); - $dummyModel->id = 1; - - $this->model->dummy_model_id = $dummyModel->id; - - $form = $this->formBuilder - ->plain([ - 'model' => $this->model, - ]) - ->add('dummy_model_id', 'entity', [ - 'class' => DummyModel::class, - 'property' => 'name', - ]); - - $this->assertEquals($form->dummy_model_id->getValue(), $this->model->dummy_model_id); - } - /** @test */ public function it_reads_configuration_properly() { @@ -1202,29 +1180,13 @@ public function it_add_option_attributes_properly() 'choices' => $choices, 'option_attributes' => $optionAttributes ]) - ->add('languages_choice_select', 'choice', [ + ->add('languages_checkboxes', 'checkboxes', [ 'choices' => $choices, 'option_attributes' => $optionAttributes, - 'expanded' => false, - 'multiple' => false ]) - ->add('languages_choice_select_multiple', 'choice', [ + ->add('languages_radios', 'radios', [ 'choices' => $choices, 'option_attributes' => $optionAttributes, - 'expanded' => false, - 'multiple' => true - ]) - ->add('languages_choice_checkbox', 'choice', [ - 'choices' => $choices, - 'option_attributes' => $optionAttributes, - 'expanded' => true, - 'multiple' => true - ]) - ->add('languages_choice_radio', 'choice', [ - 'choices' => $choices, - 'option_attributes' => $optionAttributes, - 'expanded' => true, - 'multiple' => false ]); $formView = $form->renderForm(); @@ -1232,9 +1194,11 @@ public function it_add_option_attributes_properly() $this->assertStringContainsString('