0

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 :)

5
  • 2
    I think you should give the code for the "blabla" sections, because if you have an error, it may be there. Aug 23, 2013 at 8:28
  • added the attribute declarations.
    – erisazani
    Aug 23, 2013 at 8:48
  • 1
    You will need to post the entire code for actionOrder. Aug 23, 2013 at 8:57
  • If your user_phone is always empty, then you likely have a bug in the code in actionOrder that does not correctly assign the variables from the form post, or a bug in the form that does not correctly post the values. Either way, you need to show all the relevant sections of your code. Aug 23, 2013 at 9:38
  • added all relevant codes, actually it has been solved. Thanks anyway :)
    – erisazani
    Aug 24, 2013 at 1:16

1 Answer 1

0
  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
...
}
5
  • (1) Wow, there are plenty of informations o_o, what section should I check? (2) It says "Phone number cannot be empty", but it didn't give detailed information about it, actually I have tried this. (3) I'll check it immediately, thanks anyway :)
    – erisazani
    Aug 23, 2013 at 8:21
  • (1) Just dump $order->attributes instead if you can't find the column user_phone. Once you see $order->user_phone is empty, it means the code that you use the set data for model was incorrect. Aug 23, 2013 at 8:26
  • It prints NULL... actually I have a variable named user_phone in the model, which is same with a field in my database, user_phone. Is the input will be passed to the variable in the model? (I've added the declaration of the attributes in the model code). And is it necessary to write public $user_phone?
    – erisazani
    Aug 23, 2013 at 8:48
  • No, you don't need declare it on model, Yii handle it for you. I'll update my answer for you see what wrong since you did not post full your form and code in controller and there has many cases you got it wrong. Aug 23, 2013 at 9:02
  • Your code snippet above works! That's what I didn't do. Thank you for helping me :)
    – erisazani
    Aug 24, 2013 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.