Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php $form = ActiveForm::begin(["method" => "post",'options' => ['enctype' => 'multipart/form-data'],'class'=>'well padding-bottom-10']); ?>
- <?= $form->field($model, "imgs[]")->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>
- <div class="pull-right margin-bottom-10 ">
- <div class="form-group">
- <?= Html::submitButton($model->isNewRecord ? 'Publicar' : 'Actualizar', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary', ]) ?>
- </div>
- <?php ActiveForm::end(); ?>
- public function actionCreate()
- {
- $model = new Foro();
- $msg= null;
- if ($model->load(Yii::$app->request->post())) {
- $model->imgs=UploadedFile::getInstances($model, 'imgs');
- if ($model->imgs && $model->validate()) {
- foreach ($model->imgs as $file) {
- if ($model->save() && $file->saveAs('archivos/foro/' . $file->baseName . '.' . $file->extension)){
- $msg = "<strong class='label label-info'>Enhorabuena, subida realizada con éxito</strong>";
- return $this->redirect('/konsultorio/web/index.php?r=foro/mostrarpost');
- }
- }
- }
- return $this->renderAjax('create', [
- 'model' => $model, "msg" => $msg
- ]);
- }
- class Foro extends yiidbActiveRecord{
- public $imgs;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'foro';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['titulo', 'post', 'fecha', 'medico_idMedico'], 'required'],
- [['post'], 'string'],
- [['fecha'], 'safe'],
- [['medico_idMedico'], 'integer'],
- [['titulo'], 'string', 'max' => 250],
- //atributo de la tabla "foro"
- [['img'], 'file', 'max' => 45],
- [['medico_idMedico'], 'exist', 'skipOnError' => true, 'targetClass' => Medico::className(), 'targetAttribute' => ['medico_idMedico' => 'idMedico']],
- //atributo que tuve que crear para que se guardara en el servidor ya que si dejo el atributo de la tabla no me guarda en nungun lado
- ['imgs','file', 'skipOnEmpty' => true,'maxSize' => 1024*1024*1, //1 MB
- 'tooBig' => 'El tamaño máximo permitido es 1MB', //Error
- 'minSize' => 10, //10 Bytes
- 'tooSmall' => 'El tamaño mínimo permitido son 10 BYTES', //Error
- 'extensions' => 'png, jpg', 'maxFiles' => 4]
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'idforo' => 'Idforo',
- 'titulo' => 'Titulo',
- 'post' => 'Post',
- 'fecha' => 'Fecha',
- 'img' => 'seleccionar archivo:',
- 'medico_idMedico' => 'Medico Id Medico',
- ];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement