Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

How To Create/save More Model Inputs ?

$
0
0
Hi all,
I'm trying this document. http://www.yiiframework.com/wiki/29/

My database is Posted Image

My problem is
$model->attributes=$_POST['Resimler'] array is always EMPTY!

How can inputs values $_POST['Resimler'] ?
Thanks for helps...
/GaleriController.php
	public function actionCreate()
	{
		$model=new Galeri;
		$model2 =array(new Resimler,);
		
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if( isset( $_POST['submitDatas'] ) && isset( $_POST['Galeri'], $_POST['Resimler'] ) )
		{
			$model->attributes=$_POST['Galeri'];
			
			print_r($_POST['Resimler']);
			// PRİNT Array ( [resim_url] => Array ( [0] => ) )
			$valid = $model->validate();
			foreach ( $_POST['Resimler'] as $i => $photo ) {
				$model2[$i] = new Resimler;   
				if ( isset( $_POST['Resimler'][$i] ) )
				    $model2[$i]->resim_url = CUploadedFile::getInstance($model2[$i], "resim_url[$i]" );
				$valid = $valid && $model2[$i]->validate();               
			}
			
			if( $valid && $model->save(false) ) {
			   
			    // Saving each period ( i've to change the date format couse in Italy we use dd/mm/yyyy
			    foreach ($model2 as $i => $photo) {
				$photo->galeri_idgaleriler = $model->idgaleri;
				
				$photo->save(false);
				}	
			if($model->save())
				$this->redirect(array('view','id'=>$model->idgaleri)); 
			}
		}

		$this->render('create',array(
			'model'=>$model,
			'model2' => $model2,
			'photosNumber' => isset($_POST['Resimler']) ? count($_POST['Resimler'])-1 : 0, //How many PhotoEvent the user added
			'update' => false,
		));
	}


Views:
<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'galeri-form',
	'enableAjaxValidation'=>false,
	'htmlOptions' => array(
	'enctype' => 'multipart/form-data'),

)); ?>

	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<?php echo $form->errorSummary($model); ?>

	<div class="row">
		<?php echo $form->labelEx($model,'galeri_adi'); ?>
		<?php echo $form->textField($model,'galeri_adi',array('size'=>60,'maxlength'=>128)); ?>
		<?php echo $form->error($model,'galeri_adi'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'galeri_resim'); ?>
		<?php echo $form->textField($model,'galeri_resim',array('size'=>60,'maxlength'=>128)); ?>
		<?php echo $form->error($model,'galeri_resim'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'galeri_sira'); ?>
		<?php echo $form->textField($model,'galeri_sira',array('size'=>60,'maxlength'=>128)); ?>
		<?php echo $form->error($model,'galeri_sira'); ?>
	</div>
	
	<?php echo CHtml::button('add Photos', array('name'=>'addPhotos', 'id'=>'addPhotos')); ?>
 
	<?php foreach($model2 as $i => $photo): ?>
    <div id="photo-<?php echo $i ?>">
        <div class="simple">   
            <?php echo $form->LabelEx($photo,'resim_url'); ?>
            <?php echo $form->FileField($photo, "resim_url[$i]"); ?>
        </div>
        <br />
    </div>
    <?php endforeach; ?>
	
	<div class="row buttons">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',array('name'=>'submitDatas')); ?>
	</div>

<?php $this->endWidget(); ?>

</div>&lt!-- form -->

<script type="text/javascript">
// I need to know how many photos I've already added when the validate return FALSE
var photosAdded = <?php echo $photosNumber; ?>;
 
// Add the event to the period's add button
$('#addPhotos').click(function () {
    // I'm going to clone the first div containing the Model input couse I don't want to create a new div and add every single structure
    var divCloned = $('#photo-0').clone();      
    // I'm attaching the div to the last input created
    $('#photo-'+(photosAdded++)).after(divCloned);
    // Changin the div id
    $('.photos:last').after(divCloned);
    divCloned.attr('id', 'photo-'+photosAdded);
    // Initializing the div contents
    initNewInputs(divCloned.children('.simple'), photosAdded);
});
 
function initNewInputs( divs, idNumber ) {
    // Taking the div labels and resetting them. If you send wrong information,
    // Yii will show the errors. If you than clone that div, the css will be cloned too, so we have to reset it
    var labels = divs.children('label').get();
    for ( var i in labels )
        labels[i].setAttribute('class', 'required');      
 
    // Taking all inputs and resetting them.
    // We have to set value to null, set the class attribute to null and change their id and name with the right id.
    var inputs = divs.children('input').get();      
 
    for ( var i in inputs  ) {
        inputs[i].value = "";
        inputs[i].setAttribute('class', '');
        inputs[i].id = inputs[i].id.replace(/\d+/, idNumber);
        inputs[i].name = inputs[i].name.replace(/\d+/, idNumber);
    }
}
</script>


/create.php
<h1>Create Galeri</h1>

<?php echo $this->renderPartial('_form', array('model'=>$model,'model2'=>$model2,'photosNumber'=>$photosNumber)); ?>

Viewing all articles
Browse latest Browse all 18717


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>