Dite Gashi
Dite Gashi

Reputation: 128

Validation fails on dropdown list input item

Friends,

I have a registration form with several items and the drop down list on these items seems like its not being picked up by the form submission.

The View:

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

Validation rules:

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'),
        );
    }

After submitting my form I am getting the following when I do a print_r on $_POST['Auser']:

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

and the error message:

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.

Upvotes: 0

Views: 1143

Answers (1)

Oladapo Omonayajo
Oladapo Omonayajo

Reputation: 980

Your dropdownlist should be defined like this:

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

Instead of:

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

In the first instance (where I used CHtml::activeDropDownList($model, 'attribute_name')), you can access your dropdown attributes after doing $model->attributes = $_POST['ModelName']; like this $model->sex, $model->marital_status because the POST array is ging to be like: $_POST['ModelName']['sex'], $_POST['ModelName']['marital_status']

When you use CHtml::dropDownList('field_name', 'selected_value', array('option1', 'option2')), the POST array is of the form $_POST['field_name'] and for ActiveRecord, what you want is $_POST['ModelName']['field_name']

I hope that solves your question.

Upvotes: 2

Related Questions