E
E
EVOSandru62015-12-30 06:29:43
Yii
EVOSandru6, 2015-12-30 06:29:43

How to make fileField required in Yii?

Good afternoon, such a problem.
There is a Slider model with text properties and a foreign key to the model that stores file names.
After the form validation, when I click on the save button, a message appears:
You must fill in the "Image" field. Although I chose an image to upload. The rules in the model are listed below, what could be wrong?
The main model is connected to the image model via the Forign Key, the link is specified in the rules. I removed Integer only from the Photo field in the main Slider model.
In the form, the field is displayed like this.

<?php echo $form->labelEx($model, 'PHOTO'); ?>
<?php echo $form->error($model, 'PHOTO'); ?>
<?php echo $form->fileField($model, 'PHOTO'); ?>

The model file is described here:
public function attributeLabels()
  {
    return array(
                        ....
      'PHOTO' => 'Код',
                        ....
                )
        }

public function relations()
  {
    return array(
                        // связочная на модель, где хранятся названия картинок
                        ... 
      'photo' => array(self::BELONGS_TO, 'Photo', 'PHOTO'),
                        ... 
    );
  }

public function rules()
  {

    return array(
                        ...
      array('NAME, PHOTO', 'required'),
                        array('PHOTO', 'file', 'types' => 'jpg, png, gif', 'allowEmpty' => true),
                        ...
                );
  }

You cannot opt ​​out of having to upload an image file. Help good Santa Claus!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alibek Musin, 2015-12-30
@Alibek_Mussin

It's safer to do validation on both client and server
Try this:
<?php echo $form->fileField($model, 'PHOTO',array('required'=>true)); ?>
And on the server what the guy wrote above.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question