yii架構表單模型以數組形式提交表單步驟詳解

來源:互聯網
上載者:User
這次給大家帶來yii架構表單模型以數組形式提交表單步驟詳解,yii架構表單模型以數組形式提交表單的注意事項有哪些,下面就是實戰案例,一起來看一下。

按Yii文檔裡的描述,Yii在處理表單的一般過程是:
建立表單對應的模型類,設定欄位驗證規則
建立表單提交對應的action,處理提交的內容
在視圖中建立表單form
在剛剛的一個小項目裡,想使用ajax提交表單資訊並驗證儲存,又不想用隱藏iframe來做無重新整理提交,並且action中能夠用到模型類的校正方法,就想到使用表單數組提交的方式。
例子,form代碼:

<form action='' method='post' name='form_test'>    <input type='text' name='arr[]' value='1'>    <input type='text' name='arr[]' value='2'>    <input type='text' name='arr[]' value='3'></form>

提交後可以直接使用 $_POST['arr'] 來擷取提交的資料,$_POST['arr'] 為:

Array(    [0] => a    [1] => b    [2] => c)

同理,如果使用以下form提交:

<form action='' method='post' name='form_test'>    <input type='text' name='arr[3]' value='a'>    <input type='text' name='arr[6]' value='b'>    <input type='text' name='arr[8]' value='c'></form>$_POST['arr']Array(    [3] => a    [6] => b    [8] => c)

當然也能提交二維數組:

<form action='http://127.0.0.1/zhaobolu/test.php' method='post' name='form_test'>    <input type='text' name='arr[][name1]' value='a'>    <input type='text' name='arr[][name2]' value='b'>    <input type='text' name='arr[][name3]' value='c'></form>$_POST['arr'] 為:Array(    [0] => Array        (            [name1] => a        )    [1] => Array        (            [name2] => b        )    [2] => Array        (            [name3] => c        ))

這裡有一個問題,如果不設定第一個子數組的key,在產生數組時會將每個值順序在arr中添加,如果想將資訊儲存在一個array中,添加一個key值即可,如下:

<form action='http://127.0.0.1/zhaobolu/test.php' method='post' name='form_test'>    <input type='text' name='arr[a][name1]' value='a1'>    <input type='text' name='arr[a][value1]' value='a2'>    <input type='text' name='arr[b][name2]' value='b1'>    <input type='text' name='arr[b][value2]' value='b2'></form> $_POST['arr'] 為:Array(    [a] => Array        (            [name1] => a1            [value1] => a2        )    [b] => Array        (            [name2] => b1            [value2] => b2        ))

用ajax提交表單並且用yii表單模型驗證的樣本,首先是模型類部分,只有最簡單的校正方法:

<?phpclass LandingForm extends CFormModel{    public $landing_title;    public $landing_content;    public $landing_position;    public function rules()    {        return array(            array('landing_title, landing_content', 'required'),            array('landing_position', 'default', 'value'=>''),        );    }}

模型類在設定參數校正的方法時,需要對每一個public參數都設定規則,如果有未設定規則的參數,在用$_POST中的表單值為模型賦值後,未設定規則的參數值將為空白
action中擷取表單提交的參數並且校正:

$model = new LandingForm;$model->attributes = $_POST['form'];if($model->validate()){    $info = $model->attributes;    ... }

最後是前端提交表單部分的代碼,用的jquery:

var info = new Object();info = { 'form[landing_title]': landing_title,        'form[landing_content]': landing_content,        'form[landing_position]': landing_position,        };var url = "...";$.post(url, info, function(rst){    ... });

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

yii架構通過控制台命令建立定時任務步驟詳解

PHP加速器eAccelerator配置使用步驟詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.