Problem with CActiveForm

Can someone please tell what I am doing wrong here? The form validation just doesn’t work. For example bob*+ is not valid username but the form accepts it with no errors…

Here’s my model class:

<?php

class newUserForm extends CFormModel {

public &#036;username;


public &#036;fullName;


public &#036;email;


public &#036;phonenumber;








public function rules() {


    return array(


        array('username, fullName, email', 'required'),


        array('fullName', 'length', 'max' =&gt; 30),


        array('username', 'length', 'max' =&gt; 20),


        array('username', 'match', 'pattern' =&gt; '/^([A-Za-z0-9_&#092;-&#092;.])/'),


        array('email', 'email', 'checkMX' =&gt; TRUE),





        );


}

}

?>

And here’s Controller:

<?php

class UserManagementController extends Controller {

public function filters() {

    return array(


        'accessControl'           // required to enable accessRules


    );


}





public function accessRules() {


    return array(


        array('allow', // allow readers only access to the view file


            'users' =&gt; array('*')


        ),


    );


}





// Called before each action


protected function beforeAction() {


    if (Yii::app()-&gt;user-&gt;checkAccess(ucfirst(&#036;this-&gt;getAction()-&gt;getId())))


        return true;


    else


        throw new CHttpException(400, Yii::t('err', 'You are not authorized to view this page'));


}





/**


 * Declares class-based actions.


 */


public function actions() {


    return array(


        // captcha action renders the CAPTCHA image displayed on the contact page


        'captcha' =&gt; array(


            'class' =&gt; 'CCaptchaAction',


            'backColor' =&gt; 0xFFFFFF,


        ),


        // page action renders &quot;static&quot; pages stored under 'protected/views/site/pages'


        // They can be accessed via: index.php?r=site/page&amp;view=FileName


        'page' =&gt; array(


            'class' =&gt; 'CViewAction',


        ),


    );


}





public function actionUserManagement() {


    &#036;model = new newUserForm;


    &#036;this-&gt;performAjaxValidation(&#036;model);


    if (isset(&#036;_POST['newUserForm'])){


        &#036;model-&gt;attributes = &#036;_POST['newUserForm'];


        if(&#036;model-&gt;validate()){


            Yii::app()-&gt;getController()-&gt;redirect('userSubmit');


        }


    }


    &#036;this-&gt;render('userManagement/userManagementView', array(


            'model' =&gt; &#036;model,


        ));


}


protected function performAjaxValidation(&#036;model){


    if (isset(&#036;_POST['ajax']) &amp;&amp; &#036;_POST['ajax'] === 'add_user_form')


    {


        echo CActiveForm::validate(&#036;model);


        Yii::app()-&gt;end();


    }


}





public function actionUserSubmit() {


   &#036;this-&gt;render('userManagement/submitSuccessful');





}

}

?>

And finally the view:

<?php

&#036;this-&gt;pageTitle = Yii::app()-&gt;name . ' - ' . Yii::t('translation', 'User management');


echo CHtml::tag('h2') . Yii::t('translation', 'User management') . CHtml::closeTag('h2');


echo &quot;&lt;b&gt;&quot; . Yii::t('translation', 'Create user by hand') . &quot;&lt;/b&gt;&#092;n&quot;;


echo CHtml::tag('div', array('class'=&gt;'form')) . &quot;&#092;n&quot;;


    &#036;form = &#036;this-&gt;beginWidget('CActiveForm', array(


                'id' =&gt; 'add_user_form',


                'method' =&gt; 'POST',


                'action' =&gt; 'userManagement',


                'focus'=&gt;array(&#036;model, 'loginname'),


                'enableClientValidation' =&gt; TRUE,


                'enableAjaxValidation' =&gt; TRUE,


                'clientOptions' =&gt; array(


                    'validateOnSubmit' =&gt; TRUE,


                    'validateOnChange' =&gt; TRUE,


                    'validateOnType' =&gt; TRUE,


                    ),


            ));


    ?&gt;

<div class="view">

&lt;p class=&quot;note&quot;&gt;


    &lt;?php echo Yii::t('translation', 'Fields with'); ?&gt;


    &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt;


    &lt;?php echo Yii::t('translation', 'are required.'); ?&gt;


&lt;/p&gt;


&lt;div class=&quot;row&quot;&gt;


    &lt;?php &#036;form-&gt;labelEx(&#036;model, 'username'); ?&gt;


    &lt;?php echo Yii::t('translation', 'Username').&quot;&lt;span class=&#092;&quot;required&#092;&quot;&gt; *&lt;/span&gt;&lt;br&gt;&#092;n&quot;; ?&gt;


    &lt;?php echo &#036;form-&gt;textField(&#036;model, 'username', array('autocomplete' =&gt; 'on')); ?&gt;


    &lt;?php echo &#036;form-&gt;error(&#036;model, 'username') ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


    &lt;?php &#036;form-&gt;labelEx(&#036;model, 'fullName'); ?&gt;


    &lt;?php echo Yii::t('translation', 'Full name').&quot;&lt;span class=&#092;&quot;required&#092;&quot;&gt; *&lt;/span&gt;&lt;br&gt;&#092;n&quot;; ?&gt;


    &lt;?php echo &#036;form-&gt;textField(&#036;model, 'fullName', array('autocomplete' =&gt; 'on')); ?&gt;


    &lt;?php echo &#036;form-&gt;error(&#036;model, 'fullName') ?&gt;


&lt;/div&gt;

<div class="row">

    &lt;?php &#036;form-&gt;labelEx(&#036;model, 'email'); ?&gt;


    &lt;?php echo Yii::t('translation', 'Email').&quot;&lt;span class=&#092;&quot;required&#092;&quot;&gt; *&lt;/span&gt;&lt;br&gt;&#092;n&quot;; ?&gt;


    &lt;?php echo &#036;form-&gt;textField(&#036;model, 'email', array('autocomplete' =&gt; 'on')); ?&gt;


    &lt;?php echo &#036;form-&gt;error(&#036;model, 'email'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


    &lt;?php &#036;form-&gt;labelEx(&#036;model, 'phonenumber'); ?&gt;


    &lt;?php echo Yii::t('translation', 'Phone number') . &quot;&lt;br&gt;&#092;n&quot;; ?&gt;


    &lt;?php echo &#036;form-&gt;textField(&#036;model, 'phonenumber', array('autocomplete' =&gt; 'off')); ?&gt;


    &lt;?php echo &#036;form-&gt;error(&#036;model, 'phonenumber'); ?&gt;


&lt;/div&gt;

</div>

<div class="row submit">

    &lt;?php


    echo CHtml::submitButton(Yii::t('translation', 'Submit'));


    ?&gt;

</div>

    &lt;?php &#036;this-&gt;endWidget(); 


echo CHtml::closeTag('div') . &quot;&lt;br/&gt;&#092;n&quot;;

?>

Best Wishes

Juha Ruokolainen

Please check the posting guidelines - http://www.yiiframew…ing-this-forum/

You will get better help if you make proper posts… the above code is too long and really unreadable…

Sorry, but don’t know how to put this shorter… Anyway, is there something in the model class?




<?php

    class newUserForm extends CFormModel {


            public $username;

            public $fullName;

            public $email;

            public $phonenumber;




    public function rules() {

            return array(

                    array('username, fullName, email', 'required'),

                    array('fullName', 'length', 'max' => 30),

                    array('username', 'length', 'max' => 20),

                    array('username', 'match', 'pattern' => '/^([A-Za-z0-9_\-\.])/'),

                    array('email', 'email', 'checkMX' => TRUE),


                    );

            }

    }


?>



Your RegEx is flawed. Bob+* will be accepted because it starts of with a valid character. Try this: /^[a-z0-9_\-\.]+$/i

That did the trick! Thanks a lot :)