欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

yii框架表單模型使用及以數(shù)組形式提交表單數(shù)據(jù)示例

 更新時(shí)間:2014年04月30日 11:22:02   作者:  
這篇文章主要介紹了yii框架表單模型使用及以數(shù)組形式提交表單數(shù)據(jù)示例,需要的朋友可以參考下

按Yii文檔里的描述,Yii在處理表單的一般過程是:

創(chuàng)建表單對(duì)應(yīng)的模型類,設(shè)置字段驗(yàn)證規(guī)則
創(chuàng)建表單提交對(duì)應(yīng)的action,處理提交的內(nèi)容
在視圖中創(chuàng)建表單form
在剛剛的一個(gè)小項(xiàng)目里,想使用ajax提交表單信息并驗(yàn)證保存,又不想用隱藏iframe來做無刷新提交,并且action中能夠用到模型類的校驗(yàn)方法,就想到使用表單數(shù)組提交的方式,舉個(gè)例子:

form代碼:

復(fù)制代碼 代碼如下:

<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'] 來獲取提交的數(shù)據(jù),$_POST['arr'] 為:
復(fù)制代碼 代碼如下:

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

同理,如果使用以下form提交:
復(fù)制代碼 代碼如下:

<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
)


當(dāng)然也能提交二維數(shù)組:
復(fù)制代碼 代碼如下:

<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
        )
)

這里有一個(gè)問題,如果不設(shè)置第一個(gè)子數(shù)組的key,在生成數(shù)組時(shí)會(huì)將每個(gè)值順序在arr中添加,如果想將信息保存在一個(gè)array中,添加一個(gè)key值即可,如下:

復(fù)制代碼 代碼如下:

<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表單模型驗(yàn)證的示例,首先是模型類部分,只有最簡(jiǎn)單的校驗(yàn)方法:

復(fù)制代碼 代碼如下:

<?php
class 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'=>''),
        );
    }
}

發(fā)現(xiàn)個(gè)比較有意思的,就是模型類在設(shè)置參數(shù)校驗(yàn)的方法時(shí),需要對(duì)每一個(gè)public參數(shù)都設(shè)置規(guī)則,如果有未設(shè)置規(guī)則的參數(shù),在用$_POST中的表單值為模型賦值后,未設(shè)置規(guī)則的參數(shù)值將為空

action中獲取表單提交的參數(shù)并且校驗(yàn):

復(fù)制代碼 代碼如下:

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

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

復(fù)制代碼 代碼如下:

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){
    ...
});

相關(guān)文章

最新評(píng)論