0

朋友们,

我有一个包含几个项目的注册表单,这些项目的下拉列表似乎没有被表单提交选中。

风景:

 </div>
    <div class="row">
        <?php echo CHtml::activeLabel($model, 'Gjinia'); ?>
        <?php echo CHtml::dropDownList('sex', 0, $data = array(0 => 'Mashkull', 1 => 'Femer'))
        ?>
    </div>

    <div class="row">
        <?php echo CHtml::activeLabel($model, 'Arsimimi'); ?>
        <?php echo CHtml::dropDownList('education', 0, $data = array(0 => 'Ulet', 1 => 'Mesem', 2 => 'Larte')) ?>
    </div>

    <div class="row">
        <?php echo CHtml::activeLabel($model, 'Statusi Martesor'); ?>
        <?php echo CHtml::dropDownList('marital_status', 0, $data = array(0 => 'Beqar/e', 1 => 'I/E divorcuar', 2 => 'I/E martuar', 3 => 'I/E veje')) ?>
    </div>

验证规则:

public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, lastname, sex, education, 
                marital_status, employment, 
                dob, municipality, address, 
                cell_no, email, initialPassword, 
                repeatPassword', 'required'),
            array('sex, employment, municipality,', 'numerical', 'integerOnly' => true),
            array('email, initialPassword, repeatPassword, name, lastname', 'length', 'max' => 100),
            array('initialPassword, repeatPassword', 'required', 'on' => 'insert'),
            array('initialPassword, repeatPassword', 'length', 'min' => 6, 'max' => 40),
            array('initialPassword', 'compare', 'compareAttribute' => 'repeatPassword'),
        );
    }

提交表单后,当我在 $_POST['Auser'] 上执行 print_r 时,我得到以下信息:

Array ( [name] => Name [lastname] => Lname [address] => B ellit 
[cell_no] => 044 568 178 [email] => gzzi@gmail.com [id] => [sex] => 
[education] => [marital_status] => [employment] => [industry] => [dob] => 
[municipality] => [password] => )

和错误信息:

Sex cannot be blank. 
Education cannot be blank.  
Marital Status cannot be blank. 
Employment cannot be blank. 
Dob cannot be blank.
Municipality cannot be blank.
4

1 回答 1

2

你的下拉列表应该这样定义:

 <div class="row">
    <?php echo CHtml::activeLabel($model, 'Statusi Martesor'); ?>
    <?php echo CHtml::activeDropDownList($model, 'marital_status', array(0 => 'Beqar/e', 1 => 'I/E divorcuar', 2 => 'I/E martuar', 3 => 'I/E veje')) ?>
</div>

代替:

<div class="row">
    <?php echo CHtml::activeLabel($model, 'Statusi Martesor'); ?>
    <?php echo CHtml::dropDownList('marital_status', 0, $data = array(0 => 'Beqar/e', 1 => 'I/E divorcuar', 2 => 'I/E martuar', 3 => 'I/E veje')) ?>
</div>

在第一个实例中(我使用的地方CHtml::activeDropDownList($model, 'attribute_name')),你可以在这样做之后访问你的下拉属性$model->attributes = $_POST['ModelName'];$model->sex因为$model->marital_statusPOST 数组就像:$_POST['ModelName']['sex']$_POST['ModelName']['marital_status']

当您使用CHtml::dropDownList('field_name', 'selected_value', array('option1', 'option2'))时,POST 数组的形式$_POST['field_name']为,对于 ActiveRecord,您想要的是$_POST['ModelName']['field_name']

我希望这能解决你的问题。

于 2013-07-24T15:24:21.337 回答