dqrq93879
dqrq93879
2016-07-17 15:55

从表单中获取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 doujia4759 5年前

    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.

    点赞 评论 复制链接分享

相关推荐