dropDownList: required

Hello,

maybe I am just tired… but I cannot find a solution to this problem. :unsure:

I have a CActiveForm widget in a view page the contains a dropdownlist:

$form->dropDownList($dealers, ‘dealer’, CHtml::listData($dealers->active()->findAll(),‘id’,‘area’), array(‘prompt’=>‘Choose your country:’));

In the corresponding model the "dealer" item is declared as required:

array(‘name, email, dealer, phone, subject, body’, ‘required’),

But when i submit the form, selecting correctly the Country item in the dropdownlist, I get a warning telling that "Country cannot be blank". The dropwownlist is filled correctly.

What’s wrong with my form?

Sorry for the question, but I’m rather new to Yii and OO-framework.

Bye.

Mauro

I think the attribute should be id which is the one that is used to select the default value.




$form->dropDownList($dealers, 'id', CHtml::listData($dealers->active()->findAll(),'id','area'), array('prompt'=>'Choose your country:'));



Also, in your rules, mark the ‘country’ as ‘safe’

array(‘country’,‘safe’);

I mean… if your attribute in dealers is named ‘country’

Many thanks Antonio for you reply, but the problem persists.

Maybe there is a naming error. This is the $_POST variable I get after submit:

Array ( [ContactForm] => Array ( [name] => Mauro [email] => [phone] => [address] => [city] => [state] => [zip] => [subject] => [body] => [verifyCode] => yelonqb ) [Dealer] => Array ( [id] => ) [yt1] => Submit )

This is the HTML code created by Yii and PHP (not that I renamed the label in Country with attributeLabels() method)

<div class="row">

<label class="error required" for="ContactForm_dealer">Country <span class="required">*</span></label>

<select name="Dealer[id]" id="Dealer_id">

<option value="">Choose your country:</option>

<option value="1">Italy</option>

<option value="2">Austria</option>

<option value="3">Germany</option>

</select>

</div>

Why the name of the dropwdownlist is "Dealer[id]"? I expected something in the form of "ContactForm[dealer]" and a $_POST array like this:

Array ( [ContactForm] => Array ( [name] => Mauro [email] => [phone] => [address] => [city] => [state] => [zip] => [dealer] => 1 [subject] => [body] => [verifyCode] => yelonqb ) [yt1] => Submit )

:blink:

Normally, when you include a model in the activeform fiels, the name of the model (class) is before its attribute. So its resulting HTML is NAMEOFMODEL[ATTRIBUTE]

Maybe, if you drop the view file here, we will better understand what is happening. Why for the others is ContactForm and only for that is Dealer… by the way, I have seen that in your code the variable in the combo is $dealears… is the same for the rest of HTML elements?

This is the view file:




<h1>Information Request Form</h1>




<?php

// Flash message

if(Yii::app()->user->hasFlash('contact')):

?>


<div class="flash-success">

        <?php echo Yii::app()->user->getFlash('contact'); ?>

</div>


<?php else: ?>


<?php

$this->beginWidget('CMarkdown', array('purifyOutput'=>true));

echo $labels['intro']['siteitem'];

$this->endWidget();

?>


<div class="form">


