Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. <?php $form = ActiveForm::begin(["method" => "post",'options' => ['enctype' => 'multipart/form-data'],'class'=>'well padding-bottom-10']); ?>
  2.  
  3. <?= $form->field($model, "imgs[]")->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>
  4. <div class="pull-right margin-bottom-10 ">
  5.  
  6. <div class="form-group">
  7. <?= Html::submitButton($model->isNewRecord ? 'Publicar' : 'Actualizar', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary', ]) ?>
  8. </div>
  9.  
  10. <?php ActiveForm::end(); ?>
  11.  
  12. public function actionCreate()
  13. {
  14. $model = new Foro();
  15. $msg= null;
  16.  
  17.  
  18. if ($model->load(Yii::$app->request->post())) {
  19. $model->imgs=UploadedFile::getInstances($model, 'imgs');
  20.  
  21. if ($model->imgs && $model->validate()) {
  22. foreach ($model->imgs as $file) {
  23. if ($model->save() && $file->saveAs('archivos/foro/' . $file->baseName . '.' . $file->extension)){
  24. $msg = "<strong class='label label-info'>Enhorabuena, subida realizada con éxito</strong>";
  25. return $this->redirect('/konsultorio/web/index.php?r=foro/mostrarpost');
  26.  
  27. }
  28. }
  29. }
  30.  
  31.  
  32. return $this->renderAjax('create', [
  33. 'model' => $model, "msg" => $msg
  34. ]);
  35. }
  36.  
  37. class Foro extends yiidbActiveRecord{
  38.  
  39.  
  40. public $imgs;
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public static function tableName()
  45. {
  46. return 'foro';
  47. }
  48.  
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function rules()
  53. {
  54. return [
  55. [['titulo', 'post', 'fecha', 'medico_idMedico'], 'required'],
  56. [['post'], 'string'],
  57. [['fecha'], 'safe'],
  58. [['medico_idMedico'], 'integer'],
  59. [['titulo'], 'string', 'max' => 250],
  60. //atributo de la tabla "foro"
  61. [['img'], 'file', 'max' => 45],
  62. [['medico_idMedico'], 'exist', 'skipOnError' => true, 'targetClass' => Medico::className(), 'targetAttribute' => ['medico_idMedico' => 'idMedico']],
  63. //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
  64. ['imgs','file', 'skipOnEmpty' => true,'maxSize' => 1024*1024*1, //1 MB
  65. 'tooBig' => 'El tamaño máximo permitido es 1MB', //Error
  66. 'minSize' => 10, //10 Bytes
  67. 'tooSmall' => 'El tamaño mínimo permitido son 10 BYTES', //Error
  68. 'extensions' => 'png, jpg', 'maxFiles' => 4]
  69. ];
  70.  
  71. }
  72.  
  73.  
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function attributeLabels()
  78. {
  79. return [
  80. 'idforo' => 'Idforo',
  81. 'titulo' => 'Titulo',
  82. 'post' => 'Post',
  83. 'fecha' => 'Fecha',
  84. 'img' => 'seleccionar archivo:',
  85. 'medico_idMedico' => 'Medico Id Medico',
  86. ];
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement