Hi, I'm trying to validate active filefield fields that were created in Yii so dynamic.
I build using a forech for this inputs, the question is that when trying to validate when the user selects an image, indicate whether the file you chose is above the allowed weight, and also if it is a correct file format.
But i don't know that I'm doing wrong.
I tried this: My model
public $image;
public function tableName() {
return 'vrf_image';
}
/**
* @return array validation rules for model attributes.
*/
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('id_form', 'numerical', 'integerOnly'=>true),
// Pruebas para subir imagen
// Inicio
array(
'image',
'file',
'types'=>'jpg, gif, png',
'allowEmpty'=>true,
'on'=>'update',
'on'=>'insert',
'wrongType'=>'Solo imagenes',
'mimeTypes' => 'image/gif, image/jpeg, image/png',
'wrongMimeType' => 'Invalid mime type',
'maxSize'=>1024*1024*2,
'tooLarge'=>'File has to be smaller than 2MB',
),
array(
'image',
'length',
'max'=>50,
'on'=>'insert,update'
),
array(
'id, image, id_form, id_ques',
'safe',
'on'=>'search'
),
// Fin
);
}
My views
<div class="container-fluid">
<div class="row paddings_bottom">
<?php
$hoho = VrfImage::model()->findAll("id_form = $search->id");
$formImage=$this->beginWidget('CActiveForm', array(
'id'=>'verification-form-image',
'action' => Yii::app()->createUrl("backendVerification/default/CreateImage?id=".$modUser->id),
'htmlOptions' => array('enctype' => 'multipart/form-data'),
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
'validateOnChange'=>true,
'validateOnType'=>true,
),
));
?>
<div class="col-md-12 col-lg-12">
<table class="table table-bordered" style="margin-bottom: 0;">
<?php
$oo = 0; $ban = 0;
foreach ($numQues as $ii => $NQ){
if($ii==0){
echo '<div class="borTopFormVrf"><span class="White">Company Documentation</span></div>';
}
if($ii <6){
if($oo<2){
if($oo==0){
echo '<tr>';
}
?>
<td class="col-md-6 col-lg-6 nopadd" style="border: 1px solid #000;">
<?php
foreach ($hoho as $key => $value) {
if($hoho[$key]->id_ques == $NQ){
echo '<img src="'.getThemeUri().'/../../VerificationResource/images/'.$hoho[$key]->image.'" style="max-width:100%;">';
$ban = 1;
}
}
if($ban!=1){ echo '<img src="#" style="max-width:100%; visibility: hidden;" id="img_prev'.$ii.'" alt="your image"/>'; }
else{ $ban=0; }
?>
<?php echo $form->HiddenField($modImage,"[".$ii."]id_ques",array('value'=>$NQ)); ?>
<?php echo $formImage->fileField($modImage,'['.$ii.']image', array('class'=>'form-control Borf-c', 'onChange'=>'readURL(this,'.$ii.')')); ?>
<?php echo $formImage->error($modImage,'['.$ii.']image'); ?>
<div class="text-center">
<?php echo $nameQues[$ii]; ?>
</div>
</td>
<?php if($oo==1){ echo '</tr>'; $oo = -1; }
$oo++;
}}
}
?>
</table>
</div>
<div class="col-md-12 col-lg-12">
<div class="col-md-12 col-lg-12 divTopFormBVrf">
<?php
if(isset($search->id_verifier)){
if($search->id_verifier == userID()){
echo CHtml::submitButton('Save', array('class' => 'btn btn-success btn_create_address btn_create_address_size'));
}
}
?>
</div>
</div>
<?php $this->endWidget(); ?>
</div>
In my controller
public function actionCreateImage($id) {
$form = VrfForm::model()->find("id_user = $id");
$path_picture = realpath(Yii::app()->getBasePath()."/../VerificationResource/images")."/"; //ruta final de la imagen
if(isset($_POST['VrfImage'])) {
foreach ($_POST['VrfImage'] as $key => $value) {
$modImage = new VrfImage;
$modImage->attributes = $value;
//print_r($modImage->validate());
if($modImage->validate()){
echo 'Correcto';
////////////////////////////////////////////////////////////////////
$rnd = rand(0,9999); // generar números aleatorios entre 0-9999
$uploadedFile=CUploadedFile::getInstance($modImage,'['.$key.']image');
$fileName = "{$rnd}-{$uploadedFile}"; // número aleatorio + nombre de archivo, o se puede usar: $fileName=$uploadedFile->getName();
if(!empty($uploadedFile)) { // Compruebe si el archivo se ha subido o no.
$uploadedFile->saveAs($path_picture.$fileName);
$modImage->image = $fileName;
$modImage->id_ques = $value['id_ques'];
$modImage->id_form = $form->id;
if($modImage->save()) {
//echo ' REVISA!!! <br>';
}
else{
echo $modImage->error();
}
}
////////////////////////////////////////////////////////////////////
}
else{
echo 'Falla';
//$this->redirect($this->createUrl('default/Form'));
}
}
}
//$this->redirect(Yii::app()->user->returnUrl.'backendVerification/default/Form?id='.$id);
}
But don't It does not work.!!!
Is bad my "clientOptions" in the form? Is bad my rules in the model?
Help T.T