<?php $form=$this->beginWidget('CActiveForm'); ?>


        <p class="note"><?php echo $labels['required']['siteitem']; ?></p>


        <?php echo $form->errorSummary($model); ?>


        <fieldset>

            <legend>Your data:</legend>


            <div class="row">

                    <?php echo $form->labelEx($model,'name'); ?>

                    <?php echo $form->textField($model,'name'); ?>

            </div>


            <div class="row">

                    <?php echo $form->labelEx($model,'email'); ?>

                    <?php echo $form->textField($model,'email'); ?>

            </div>


            <div class="row">

                    <?php echo $form->labelEx($model,'phone'); ?>

                    <?php echo $form->textField($model,'phone'); ?>

            </div>


            <div class="row">

                <?php echo $form->labelEx($model,'address'); ?>

                <?php echo $form->textField($model,'address', array('size'=>60,'maxlength'=>128)); ?>

            </div>


            <div class="row">

                <?php echo $form->labelEx($model,'city'); ?>

                <?php echo $form->textField($model,'city', array('size'=>60,'maxlength'=>128)); ?>

            </div>


            <div class="row">

                <?php echo $form->labelEx($model,'state'); ?>

                <?php echo $form->textField($model,'state', array('size'=>60,'maxlength'=>128)); ?>

            </div>


             <div class="row">

                <?php echo $form->labelEx($model,'zip'); ?>

                <?php echo $form->textField($model,'zip', array('size'=>10,'maxlength'=>20)); ?>

            </div>


            <div class="row">

                <?php echo $form->labelEx($model,'dealer'); ?>

                <?php echo $form->dropDownList($dealers, 'id', CHtml::listData($dealers->active()->findAll(),'id','area'), array('prompt'=>'Choose your country:')); ?>

            </div>





        </fieldset>

            

        <fieldset>

            <legend>Your question:</legend>


            <div class="row">

                    <?php echo $form->labelEx($model,'subject'); ?>

                    <?php echo $form->textField($model,'subject',array('size'=>60,'maxlength'=>128)); ?>

            </div>


            <div class="row">

                    <?php echo $form->labelEx($model,'body'); ?>

                    <?php echo $form->textArea($model,'body',array('rows'=>6, 'cols'=>50)); ?>

            </div>


          </fieldset>




        <?php if(extension_loaded('gd')): ?>

        <div class="row">

                <?php echo $form->labelEx($model,'verifyCode'); ?>

                <div>

                <?php $this->widget('CCaptcha'); ?>

                <?php echo $form->textField($model,'verifyCode'); ?>

                </div>

                <div class="hint"><?php echo $labels['hint']['siteitem']; ?></div>

        </div>

        <?php endif; ?>


        <div class="row submit">

                <?php echo CHtml::submitButton('Submit'); ?>

        </div>


<?php $this->endWidget(); ?>


</div><!-- form -->


<?php endif; ?>



…and this is the model:




<?php


/**

 * ContactForm class.

 * ContactForm is the data structure for keeping

 * contact form data. It is used by the 'contact' action of 'SiteController'.

 */

class ContactForm extends CFormModel

{

        public $name;

        public $email;

        public $phone;

        public $address;

        public $city;

        public $state;

        public $zip;

        public $dealer;        

        public $subject;

        public $body;

        public $verifyCode;


        /**

         * Declares the validation rules.

         */

        public function rules()

        {

                return array(

                        // name, email, subject and body are required

                        array('name, email, dealer, phone, subject, body', 'required'),

                        // email has to be a valid email address

                        array('email', 'email'),

                        // verifyCode needs to be entered correctly

                        array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')),

                        array('dealer','safe'),

                );

        }


        /**

         * Declares customized attribute labels.

         * If not declared here, an attribute would have a label that is

         * the same as its name with the first letter in upper case.

         */

        public function attributeLabels()

        {

                return array(

                        'verifyCode'=>'Verification Code',

                        'state'=>'State / Province / Region',

                        'zip'=>'Zip Code',

                        'dealer'=>'Country',

                );

        }

}



Very very simple as you can see. But it doesn’t work correctly.

Mauro

Ah problem solved…


<?php echo $form->dropDownList($model, 'id', CHtml::listData($dealers->active()->findAll(),'id','area'), array('prompt'=>'Choose your country:')); ?>

and NOT


<?php echo $form->dropDownList($dealers, 'id', CHtml::listData($dealers->active()->findAll(),'id','area'), array('prompt'=>'Choose your country:')); ?>

as in my previous version. I confused the meaning of the first parameter of dropDownList method (the data model: public string dropDownList(CModel $model, string $attribute, array $data, array $htmlOptions=array ( ))).

Thank you anyway for your support.

Mauro

"by the way, I have seen that in your code the variable in the combo is $dealears… is the same for the rest of HTML elements? "

Good you found the problem