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

Add feature: MultiCheckbox (checkboxes with multiple choices). #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

RenoLooijmans
Copy link

No description provided.

Copy link
Member

@ClintWinter ClintWinter left a comment

Choose a reason for hiding this comment

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

Thanks for this! We have an internal version of this that I just haven't gotten around to adding back into the package.

If you wouldn't mind making a few tweaks I'm happy to merge this.

@@ -29,6 +29,7 @@
'select' => \Givebutter\LaravelCustomFields\FieldTypes\SelectFieldType::class,
'textarea' => \Givebutter\LaravelCustomFields\FieldTypes\TextareaFieldType::class,
'text' => \Givebutter\LaravelCustomFields\FieldTypes\TextFieldType::class,
'multicheckbox' => \Givebutter\LaravelCustomFields\FieldTypes\MultiCheckboxFieldType::class,
Copy link
Member

Choose a reason for hiding this comment

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

Rename "multicheckbox" to "multiselect". Checkbox is overly-specific and the input doesn't always have to be a checkbox. (i.e. <select multiple>)

Comment on lines +12 to +16
$this->validationPrefix.$this->field->id => array_filter([
$this->requiredRule($attributes['required']),
'array',
$attributes['required'] ? 'min:1' : null
]),
Copy link
Member

Choose a reason for hiding this comment

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

The required rule implies that there will be at least 1 value, so you can drop the min:1 ternary and the array_filter wrapper.

'required',
'string',
'max:255',
Rule::in($this->field->answers),
Copy link
Member

Choose a reason for hiding this comment

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

Let's also add distinct here just to make sure there are no duplicates.

Comment on lines +18 to +23
public function getValue(): mixed
{
return $this->formatValue(
$this->response->getAttribute($this::VALUE_FIELD)
);
}
Copy link
Member

Choose a reason for hiding this comment

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

Can drop this as it doesn't modify the behavior of the parent class.

Comment on lines +9 to +16
public function formatValue(mixed $value): mixed
{
if (! is_array($value)) {
return [$value];
}

return $value;
}
Copy link
Member

Choose a reason for hiding this comment

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

Can drop this too. I think it's overly cautious to not trust the data in our DB to not be an array as that's the format we require in the validation.

Comment on lines +27 to +41
$answers = $this->response->field->answers;
$values = $this->response->value;
$list = [];

if(! is_array($values)) {
$values = [$values];
}

foreach ($values as $value) {
if (isset($answers[$value])) {
$list[] = $answers[$value];
}
}

return implode(', ', $list);
Copy link
Member

Choose a reason for hiding this comment

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

I think all we need is a guard clause for null because as I said above it'll either be an array or null. We also shouldn't need to check isset on the available answers. Try this:

if (is_null($this->getValue())) {
    return '';
}

return Arr::join($this->getValue(), ', ');

@RenoLooijmans
Copy link
Author

Hi Clint, thanks for your reply and sorry for my late answer. I'm happy to make these changes, though I'm interested in your internal version as well. If it's better I don't see why my pull request should be added back into the package :-)

I'm on holiday the next weeks, but I'll be sure to make these adjustments when I am back. Thanks again.

@ClintWinter
Copy link
Member

The versions are very close other than the few comments I left, so I was trying to be nice and allow you to make a successful contribution rather than undercut you.

If you don't care, we can add the internal version instead, but you may end up making the adjustments before that happens 😆

Whichever comes first!

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.

2 participants