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

Validate Only One Field (File Field Or Textfield) Is Filled Out

$
0
0
Hello everyone,

I've got a view with a file upload field say "Image" and a simple text field say "URL".
I try to get a validation working that only one of the fields is allowed to be filled out (at least one must be) and submitted.
The problem is, that I indeed can check the attribute value of the textfield in a custom validation rule in the model, but it seems that the attribute value of the
file field is not available [because it's a file field I guess]. Here is a dump of the attribute values after click on "submit" in the form:

array
(
'url' => 'http://www.yiiframework.com/doc/api/1.1/CValidator'
'title' => 'This is a test'
'id' => null
'img' => null
)


Here you can see, that - although I selected a picture for uploading - its attribute value for "img" after submitting the form is "null". So my custom validation condition
aka
// validate that only one field is filledo out
	if($this->url != "" &&  $this->img != "") {
		some_error_msg
	}


won't work because $this->img is always null in the model. So do I have to put this validation condition into the controller, after $_POST isset and the file attribute exists? Maybe this way? But if so, how do I return the error message to the initial upload form for the user to see, saying "Error: Please fill out only one of the fields."

$model->attributes=$_POST['Image'];
                        if($model->validate()) {
                            /*if($model->imgName == null && ($model->imgSrc == "" || $model->imgSrc == null)) {
                                $this
                                 $this->addError(, 'Please select either a local image or an external image url to upload ');
                            }*/
                            // upload image file
                            $model->imgName = CUploadedFile::getInstance($model, 'imgName');
			    (...)


Best regards,
Sierra101

Viewing all articles
Browse latest Browse all 18717

Trending Articles



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