dqrq93879 2016-07-17 15:55
浏览 56
已采纳

从表单中获取select元素

I am beginner when it comes to Phalcon framework and his Volt engine but i would really like to learn it. I am following their documentation and i am currently on their INVO project.

And they have a database of products, and those products have like a type (5-Vegetables, 6-Fruit). So when i list all the products, it displays all the data about that product and it gives you the link to "edit" that data for that specific product. So when i click on "edit" i want to open like a html form that will contain already filled fields with data from that specific product.

This is the form class that they use to create a form for products:

<?php

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Select;
use Phalcon\Validation\Validator\Email;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Numericality;

class ProductsForm extends Form
{
    /**
     * Initialize the products form
     *
     * @param null $entity
     * @param array $options
     */
    public function initialize($entity = null, $options = array())
    {
        if (isset($options['edit'])) {
            $element = new Text("id");
            $this->add($element->setLabel("Id"));
        } else {
            $this->add(new Hidden("id"));
        }

        $name = new Text("name");
        $name->setLabel("Name");
        $name->setFilters(array('striptags', 'string'));
        $name->addValidators(
            array(
                new PresenceOf(
                    array(
                        'message' => 'Name is required'
                    )
                )
            )
        );
        $this->add($name);

        $type = new Select(
            'profilesId',
            ProductTypes::find(),
            array(
                'using' => array('id', 'name'),
                'useEmpty' => true,
                'emptyText' => '...',
                'emptyValue' => ''
            )
        );
        $this->add($type);

        $price = new Text("price");
        $price->setLabel("Price");
        $price->setFilters(array('float'));
        $price->addValidators(
            array(
                new PresenceOf(
                    array(
                        'message' => 'Price is required'
                    )
                ),
                new Numericality(
                    array(
                        'message' => 'Price is required'
                    )
                )
            )
        );
        $this->add($price);
    }
}

This is the action from the controller:

/**
 * Shows the view to "edit" an existing product
 */
public function editAction($id)
{
    if (!$this->request->isPost()) {
        $product = Products::findFirstById($id);
        if (!$product) {
            $this->flash->error("Product was not found");
            return $this->forward("products/index");
        }

        $this->view->form = new ProductsForm($product, array('edit' => true));
    }
}

And this is my edit.volt view file:

{{ form("products/whatever") }}

<h2>Search products</h2>

<fieldset>

    {% for element in form %}
        <div class="control-group">
            {{ element.label(['class': 'control-label']) }}
            <div class="controls">{{ element }}</div>
        </div>
    {% endfor %}

    <div class="control-group">
        {{ submit_button("Edit", "class": "btn btn-primary") }}
    </div>

</fieldset>

Now when i click on a specific product i get this: Edit screen

As you can see... everything except the select tag has been already filled and that works fine... its just that one select that doesn't get selected. :( I mean i can click the dropdown menu and it does show me the data... but i want it to be already selected like everything else is.

So my question is, can i add (and how to do it if its possible) like an if statement inside my edit.volt that will go like "if the next element is a select element, just select the value that is already in the database", like in that screen, if its bell pepper then 5-Vegetables should already be selected... then if you want you can change it and save it... but i just want it to already be selected.

I apologize for the long post.

  • 写回答

1条回答 默认 最新

  • doujia4759 2016-07-18 14:08
    关注

    If you want Phalcon to auto assign database values to your form, you need to make sure your form elements have the same name as your Products columns (~database columns).
    Otherwise Phalcon can't know how to map your Products values to their correct form elements.

    Controller:

    $product = Products::findFirstById($id);
    // ...
    $this->view->form = new ProductsForm($product, array('edit' => true));
    

    Form:

    $type = new Select(
        'product_type_id', // was previously "profilesId"
        ProductTypes::find(),
        array(
            'using' => array('id', 'name'),
            'useEmpty' => true,
            'emptyText' => '...',
            'emptyValue' => ''
        )
    );
    

    As you mentioned in the comments, your Products model doesn't have a profilesId column but instead a product_type_id column. Therefor you should rename your Select element from profilesId to product_type_id.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办