Need Help In Creating A Form Having Multiple Text Fields With Same Name


I’m new to Yii.

I’m working on yii project where i can create a food menu, which has multiple food categories and each category has multiple Dishes. Further these dishes have multiple prices (like small, medium and large).

I’ve created 4 tables for this.







Menu table consists of menu_id, name, desc

Menu_categories table consists of category_id, menu_id,name,desc

Menu_dish consists of dish_id, category_id, name, desc

Dish_prices table consists of dish_id, size, prize

I’ve done adding dishes but i’m having trouble with dish prices. I trying to add all dishes’s pprice belonging to a menu at a time and eadch dish has multiple prices.

Please help me by providing the view and controller which can handle my requirement.

you can add field with name like dish_price[]

<input type=‘text’ name=‘dish_price[]’ />

I’ve tried but it did not work.

I’m having multiple dishes and multiple prices for each dish.

Why don’t you use CRUD made available by GII ?

I’ve used CRUD but it generates text fields for only single dish not multiple.

I think you have two way to solve your problem:

  1. You have textboxes for one dish at time (description and price), so also a "save and new dish" button to insert again a dish and a "finished" button to terminate operation (simply to close the operation, because data has been saved before);

  2. You have some rows with textboxes for insert many dish at time (for example 3 or 4), and a "+" button to add new textboxes (using jquery for example) and then an unique submit to save data;

But both solution are client side, Yii has little to do.

If I use Jquery for inserting many dishes at a time then how can i save those multiple text fields with same names, can u show me sample code.

Using array field, for example:

Client side:

<?php for($k=0;$k<5;$k++) { ?>

       <input type="textbox" name="dish_name[]" /><input type="textbox" name="dish_price[]" />

       <br />

<?php } ?>

Server side:

$arrDishName = Yii::app()->request->getParam("dish_name", array());

$arrDishPrice = Yii::app()->request->getParam("dish_price", array());



       $name = $arrDishName[$k];

       $price = $arrDishPrice[$k];


can we use this if we are submiting the form using POST action. If no, then how can we handle them using POST action.

Sorry, i don’t understand your problem.

what should we use in form action attribute either GET or POST

getParam support either GET and POST params.

Thank you…

while using the name="dish_name[] validator will not work properly i.e. you cannot use the custom message for the validator.