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

[WIP] Selectbox auto default value #183

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

[WIP] Selectbox auto default value #183

wants to merge 5 commits into from

Conversation

h4kuna
Copy link
Contributor

@h4kuna h4kuna commented Jun 13, 2018

  • new feature
  • BC break? probably no
  • doc PR: not need

If you create form with selectbox and render it. Nothing change and submit form, we get first value of items. This unite behavior if you create selectbox and call method getValue() then return first item. It does not matter the order of the called methods. You can see comit named "test".

Example

$form = new Form;
$select = $form->addSelect('foo')
	->setItems(['bar' => 'Bar', 'foo' => 'Foo']);

Old bahavior

Value null is out of range.

$select->getValue() // null

New behavior

$select->getValue() // bar

What do you think?

@dg
Copy link
Member

dg commented Jun 13, 2018

That's fine, I tried something like this.

But beware of that <select size=2> is not submitted with first value.

@h4kuna
Copy link
Contributor Author

h4kuna commented Jun 13, 2018

I thinking about one property for value sorted by priority like this:

class NoValue {
// probably it can be singleton :)
}

BaseControl

protected $value = [
    'value' => new NoValue,
    'default' => new NoValue,
    'prompt' => new NoValue,
    'auto' => new NoValue, // defined by input
];

public function setPrompt($value) 
{
$this->value['prompt'] = $value;
}

protected function setValue($value) 
{
...
}

public function setDefaultValue($value) 
{
...
}

public function getValue() 
{
    foreach ($this->value as $value) {
        if (!$value instanceof NoValue) {
            return $value;
        }
    } 
    return null;
}

This implementation is not for this PR.

@@ -26,6 +26,9 @@ class SelectBox extends ChoiceControl
/** @var mixed */
private $prompt = false;

/** @var bool */
private $lockAutoDefault = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LockAutoDefault looks weird to me.

@h4kuna
Copy link
Contributor Author

h4kuna commented Jun 14, 2018

It is little bit complicated, in render time.

Macro input

if i use

{input foo size => 2}

It is compiled

echo end($this->global->formsStack)["foo"]->getControl()->addAttributes(['size' => 2]);

I haven't access to the instance Selectbox. This i can resolve like:

echo end($this->global->formsStack)["foo"]->addAttributes(['size' => 2]);

Html select n: macro

<select n:name="foo" size="2"></select>

Know i value of attribute size in compile time?

<select size="2"<?php
		$_input = end($this->global->formsStack)["foo"];
		echo $_input->getControlPart()->addAttributes(array (
		'size' => NULL,
		))->attributes() ?>><?php echo $_input->getControl()->getHtml() ?></select>

@h4kuna
Copy link
Contributor Author

h4kuna commented Jun 21, 2018

I don't know how continue this PR.

Must macros works from template when set up size? If yes, i need help with n:macro and compile time.

@dg wrote anything same, could you paste your resolution or idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants