I have a model with an attribute that holds a CSV string.
(The model is actually an ActiveRecord object but I guess this is not important. Correct me if I'm wrong.)
/**
* @property string $colors Can be something like "red" or "red,green,blue" or ""
*/
class Product extends Model {
}
And I have a form in which I'd like to display this attribute as a checkboxList so that the user can select the possible values with simple clicks instead of typing into a textInput.
Theoretically, it should look similar to this:
<?php $availableColors = ['red' => 'Red', 'green' => 'Green', 'blue' => 'Blue']; ?>
<?php $form = ActiveForm::begin([]); ?>
<?= $form->field($model, 'colors')->checkboxList($availableColors) ?>
<?php ActiveForm::end(); ?>
This does obviously not work since the field colors
would need to be an array. But in my model it is a string.
What would be a good way to achieve that? With JS or pseudo attributes? The colors
attribute must not be changed since it is already used in other contexts that shouldn't be modified.
Now I solved it with an extra model for the form. This seems to me a proper solution.
With this I can use the form that way:
Of course, now the controller has to use the inherited model class.
The solution deals also with the issue if no checkbox is selected. That is why
'none-value'
is introduced.CSV is a file format used for moving tabular data between programs that natively operate on incompatible formats. Using it as a model attribute is not very elegant (to say it nicely). In my opinion you should have started out storing your colors in an array.
That being said you can achieve converting the array data from the dropdown list to CSV using the
beforeValidate()
function in your model:You can override
beforeValidate
method in your model, toimplode
your colors array into string. In your view you can use following:I think this is a PHP question, but anyway you can use PHP explode for build the array you need. See here for more details and then user the array inside the checkboxList