下拉列表输入项上的验证失败

时间:2013-07-24 14:54:23

标签: yii

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

视图:

 </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.

1 个答案:

答案 0 :(得分: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_status,因为POST数组是ging就像:$_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']

我希望能解决你的问题。