Yii Framework Forum: Help With Cactiveform Dropdownlist() - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Help With Cactiveform Dropdownlist() How to keep selected list item active after form submission Rate Topic: -----

#1 User is offline   joev 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 21-December 11
  • Location:NYC

Posted 02 February 2014 - 04:16 PM

I am trying to create a simple filter using a form and CActiveForm dropDownList(). I cannot keep the selected list item active after the form is posted and I cannot see what I am missing.

	

    Model
    class Art extends CActiveRecord
    {
             public $locationID;
             /**
             * Returns the static model of the specified AR class.
             * @param string $className active record class name.
             * @return Art the static model class
             */
            public static function model($className=__CLASS__)
            {
                    return parent::model($className);
            }
     
            /**
             * @return string the associated database table name
             */
            public function tableName()
            {
                    return 'art';
            }
     
            /**
             * @return array validation rules for model attributes.
             */
            public function rules()
            {
                    // NOTE: you should only define rules for those attributes that
                    // will receive user inputs.
                    return array(
                            array('name, frame, categoryID, sizeID, locationID, statusID, note', 'required'),
                            array('frame, categoryID, sizeID, locationID, statusID, typeID, collectorID, reproductionID', 'numerical', 'integerOnly'=>true),
                            array('name', 'length', 'max'=>100),
                            // The following rule is used by search().
                            // Please remove those attributes that should not be searched.
                            array('id, name, frame, categoryID, sizeID, locationID, statusID, typeID, collectorID, reproductionID, note', 'safe'),
                    );
            }
     
            /**
             * @return array relational rules.
             */
            public function relations()
            {
                    // NOTE: you may need to adjust the relation name and the related
                    // class name for the relations automatically generated below.
                    return array(
                            'status' => array(self::BELONGS_TO, 'Status', 'statusID'),
                            'category' => array(self::BELONGS_TO, 'Category', 'categoryID'),
                            'type' => array(self::BELONGS_TO, 'Type', 'typeID'),
                            'collector' => array(self::BELONGS_TO, 'Collector', 'collectorID'),
                            'location' => array(self::BELONGS_TO, 'Location', 'locationID'),
                            'reproduction' => array(self::BELONGS_TO, 'Reproduction', 'reproductionID'),
                            'size' => array(self::BELONGS_TO, 'Size', 'sizeID'),
                    );
            }
     
            /**
             * @return array customized attribute labels (name=>label)
             */
            public function attributeLabels()
            {
                    return array(
                            'id' => 'ID',
                            'name' => 'Name',
                            'frame' => 'Frame',
                            'categoryID' => 'Category',
                            'sizeID' => 'Size',
                            'locationID' => 'Location',
                            'statusID' => 'Status',
                            'typeID' => 'Type',
                            'collectorID' => 'Collector',
                            'reproductionID' => 'Reproduction',
                            'note' => 'Note',
                    );
            }
    }
     
     
    Controller
    public function accessRules()
            {
                    return array(
                            array('allow',  // allow all users to perform 'index' and 'view' actions
                                    'actions'=>array('index','view'),
                                    'users'=>array('*'),
                            ),
                            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                                    'actions'=>array('create','update'),
                                    'users'=>array('@'),
                            ),
                            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                                    'actions'=>array('admin','delete'),
                                    'users'=>array('admin'),
                            ),
                            array('deny',  // deny all users
                                    'users'=>array('*'),
                            ),
                    );
            }
     
    public function actionIndex()
            {
                    $model=new Art;
                    $condition='statusID=1';
                    if(isset($_POST['Art'])){
                            if($_POST['Art']['locationID']>0)
                                    $condition.=' AND locationID='.$_POST['Art']['locationID'];
                    }
                    $dataProvider=new CActiveDataProvider('Art',array(
                            'criteria'=>array(
                            'condition'=>$condition,
                            'order'=>'name',
                    ),
                    'pagination'=>array(
                                    'pageSize'=>12,
                            ),
                    ));
                    //$dataProvider->setPagination(false);
                    //$rows=$dataProvider->getTotalItemCount();
                    //$dataProvider=$dataProvider->getData();
                    $this->render('index',array(
                            'dataProvider'=>$dataProvider,
                            'model'=>$model,
                    ));
            }
     
    View
    <?php $form=$this->beginWidget('CActiveForm', array(
            'id'=>'art-form',
            'enableAjaxValidation'=>false,
    )); ?>
     
           
     
            <div class="row">
                    <?php echo $form->dropDownList($model,'locationID',array(0=>'All',1=>'Studio',5=>'Storage - New York',6=>'Stotage - Boston')); ?>
            </div>
     
            <div class="row buttons">
                    <?php echo CHtml::submitButton('Filter'); ?>
            </div>
     
    <?php $this->endWidget(); ?>


0

#2 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,695
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 02 February 2014 - 04:36 PM

You forgot
$model->locationID = $_POST['Art']['locationID'];
inside
if($_POST['Art']['locationID']>0) {...}

God is real unless declared as integer
0

#3 User is offline   joev 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 21-December 11
  • Location:NYC

Posted 02 February 2014 - 04:57 PM

View PostORey, on 02 February 2014 - 04:36 PM, said:

You forgot
$model->locationID = $_POST['Art']['locationID'];
inside
if($_POST['Art']['locationID']>0) {...}



Ughhhhhh, simple oversight.

Thanks.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users