0

最初 : 長いメッセージで申し訳ありません。

Fuel を学習しようとしていますが、Fieldset クラスと Orm に問題があります。データベースによると、自動生成されたフォームを取得するために、ORM を拡張するモデルを作成しました。

私のモデル

class Model_Product extends \Orm\Model
{
    protected static $_table_name = 'products';

    protected static $_properties = array(
        'id' => array(
            'data_type' => 'int'
        ),
        'name' => array(
            'data_type' => 'varchar',
            'label' => 'Name',
            'validation' => array(
                'required', 'trim', 'max_length'=>array(30), 'min_length'=>array(3)
            ),
        ),
        'description' => array(
            'data_type' => 'varchar',
            'label' => 'Description',
            'validation' => array(
                'max_length' => array(290)
            ),
            'form' => array(
                'type' => 'textarea'
            ),
        ),
        'price' => array(
            'data_type' => 'integer',
            'label' => 'Price',
            'validation' => array(
                'required', 'trim', 'valid_string' => array('numeric','dots')
            ),
        ),
        'pic' => array(
            'data_type' => 'varchar',
            'label' => 'Path to the pic',
            'validation' => array(
                'required', 'trim'
            ),
        ),
        'registered' => array(
            'data_type' => 'date',
            'label' => 'Registration date',
            'validation' => array(
                'required', 'trim'
            ) //'valid_string' => array('numeric','dashes')
        ),
    );
} //end of class Model_Product

次に、フォームを検証するコントローラーを作成します。

コントローラーからの私の機能

function action_add() 
{ 
    $fieldset = Fieldset::forge('add_product')->add_model('Model_Product')->repopulate();

    $form = $fieldset->form();

    $form->add('submit', '', array('type' => 'button', 'value' => 'Add item', 'class' => 'button-link' ));

    $validation = $fieldset->Validation();

    if($validation->run() === true) 
    { 
        $fields = $fieldset->validated();

        //create a new Product, with validated fields 
        $product = new Model_Product;

        $product->name = $fields['name']; 
        $product->description = $fields['description']; 
        $product->price = $fields['price'];
        $product->pic = $fields['pic']; 
        $product->registered = $fields['registered'];

        try 
        { 
            //if the product is successfully inserted in the database
            if($product->save()) 
            { 
                Session::set_flash('success', 'Product successfully added !');
                \Response::redirect('products/product_details/'.$product->id); 
            }
        } 
        catch(Exception $e) 
        { 
            Session::set_flash('error', 'Unable to save the product into the database !'.$e->getMessage()); 
        }
    } 
    //If the validation doesn't pass 
    else 
    { 
        Session::set_flash('error', $fieldset->show_errors()); 
    }

    $this->template->set('content', $form->build(), false);

} // end of method add()

私の最初の質問: 自動生成されたフォームを「美しく」するために、コントローラーからの関数のどこに、特定のクラスの「fieldset」タグを追加できますか? まあ言ってみれば

<fieldset class="add_product">

2 番目の質問: MySQL では decimal(5,2) に設定されているため、「価格」フィールドを正しく検証するにはどうすればよいですか? t パス (整数値の例: 42 でのみ機能しますが、10 進数の例: 42.35 では機能しません)。タイプを「integer」から「double」に変更しようとしましたが、うまくいきません。

私の問題に関する特定のドキュメントを指摘できる場合は、まだ読んでいない可能性があります.

ガブリエル

4

1 に答える 1

1

最初の質問に答えることができます 自動生成されたフォームを変更するfuel/core/config/form.phpには、ディレクトリにコピーして、fuel/app/configこのファイルを必要に応じて編集する必要があります。

于 2013-03-02T18:00:05.173 に答える