始终无法在Yii中验证

I've been confused with validation rules in Yii. I am sure when I give inputs there is no mistakes, corresponds to the rules given.

This is the validation rules in my model:

 
    // public $user_phone; //updated: this isn't necessary
    public $maxId;
    ...
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('user_phone', 'required', 'message'=>'{attribute} cannot be empty.<br />'),
            array('user_phone', 'length', 'max'=>12),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('user_phone', 'safe', 'on'=>'search'),
            );
    } 

This is when I call the validation function in the controller: Updated: Added the assignment of the model's attributes

 
    public function actionOrder(){
        $order = new IptvOrder;
        if(isset($_POST["IptvOrder"])) {
            $order->attributes = $_POST["IptvOrder"]; // this is what I forgot
            // some assignments to $id, $phone, $date, $time
            if($order->validate()) {
                $order->addOrder($id, $phone, $date, $time);
                $this->redirect(array('order/orderConfirm'));
            }
        ...blablabla...
    } 

And I put the validation error message in the view:

 
    ...blablabla...
    <?php
        echo $form->label($order,'Phone Number');
        echo "<font color='red'> *</font>";
        echo "<span style='font-size:10pt; color:#888888'><br/>Digunakan untuk konfirmasi pemesanan. Kerahasiaan kami jamin.</span><br/>";
        echo $form->textField($order,'user_phone'); 
        echo "<font color='red'>".$form->error($order, 'user_phone')."</font>";
    ?>
        echo CHtml::submitButton('Pesan Sekarang', array('confirm' => 'Apakah anda yakin?
Silahkan cek pemesanan anda terlebih dahulu.'));
    ...blablabla...
    
// $form is CActiveForm, $order is the model

And it won't be redirected to order/orderConfirm although the textfield is not empty. Anybody can help? Thanks :)

  1. Dump $order->attributes check the attribute user_phone has content or empty
  2. Print out what your given error is from $order->getErrors()
  3. About max length validation, because your input for that field is phone number, I exclude the possible of ASCII letter like below case

http://www.yiiframework.com/forum/index.php/topic/16338-validation-problem-with-length-on-textfields/

Updated: If your model name was Order as example, you should have to look like before you perform the validation

if(isset($_POST['Order']))
        {
            $order->attributes=$_POST['Order']; //<== Make sure you have set data for your order model before performing a validation
//validate part
...
}