Hi folks, I’m having some problems when I try upload multiples files in multiples instances of my model (tabular way).
I have a model called Files and a view that generate a form for multiple instances of that model. When I save the form without "multpart/form-data", everything works, but if I put this parameter on form and submit it, the validation shows the message that "File cannot be blank."
You are passing $_POST array instead $_FILES array.
Ok, this will break your generic validation method, but you must pass $_FILES for attribute which has rule type file along as $_POST for other attributes.
I did a test using your suggestion, but without success.
When I submit the form passing $_FILES array instead of passing $_POST the php thow a fatal error (see bellow).
Fatal error: Call to undefined method stdClass::validate() in /Users/san/src/neogama/fornecedores/svn/trunk/web/public/protected/controllers/SiteController.php on line 79
I also tried to assing the files instance before validations but without success too, the same error happens "File cannot be blank."
I think that yii upload feature not work with tabular input. Maybe I’ll have to implement my own upload method.
// model file
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;
public $image;
...
public function rules()
{
return array(
array('username, password', 'required'),
array('image', 'length', 'max'=>255),
array('image', 'file', 'allowEmpty' => false),
/*
You may put image also in required validator
and error message will be displayed with all
required attributes. Otherwise it will be displayed
after all required fields are filled. You may remove
allowEmpty => false, because it is false by default.
Anyway it's just our old PHP constant UPLOAD_ERR_NO_FILE <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/smile.gif' class='bbc_emoticon' alt=':)' />
*/
...
// and finally in controller action
...
public function actionLogin()
{
$model=new LoginForm;
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
$model->image = $_FILES['LoginForm']; // this was wrong in my previous post... sorry <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/smile.gif' class='bbc_emoticon' alt=':)' />
if($model->validate())
{
die(print_r($model)); // here you will see image file
...