Yii Framework Forum: Beforevalidate Not Working For Me - Yii Framework Forum

Jump to content

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

Beforevalidate Not Working For Me Rate Topic: -----

#1 User is offline   drmovi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 08-March 12
  • Location:Egypt

Posted 08 October 2012 - 12:41 PM

hi every body
i've created a crud using gii
i add some check boxes in the form to control showing or hiding textfields
i want to implement this logic rule "when you check check box and textfield appears this textfield is required"
so i use beforeValidate() on my cmodel like the following
public function beforeValidate()
    {
        if ($this->recycle) {//this is the checkbox

            $this->validatorList->add(Yii::createComponent(array(
                'class' => 'CRequiredValidator',
                'attributes' => array('recycle_options'),//this is the textbox
                )));
        }

        return parent::beforeValidate();
    }

in my controller created automatically using gii
public function actionCreate()
	{
		$model=new MembershipItemCycles;

		// Uncomment the following line if AJAX validation is needed
		 //$this->performAjaxValidation($model);

		if(isset($_POST['MembershipItemCycles']))
		{
			$model->attributes=$_POST['MembershipItemCycles'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->cycle_id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}


i tested that with an empty textfield with checkbox checked but nothing occurs and no errors and fields saved to my database
what error i've done ?
0

#2 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,701
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 08 October 2012 - 01:48 PM

Just a thought: you can set the textbox validation rule as usual but add scenario option (for example, 'on' => 'recycle'), and then switch model validation scenario if the checkbox is checked.
God is real unless declared as integer
0

#3 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 08 October 2012 - 02:04 PM

Dear Friend

Check whether the following is helpful. (Not tested the code in my localhost. Kindly bear with me).

public function beforeValidate()
    {
        if ($this->recycle) {//this is the checkbox

            $this->validatorList->add(CValidator::createValidator('required',this,'recycle_options',array()));
        }

        return parent::beforeValidate();
    }

0

#4 User is offline   drmovi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 08-March 12
  • Location:Egypt

Posted 09 October 2012 - 01:03 PM

non of the above solution works for me ?
is there is any other idea?
0

#5 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 09 October 2012 - 02:49 PM

Dear Friend

Following code works!.

If user selects married status. He has to enter his wife name.
If he puts wife name without selecting married status. His wife name silently discarded.
MODEL:TestForm
<?php
class TestForm extends CFormModel
{

public $name;
public $married;
public $wifeName;

public function rules()
	{
		return array(
			array('name', 'required'),
			array('married', 'boolean'),
			array('wifeName', 'safe'),
		);
	}
	
	
	public function beforeValidate()
    {
        if ($this->married) {

            $this->getValidatorList()->add(CValidator::createValidator('required',$this,'wifeName',array()));
        }
         if (!$this->married) {

            $this->getValidatorList()->add(CValidator::createValidator('default',$this,'wifeName',array('value'=>'','setOnEmpty'=>FALSE)));
        }
        return  parent::beforeValidate();
    }
}

?>




VIEW:validate.php
<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'test-form',
	'enableAjaxValidation'=>false,
)); ?>

	<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,'name'); ?>
		<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>64)); ?>
		<?php echo $form->error($model,'name'); ?>
	</div>
	<div class="row rememberMe">
		<?php echo $form->checkBox($model,'married'); ?>
		<?php echo $form->label($model,'married'); ?>
		<?php echo $form->error($model,'married'); ?>
	</div>
	<div class="row">
		<?php echo $form->labelEx($model,'wifeName'); ?>
		<?php echo $form->textField($model,'wifeName'); ?>
		<?php echo $form->error($model,'wifeName'); ?>
	</div>

	<div class="row buttons">
		<?php echo CHtml::submitButton('submit'); ?>
	</div>

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

</div><!-- form -->




CONTROLLER:
public function actionValidate(){
	$model=new TestForm;
	if(isset($_POST['TestForm']))
	   {
		   $model->attributes=$_POST['TestForm'];
		   
		   $model->validate();
		   }
	
	$this->render('validate',array('model'=>$model));
	}


Regards.
0

#6 User is offline   drmovi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 08-March 12
  • Location:Egypt

Posted 09 October 2012 - 03:36 PM

thanks for replay
i've noticed somthing
your model class extends CFormModel not CActiveRecord which i extends
is this why my code doesnot works?
0

#7 User is offline   drmovi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 08-March 12
  • Location:Egypt

Posted 09 October 2012 - 04:10 PM

finally i understand why my code does not work will
recycleshould be assigned to a rule even if it will be assigned to safe
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