Note that there are some explanatory texts on larger screens.

plurals
  1. POYii validation rule not working
    text
    copied!<p>I created a file uploaded validation rule without a table in my database, so what I did is I just extended it using CFormModel. </p> <p>Here is my code:</p> <p><strong>Controller</strong></p> <pre><code>public function actionMaterials($pid) { $projectMaterialFile = new ProjectMaterialFile; $this-&gt;render('project_materials',array( 'projectMaterialFile'=&gt;$projectMaterialFile, )); } </code></pre> <p><strong>VIEW (Project Material)</strong></p> <pre><code>&lt;div id="exportMaterialContent"&gt; &lt;h4 style="text-align:left;"&gt;Export Material Document&lt;/h4&gt; &lt;?php echo CHtml::form($this-&gt;createUrl("project/export"),'post',array('enctype'=&gt;'multipart/form-data')); ?&gt; &lt;?php echo CHtml::activeHiddenField( $projectMaterialFile,'idProject',array('value'=&gt;$projectModel-&gt;idProject) ); ?&gt; &lt;?php echo CHtml::activeFileField($projectMaterialFile, 'document'); ?&gt; &lt;?php echo CHtml::submitButton("Export"); ?&gt; &lt;?php echo CHtml::endForm(); ?&gt; &lt;/div&gt; </code></pre> <p><strong>Controller - Export(After the form from the view has been submitted)</strong></p> <pre><code>public function actionExport() { $model = new ProjectMaterialFile; $model-&gt;attributes = $_POST['ProjectMaterialFile']; if( $model-&gt;validate() ) { $model-&gt;document = CUploadedFile::getInstance($model,'document'); $model-&gt;document-&gt;saveAs(Yii::getPathOfAlias('webroot')."/material_document/".$model-&gt;document-&gt;name); echo "correct"; } else { echo "Invalid!"; } } </code></pre> <p><strong>Model</strong></p> <pre><code>class ProjectMaterialFile extends CFormModel { public $document; public $idProject; public function rules() { return array( array('document','file','types'=&gt;'csv'), array('document', 'required'), ); } public function attributeLabels() { return array( 'document' =&gt; 'Project Material Document', 'idProject' =&gt; 'Project ID', ); } } </code></pre> <p>So what I did here is that it always run to the else of my controller <code>actionExport()</code> Even if I try to <code>echo $model-&gt;validate();</code>, it does not print anything that's why it always go to the else statement.</p> <p>Your help would be greatly appreciated and of course, rewarded!</p> <p>Thanks! :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload