-
Notifications
You must be signed in to change notification settings - Fork 35
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Givebutter\LaravelCustomFields\FieldTypes; | ||
|
||
use Illuminate\Validation\Rule; | ||
|
||
class MultiCheckboxFieldType extends FieldType | ||
{ | ||
public function validationRules(array $attributes): array | ||
{ | ||
return [ | ||
$this->validationPrefix.$this->field->id => array_filter([ | ||
$this->requiredRule($attributes['required']), | ||
'array', | ||
$attributes['required'] ? 'min:1' : null | ||
]), | ||
Comment on lines
+12
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
$this->validationPrefix.$this->field->id.'.*' => [ | ||
'required', | ||
'string', | ||
'max:255', | ||
Rule::in($this->field->answers), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also add |
||
], | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Givebutter\LaravelCustomFields\ResponseTypes; | ||
|
||
class MultiCheckboxResponseType extends ResponseType | ||
{ | ||
const VALUE_FIELD = 'value_json'; | ||
|
||
public function formatValue(mixed $value): mixed | ||
{ | ||
if (! is_array($value)) { | ||
return [$value]; | ||
} | ||
|
||
return $value; | ||
} | ||
Comment on lines
+9
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
public function getValue(): mixed | ||
{ | ||
return $this->formatValue( | ||
$this->response->getAttribute($this::VALUE_FIELD) | ||
); | ||
} | ||
Comment on lines
+18
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
public function getValueFriendly(): mixed | ||
{ | ||
$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); | ||
Comment on lines
+27
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think all we need is a guard clause for if (is_null($this->getValue())) {
return '';
}
return Arr::join($this->getValue(), ', '); |
||
} | ||
} |
There was a problem hiding this comment.
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>
)