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

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

Use custom validation rules

use this wiki

http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/

wiki

u can try this too

http://www.yiiframework.com/doc/guide/1.1/en/form.model#declaring-validation-rules

doc

Thanks for your replies Rajith. I managed to get the validation for the file field working. But how would I implement ClientSide Validaton of the Image Field and the Url Field that the user gets immediate response that he should fill out only one of the fields? And please note, it’s not as for the normal ClientSide Validation yii tutorial since I cannot access the value of the file field until it’s submitted (only with some javascript validation).

What I want is maybe best described in a picture, but as this is one of my first posts here, I cant provide a url >.<

So as you can see, if the user enters a picUrl and selects a local file to upload simultaneously there should a ClientSide Validation error like shown in the picture. How do I accomplish that?

Hope one can understand my problem ;-).

Greetings!

use ajax validation

enable the ajax validation

use the above doc methods to validate

validate only by using model rules