Php 总是无法在Yii中验证

Php 总是无法在Yii中验证,php,validation,yii,Php,Validation,Yii,我对Yii中的验证规则感到困惑。我确信当我给出输入时没有错误,符合给定的规则 这是我的模型中的验证规则: // public $user_phone; //updated: this isn't necessary public $maxId; ... public function rules() { // NOTE: you should only define rules for those attributes that

我对Yii中的验证规则感到困惑。我确信当我给出输入时没有错误,符合给定的规则

这是我的模型中的验证规则:

    // 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'),
            );
    }

我将验证错误消息放在视图中:

    ...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

……等等。。。
  • 转储
    $order->attributes
    检查属性
    user\u phone
    是否有内容或为空
  • 
        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...
        }
  • $order->getErrors()
  • 关于最大长度验证,因为该字段的输入是电话号码,所以我排除了可能的ASCII字母,如下例所示
  • 更新:例如,如果您的型号名称为
    Order
    ,则在执行验证之前,您必须看起来像

    if(isset($_POST['Order']))
            {
                $order->attributes=$_POST['Order']; //<== Make sure you have set data for your order model before performing a validation
    //validate part
    ...
    }
    
    if(isset($\u POST['Order']))
    {
    
    $order->attributes=$\u POST['order']];/(1)哇,有很多信息,我应该检查哪个部分?(2)上面写着“电话号码不能为空”,但没有给出详细信息,实际上我已经尝试过了。(3)我会立即检查,谢谢:)(1)如果找不到user\u phone列,只需转储$order->attributes即可。一旦您看到$order->user\u phone为空,这意味着您为模型使用的设置数据的代码不正确。它打印空值…实际上,我在模型中有一个名为user\u phone的变量,该变量与我数据库中的一个字段user\u phone相同。输入是否将被传递给to模型中的变量?(我在模型代码中添加了属性声明)。还有,是否有必要编写公共$user_电话?不,您不需要在模型上声明,我会为您处理。我会为您更新我的答案,看看有什么问题,因为您没有在控制器中完整发布表单和代码,并且有许多情况下您弄错了。上面的代码片段有效!这是我没有做的。谢谢您的帮助:)我做到了nk你应该给出“blabla”的代码部分,因为如果您有错误,它可能在那里。添加了属性声明。您需要发布actionOrder的整个代码。如果您的用户电话总是空的,则actionOrder的代码中可能存在错误,无法正确分配表单post中的变量,或者表单中存在错误,无法正确发布值。无论哪种方式,您都需要显示代码的所有相关部分。添加了所有相关代码,实际上它已经解决了。谢谢:)
    
        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...
        }