Validation For Yii Timepicker

Hi, I would like to ask does anyone knows how to validate yii timepicker?

For example, I input [color="#FF0000"]class start:10am[/color] and [color="#FF0000"]class end:8am[/color] (attachment), it is still can be saved successfully although the time input is wrong. Is there anyone knows how to validate the time input, so that the user knows there is an error of time input?




//start time

    <td><?php echo $form->labelEx($model,'start'); ?></td>

    <td><?php 

    $this->widget('ext.timepicker.timepicker', array(

	'id'=>'start',

    'model'=>$model,

    'name'=>'start',

    ));

    ?></td>


//end time

    <td><?php echo $form->labelEx($model,'end'); ?></td>

    <td><?php

    $this->widget('ext.timepicker.timepicker', array(

    'model'=>$model,

    'name'=>'end',

    ));

    ?></td>



5595

time.png

Does anyone knows how to solve this? :unsure:

Yes, you can add your own validation functions. In this function you do some checks if the time is correct and if not, you set an error.

In your model rules:




	public function rules()

	{

		array('start, end', 'validateTimeField'),

	}



In your model




	// Custom validation for time field.

	public function validateTimeField($attribute,$params)

	{

		if($this->start == '' || $this->end == '')

		{

			$this->addError($attribute, $this->getAttributeLabel('Hey fool, fill in the start AND end time please!');	

		}

                //This is just an example, you will have to do some proper check if times are correct

		if($this->start < $this->end == '')

		{

			$this->addError($attribute, $this->getAttributeLabel('Wrong!');	

		}

	}	



Hi, Bjorn. Thank you so much for your help. It is totally working well with what I want. Thanks alot :lol: