We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

render multiple checkboxes from model

Hi,

how can i render multiple checkboxes from a model? Select is working, but not checkboxes.

I started with this:

  $this->add(
        new Check(
            "country",
            Users::find(),
            [
                "using" => [
                    "id",
                    "country",
                ],
                "order" => "country",
                "group" => "id"

            ]
        )
    );

And get this message:

Wrong number of parameters

Volt

{{ form.render("country") }}

Thx

Hi @Stefjoe well unfortunately we don't have a "multiCheckbox" element but you can make a custom one or use an foreach


foreach(Users::find([...] as $user) {
echo (new Check('users[]'))->setDefault($user->getId())->render();
}

I prefer make a custom element but you decide Good luck



59.9k

Hi @Degiovanni Emilio

thx for your help, i tried this in my SearchForm.php:

    $country = $this->modelsManager->createBuilder()
        ->from('Vokuro\Models\Users')
        ->columns('id,country')
        ->orderBy('Vokuro\Models\Users.country')
        ->getQuery()
        ->execute();

    foreach ($country as $nation){
        $countryCheck = new Check("country", array(
            'value' => $nation->country,
            'checked' => 'checked',
            'name' => 'searchCriteria[]'
        ));
        $countryCheck -> setLabel('Land');
        $this->add($countryCheck);
    }

Now i get only the first record.

Thx again :-)

Hi again if you check the add method set elements array by name. You must use


$countryCheck = new Check("country-" . $nation->id, array(
            'value' => $nation->country,
            'checked' => 'checked',
            'name' => 'searchCriteria[]'
        ));

however check before foreach if you have a lot of countries with

var_dump(count($country));

Check that and write. Bye bye



59.9k

Hello @Degiovanni Emilio,

Now we got it :-)

SearchForm:

    $country = $this->modelsManager->createBuilder()
        ->from('Vokuro\Models\Users')
        ->columns('id,country')
        ->orderBy('Vokuro\Models\Users.country')
        ->getQuery()
        ->execute();
    $qty = count($country);

    $this->view->countries = $qty;

    $i = 1;
    foreach ($country as $nation){

        $this->view->countries = $qty;
        $countryCheck = new Check("country-" . $i, array(
            'value' => $nation->country,
            'checked' => 'checked',
            'name' => 'searchCriteria[]'
        ));

        $countryCheck -> setLabel('Land');
        $this->add($countryCheck);

        $i++;
    }

Volt:

{% for i in 1..countries %}
    {{ form.render("country-"~i) }}
{% endfor %}

Maybe you have a better solution, but this is working.

Thx for your help.

Bye Stefan

Well I don't know if it's better or worse, but it's different. You can use loop context in volt

    {% for country in countries %} 
        {{ form.render("coutry" ~ loop.index) }}
    {% endfor %}

Or render by element name

    {% for country in countries %}  {#coutries must be a resultset not a count #}
        {{ form.render(country.getName()) }}
    {% endfor %}

I prefer the last one Well all ways are right, use what you like Bye bye



59.9k
Accepted
answer

Hi,

i changed it this way:

Form

    $country = $this->modelsManager->createBuilder()
        ->from('Vokuro\Models\Users')
        ->columns('id,country,flags')
        ->orderBy('Vokuro\Models\Users.country')
        ->groupBy('Vokuro\Models\Users.country')
        ->getQuery()
        ->execute();

    $this->view->countries = $country;

    $i = 1;
    foreach ($country as $nation){
        $countryCheck = new Check("country-" . $i, array(
            'value' => $nation->country,
            'name' => 'searchCriteria[]'
        ));
        $this->add($countryCheck);
        $i++;
    }

Volt

     {% for country in countries %}
                                <div class="col-xs-1">
                                    <label class="btn btn-link btnFlagPadding image-checkbox" data-toggle="tooltip" data-placement="top" title="{{ country.country }}">
                                            {{ image(country.flags) }}
                                            {{ form.render("country-" ~ loop.index) }}
                                    </label>
                                </div>
       {% endfor %}

Think that is much better now :-)

Thank you for your help.

Rgds Stefan

Hi !

i have a same question. this is my form

foreach ($postcategorydata as $data) { $postcategory = new check('postcategory'.$data->id,array( "id" => "checkbox".$data->id, "value" => $data->name )); $this->add($postcategory); }