Dropdownlist Validation Error

Buenas…

i’m developing a site. i built a for for upload images according a section id. i put the following:




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

		<?php echo $form->dropDownList($model,'id_seccion',array(1=>"Inicio", 2=>"Nuestra Empresa", 3=>"Servicios", 4=>"Productos", 5=> "Nuestros Clientes")); ?>

                <?php echo $form->error($model,'seccion'); ?>



at the moment to send the form, i get an error

in the same web site, following the post sample in the tutorial, the same thing works.

setting the tables and relations as made for yii i have:

for post model




/**

	 * @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(

			'author' => array(self::BELONGS_TO, 'User', 'author_id'),

                        'seccion' => array(self::BELONGS_TO, 'Seccion', 'id_seccion'),

		);

	}



for banner model




/**

	 * @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(

			'seccion' => array(self::BELONGS_TO, 'Seccion', 'id_seccion'),

		);

	}



in the rules i have:

for post




return array(

			array('title, content, status, id_seccion', 'required'),

			array('status, id_seccion, create_time, update_time, author_id', 'numerical', 'integerOnly'=>true),

			array('title', 'length', 'max'=>128),

			array('tags', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, title, content, tags, status, id_seccion, create_time, update_time, author_id', 'safe', 'on'=>'search'),

		);



for banner




return array(

			array('imagen, id_seccion', 'required'),

			array('id_seccion', 'numerical', 'integerOnly'=>true),

                        // this will allow empty field when page is update (remember here i create scenario update)

			array('imagen', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'), 

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id_banner, id_seccion', 'safe', 'on'=>'search'),

		);



my question is: why i get that validation error in id_seccion if in post model works fine?

Hi Néstor,

Can you show us your controller code? I don’t see anything wrong so far.

thnx for the anwwer

recently checking for another thing, i’ve found i mistook a code line where to “re-create” the model:





/**

	 * Creates a new model.

	 * If creation is successful, the browser will be redirected to the 'view' page.

	 */

        public function actionCreate()

        {

            $model=new Banner;  // this is my model related to table

            if(isset($_POST['Banner']))

            {

                /*$rnd = rand(0,9999);  // generate random number between 0-9999

                $model->attributes=$_POST['Banner'];// this line i forgot to uncomment xD


                $uploadedFile=CUploadedFile::getInstance($model,'imagen');

                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name

                $model->image = $fileName;


                if($model->save())

                {

                    $uploadedFile->saveAs(Yii::app()->basePath.'/../images/banner/'.$fileName);  // image will uplode to rootDirectory/banner/

                    $this->redirect(array('admin'));

                }*/

		$model->attributes=$_POST['Banner'];//copy&paste XD

                // file handling

                $imageUploadFile = CUploadedFile::getInstance($model, 'imagen');

                $rnd = rand(0,9999);  // generate random number between 0-9999

                if($imageUploadFile !== null){ // only do if file is really uploaded

                    $imageFileName = $rnd.$imageUploadFile->name;

                    $model->imagen = $imageFileName;

                }           


                if ($model->save()){

					

                    if($imageUploadFile !== null) // validate to save file

                        $imageUploadFile->saveAs(Yii::app()->basePath.'/../images/banner/'.$imageFileName);


                    $this->redirect(array('admin'));//$this->redirect(array('view', 'id' => $model->id));

                } else {

					var_dump($_POST['Banner']);

				}

            }

			

			//Setting the default value

			$model->id_seccion=1;

            $this->render('create',array(

                'model'=>$model,

            ));

        }

but now, re-using this post, how to set the uploaded image size -height & width-?

You can use getimagesize(). :)

http://jp2.php.net/manual/en/function.getimagesize.php




$sizes = getimagesize($imageUploadFile->tempName);

$model->width = $sizes[0];

$model->height = $sizes[1];



mmm ok

but the controller is who makes the logic in this case, and checking, the saveas method moves or copies the actual image, butdoesn’t resize in any moment such as can do with


imagecopyresampled

, so in this case how to rezise the image?

if is needed, i would open a new post XD

Yii doesn’t have a built-in support for manipulating images.

If you don’t want the controllers to be fat with the codes that manipulate images (load, save, resize, rotate, crop, …), you can make some helper class to do that. Or, using some extension for images might be a good idea, too.

search for "image" in extension