douying2243 2013-08-23 07:57
浏览 42
已采纳

始终无法在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条回答 默认 最新

  • dongtu9823 2013-08-23 08:14
    关注
    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
    ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题