mardi 20 décembre 2016

Creating Form with checkboxes in Phalcon

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">
        
        <div class="controls">
            
        </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.




Aucun commentaire:

Enregistrer un commentaire