Answer the question
In order to leave comments, you need to log in
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'); ?>
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),
...
);
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question