Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix/multiple select #151

Merged
merged 2 commits into from
Aug 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 41 additions & 42 deletions src/Kris/LaravelFormBuilder/Fields/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ protected function setupValue()
*/
abstract protected function getTemplate();

/**
* @return string
*/
protected function getViewTemplate()
{
return $this->getOption('template', $this->template);
}

/**
* @param array $options
* @param bool $showLabel
Expand All @@ -130,30 +138,20 @@ abstract protected function getTemplate();
*/
public function render(array $options = [], $showLabel = true, $showField = true, $showError = true)
{
$val = null;
$value = array_get($options, $this->valueProperty);
$defaultValue = array_get($options, $this->defaultValueProperty);
$this->prepareOptions($options);
$value = $this->getValue();
$defaultValue = $this->getDefaultValue();

if ($showField) {
$this->rendered = true;
}

// Check if default value is passed to render function from view.
// If it is, we save it to a variable and then override it before
// rendering the view
if ($value) {
$val = $value;
} elseif ($defaultValue && !$this->getOption($this->valueProperty)) {
$val = $defaultValue;
}

$options = $this->prepareOptions($options);

if ($val) {
$options[$this->valueProperty] = $val;
// Override default value with value
if (!$value && $defaultValue) {
$this->setOption($this->valueProperty, $defaultValue);
}

if (!$this->needsLabel($options)) {
if (!$this->needsLabel()) {
$showLabel = false;
}

Expand All @@ -162,12 +160,12 @@ public function render(array $options = [], $showLabel = true, $showField = true
}

return $this->formHelper->getView()->make(
$this->template,
$this->getViewTemplate(),
[
'name' => $this->name,
'nameKey' => $this->getNameKey(),
'type' => $this->type,
'options' => $options,
'options' => $this->options,
'showLabel' => $showLabel,
'showField' => $showField,
'showError' => $showError
Expand Down Expand Up @@ -214,46 +212,48 @@ protected function transformKey($key)
protected function prepareOptions(array $options = [])
{
$helper = $this->formHelper;
$this->options = $helper->mergeOptions($this->options, $options);

if (array_get($this->options, 'template') !== null) {
$this->template = array_pull($this->options, 'template');
if ($this->getOption('attr.multiple') && !$this->getOption('tmp.multipleBracesSet')) {
$this->name = $this->name.'[]';
$this->setOption('tmp.multipleBracesSet', true);
}

$options = $this->options = $helper->mergeOptions($this->options, $options);

if ($this->parent->haveErrorsEnabled()) {
$this->addErrorClass($options);
}

if ($this->getOption('attr.multiple')) {
$this->name = $this->name.'[]';
}

if ($this->getOption('required') === true) {
$options['label_attr']['class'] .= ' ' . $this->formHelper
->getConfig('defaults.required_class', 'required');
$options['attr']['required'] = 'required';
$lblClass = $this->getOption('label_attr.class', '');
$requiredClass = $helper->getConfig('defaults.required_class', 'required');
if (!str_contains($lblClass, $requiredClass)) {
$lblClass .= ' ' . $requiredClass;
$this->setOption('label_attr.class', $lblClass);
$this->setOption('attr.required', 'required');
}
}

if ($this->parent->clientValidationEnabled() && $rules = $this->getOption('rules')) {
$rulesParser = new RulesParser($this);
$options['attr'] += $rulesParser->parse($rules);
$attrs = $this->getOption('attr') + $rulesParser->parse($rules);
$this->setOption('attr', $attrs);
}

$options['wrapperAttrs'] = $helper->prepareAttributes($options['wrapper']);
$options['errorAttrs'] = $helper->prepareAttributes($options['errors']);
$this->setOption('wrapperAttrs', $helper->prepareAttributes($this->getOption('wrapper')));
$this->setOption('errorAttrs', $helper->prepareAttributes($this->getOption('errors')));

if ($options['is_child']) {
$options['labelAttrs'] = $helper->prepareAttributes($options['label_attr']);
if ($this->getOption('is_child')) {
$this->setOption('labelAttrs', $helper->prepareAttributes($this->getOption('label_attr')));
}

if ($options['help_block']['text']) {
$options['help_block']['helpBlockAttrs'] = $helper->prepareAttributes(
$options['help_block']['attr']
if ($this->getOption('help_block.text')) {
$this->setOption(
'help_block.helpBlockAttrs',
$helper->prepareAttributes($this->getOption('help_block.attr'))
);
}

return $options;
return $this->options;
}

/**
Expand Down Expand Up @@ -493,13 +493,12 @@ protected function setDefaultOptions(array $options = [])
/**
* Check if fields needs label
*
* @param array $options
* @return bool
*/
protected function needsLabel(array $options = [])
protected function needsLabel()
{
// If field is <select> and child of choice, we don't need label for it
$isChildSelect = $this->type == 'select' && array_get($options, 'is_child') === true;
$isChildSelect = $this->type == 'select' && $this->getOption('is_child') === true;

if ($this->type == 'hidden' || $isChildSelect) {
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/Kris/LaravelFormBuilder/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -832,12 +832,12 @@ protected function checkIfNamedForm()
*/
protected function setupFieldOptions($name, &$options)
{
$options['real_name'] = $name;

if (!$this->getName()) {
return;
}

$options['real_name'] = $name;

if (!isset($options['label'])) {
$options['label'] = $this->formHelper->formatLabel($name);
}
Expand Down
10 changes: 8 additions & 2 deletions tests/Fields/ButtonTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function it_can_change_template_with_options()

$expectedOptions['wrapper'] = false;
$expectedOptions['wrapperAttrs'] = null;
$expectedOptions['template'] = 'laravel-form-builder::text';

$expectedViewData = [
'name' => 'some_submit',
Expand All @@ -79,8 +80,13 @@ public function it_can_change_template_with_options()
'showError' => true
];

$button = new ButtonType('some_submit', 'submit', $this->plainForm);
$button = new ButtonType('some_submit', 'submit', $this->plainForm, [
'template' => 'laravel-form-builder::text'
]);

$button->render();
$renderedView = $button->render();

$this->assertEquals($expectedOptions, $button->getOptions());
$this->assertContains('<input', $renderedView);
}
}
15 changes: 15 additions & 0 deletions tests/Fields/ChoiceTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,19 @@ public function it_creates_choice_as_radio_buttons()

$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());
}
}
1 change: 1 addition & 0 deletions tests/Fields/InputTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function explicit_value_overrides_default_values()
];

$input = new InputType('test', 'text', $this->plainForm, $options);
$data = $input->render();

$this->assertEquals(500, $input->getValue());
$this->assertEquals(100, $input->getDefaultValue());
Expand Down