Yii Framework Forum: Validate Only One Field (File Field Or Textfield) Is Filled Out - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Validate Only One Field (File Field Or Textfield) Is Filled Out Rate Topic: -----

#1 User is offline   Sierra101 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 18-December 12

  Posted 18 December 2012 - 05:43 PM

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
0

#2 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 882
  • Joined: 20-April 11
  • Location:India

Posted 19 December 2012 - 01:20 AM

Use custom validation rules

use this wiki

http://www.yiiframew...alidation-rule/

wiki
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#3 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 882
  • Joined: 20-April 11
  • Location:India

Posted 19 December 2012 - 01:23 AM

u can try this too

http://www.yiiframew...alidation-rules

doc
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#4 User is offline   Sierra101 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 18-December 12

Posted 19 December 2012 - 06:55 AM

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!
0

#5 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 882
  • Joined: 20-April 11
  • Location:India

Posted 19 December 2012 - 08:27 AM

View PostSierra101, on 19 December 2012 - 06:55 AM, said:

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
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users