Reputation: 345
I'm building a form which reads a list of keywords from a database. The model is simple: each user have different keywords associated in the database, so I don't know the number of them.
In the form I want to render all the user's associated keywords with a checkbox list, so the user can decide which keyword save in a special group. Of course I want to render the name of the keyword but I want to obtain the "id" of it.
I don't find any documentation of this. I just found the typical:
$keywords = new Check('keywords', array(
'value' => '1'
));
$keywords->setLabel('Keywords');
$this->add($keywords);
to put in the form, but it is useless. In the view I wrote
<div class="control-group">
{{ form.label('keywords', ['class': 'control-label']) }}
<div class="controls">
{{ form.render('keywords', ['class': 'form-control']) }}
</div>
</div>
And I see a checkbox (with value 1). I imagine the solution should be something like the SELECT (which I use in another form). Something like:
$idkeyword = new Select('keyword',
Keyword::find($string), [
"useEmpty" => true,
"emptyText" => "Por favor selecciona...",
"using" => ["idkeyword", "trackeable"],
]);
$idkeyword->setLabel('Keyword');
$idkeyword->addValidators(array(
new PresenceOf(array(
'message' => 'idkeyword requerida'
))
));
$this->add($idkeyword);
In the view I would like to have something like:
<input type="checkbox" name="chk_group" value="1" />Keyword 1<br />
<input type="checkbox" name="chk_group" value="2" />Keyword 2<br />
<input type="checkbox" name="chk_group" value="3" />Keyword 3<br />
When "Keyword X" is in the database and "X" is its id.
I would be glad to hear any help. I hope my question is well formulated. If not, I will accept all comments. Thanks.
Upvotes: 0
Views: 902
Reputation: 45
The Phalcon\Forms\Element\Check
is meant for a single checkbox, so if you want to use it for multiple checkboxes, you will have to write a loop:
// You should get these options from the database
$options = [1 => 'Keyword 1', 2 => 'Keyword 2', 3 => 'Keyword 3'];
foreach($options as $key => $value)
{
// Create a checkbox for each option
$keywords = new Check('keywords'.$key, [
'name' => 'chk_group',
'class' => 'form-control',
'value' => $value
]);
// Create a label for each option so the user can click on this
$keywords->setLabel($value);
$this->add($keywords);
}
then in the view:
{% for element in form %}
{{ element.label(['class': 'control-label']) }}
{{ element.render() }}
{% endfor %}
Upvotes: 1