[SOLVED]registration doesn't get save to db

i spent hours trying to figure out how to make this password repeat validation to work.

and it eventually works, but what happens now is, it doesn’t save the data to the db, but it does send the email activation code

here’s my model class





<?php


/**

 * This is the model class for table "wsmembers".

 *

 * The followings are the available columns in table 'wsmembers':

 * @property string $MemberShipID

 * @property string $WSEmailAddress

 * @property string $WSLoginName

 * @property string $WSLoginPassword

 * @property string $salt

 * @property integer $WSEmailConfirmed

 * @property string $WSMembershipDateSetup

 * @property string $WSMembershipDateConfirmed

 * @property string $WSMembershipContactName

 * @property string $WSMembershipSecretQuestion

 * @property string $WSMembershipSecretAnswer

 * @property integer $WSMembershipLoginAttempts

 * @property integer $WSMembershipLockedOut

 * @property string $WSMembershipLockOutDateTime

 * @property integer $WSMembershipSystemLockedOut

 * @property string $WSLoginNameAllUsers

 * @property string $WSLoginPasswordAllUsers

 * @property integer $IntroducingMemberID

 *

 * The followings are the available model relations:

 */

class Wsmembers extends CActiveRecord

{

	public $WSLoginPassword_repeat;

	public $verifyCode;

	public $emailed;

	/**

	 * Returns the static model of the specified AR class.

	 * @return Wsmembers the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'wsmembers';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('WSEmailAddress, WSLoginName, WSLoginPassword, WSMembershipContactName, WSMembershipSecretQuestion, WSMembershipSecretAnswer', 'required'),

			array('WSEmailAddress, WSLoginName' , 'unique'),

			array('WSLoginPassword','compare','compareAttribute' => 'WSLoginPassword_repeat',),

			array('WSLoginPassword_repeat','safe'),

			array('WSEmailConfirmed, WSMembershipLoginAttempts, WSMembershipLockedOut, WSMembershipSystemLockedOut, IntroducingMemberID', 'numerical', 'integerOnly'=>true),

			array('WSEmailAddress, WSLoginName, WSLoginPassword, WSMembershipContactName, WSLoginNameAllUsers', 'length', 'max'=>50),

			array('WSLoginPasswordAllUsers', 'length', 'max'=>40),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('MemberShipID, WSEmailAddress, WSLoginName, WSLoginPassword, WSEmailConfirmed, WSMembershipDateSetup, WSMembershipDateConfirmed, WSMembershipContactName, WSMembershipSecretQuestion, WSMembershipSecretAnswer, WSMembershipLoginAttempts, WSMembershipLockedOut, WSMembershipLockOutDateTime, WSMembershipSystemLockedOut, WSLoginNameAllUsers, WSLoginPasswordAllUsers, IntroducingMemberID', 'safe', 'on'=>'search'),

			array('verifyCode', 'captcha', 'allowEmpty' => !extension_loaded('gd')),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'company' => array(self::HAS_MANY, 'Wsmembersdetails','MemberShipID')

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'MemberShipID' => 'Member Ship',

			'WSEmailAddress' => 'Email Address',

			'WSLoginName' => 'Login Name',

			'WSLoginPassword' => 'Password',

			#'WSLoginPassword_repeat' => 'Password repeat',

			#'salt' => 'Salt',

			'WSEmailConfirmed' => 'Wsemail Confirmed',

			'WSMembershipDateSetup' => 'Wsmembership Date Setup',

			'WSMembershipDateConfirmed' => 'Wsmembership Date Confirmed',

			'WSMembershipContactName' => 'Contact Name',

			'WSMembershipSecretQuestion' => 'Secret Question',

			'WSMembershipSecretAnswer' => 'Secret Answer',

			'WSMembershipLoginAttempts' => 'Wsmembership Login Attempts',

			'WSMembershipLockedOut' => 'Wsmembership Locked Out',

			'WSMembershipLockOutDateTime' => 'Wsmembership Lock Out Date Time',

			'WSMembershipSystemLockedOut' => 'Wsmembership System Locked Out',

			'WSLoginNameAllUsers' => 'Wslogin Name All Users',

			'WSLoginPasswordAllUsers' => 'Wslogin Password All Users',

			'IntroducingMemberID' => 'Introducing Member',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('MemberShipID',$this->MemberShipID,true);

		$criteria->compare('WSEmailAddress',$this->WSEmailAddress,true);

		$criteria->compare('WSLoginName',$this->WSLoginName,true);

		$criteria->compare('WSLoginPassword',$this->WSLoginPassword,true);

		#$criteria->compare('salt',$this->salt,true);

		$criteria->compare('WSEmailConfirmed',$this->WSEmailConfirmed);

		$criteria->compare('WSMembershipDateSetup',$this->WSMembershipDateSetup,true);

		$criteria->compare('WSMembershipDateConfirmed',$this->WSMembershipDateConfirmed,true);

		$criteria->compare('WSMembershipContactName',$this->WSMembershipContactName,true);

		$criteria->compare('WSMembershipSecretQuestion',$this->WSMembershipSecretQuestion,true);

		$criteria->compare('WSMembershipSecretAnswer',$this->WSMembershipSecretAnswer,true);

		$criteria->compare('WSMembershipLoginAttempts',$this->WSMembershipLoginAttempts);

		$criteria->compare('WSMembershipLockedOut',$this->WSMembershipLockedOut);

		$criteria->compare('WSMembershipLockOutDateTime',$this->WSMembershipLockOutDateTime,true);

		$criteria->compare('WSMembershipSystemLockedOut',$this->WSMembershipSystemLockedOut);

		$criteria->compare('WSLoginNameAllUsers',$this->WSLoginNameAllUsers,true);

		$criteria->compare('WSLoginPasswordAllUsers',$this->WSLoginPasswordAllUsers,true);

		$criteria->compare('IntroducingMemberID',$this->IntroducingMemberID);

		#$criteria->compare('MemberShipID', Yii::app()->user->id,true);


		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));

	}

	

	

//	public function afterValidate()

//	{

//		parent::afterValidate();

//		$pass = $this->encrypt($this->WSLoginPassword);

//		$this->WSLoginPassword = $pass;

//		return true;

//	}

//	

//	public function encrypt($value)

//	{

//		return sha1($value);

//	}


}



//heres my controller class




	public function actionRegister()

	{

		$form = new Wsmembers;

		

		if(isset($_POST['Wsmembers']))

		{

			$form->attributes = $_POST['Wsmembers'];


			if($form->validate())

			{

				$pass = sha1($_POST['Wsmembers']['WSLoginPassword']);

				$form->WSEmailAddress = $_POST['Wsmembers']['WSEmailAddress'];

				$form->WSLoginName = $_POST['Wsmembers']['WSLoginName'];

				$form->WSLoginPassword = $pass;

				$form->WSMembershipContactName = $_POST['Wsmembers']['WSMembershipContactName'];

				$form->WSMembershipSecretQuestion = $_POST['Wsmembers']['WSMembershipSecretQuestion'];

				$form->WSMembershipSecretAnswer = $_POST['Wsmembers']['WSMembershipSecretAnswer'];

				$form->WSMembershipDateSetup = new CDbExpression('NOW()');

				$form->save();


				#if($form->save()){

					Yii::app()->user->setFlash('success','Thank you for joining!,Please check your email and activate your account');

					

					$to = $_POST['Wsmembers']['WSEmailAddress'];

					$subject = "Welcome To Buggyman";

					$message = "Thank you for joining!, we have sent you a separate email that contains your activation link";

					$from = "FROM: buggyman@gmail.com";

					

					mail($to,$subject,$message,$from);

					

					$headers  = 'MIME-Version: 1.0' . "\r\n";

					$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

					$headers .= 'From: Buggy man<buggyman@gmail.com>' . "\r\n";

					

					$subject2 = "Your Activation Link";


					$message2 = "<html><body>Please click this below to activate your membership<br />".

								 Yii::app()->createAbsoluteUrl('wsmembers/activate', array('email' => $_POST['Wsmembers']['WSEmailAddress'])).

								"</body></html>";

								

					#mail($to, $subject2, $message2, $headers);

				#}

								

				$this->redirect($this->createUrl('register', array('r'=>'wsmembers/register')));

			}

		}

		$this->render('register', array('form' => $form));

	}



Hi sasori,

Most of the times the problem comes because the attributes were not declared on rules. Make sure your object’s attributes are all properly declared and for those who don’t require specific validation, mark them as ‘safe’

EDIT:

I have seen the code and it looks that you have set the required properties correctly…

There is no errors at all displayed?

EDIT:

There is more… Why you set the attributes of the form and then, re-set them with the FORM POSTed values? Apart from the password why you need to re-configure the object?

////

$form->attributes = $_POST[‘Wsmembers’];

if($form->validate())

{

 &#036;pass = sha1(&#036;_POST['Wsmembers']['WSLoginPassword']);


 &#036;form-&gt;WSLoginPassword = &#036;pass;

// This i don’t understand

 &#036;form-&gt;WSEmailAddress = &#036;_POST['Wsmembers']['WSEmailAddress'];


 &#036;form-&gt;WSLoginName = &#036;_POST['Wsmembers']['WSLoginName'];

.

.

.

// ok, but this could be done in onBeforeSave() checking if isNewRecord

$form->WSMembershipDateSetup = new CDbExpression(‘NOW()’);

$form->save();

.

.

.

in the controller use


$form->save(false);

in order to avoid the validation on save.

This validation will always fail because you Sha1 only one password.

i changed it to







	public function actionRegister()

	{

		$form = new Wsmembers;

		

		if(isset($_POST['Wsmembers']))

		{

			$form->attributes = $_POST['Wsmembers'];

//			var_dump($_POST);

//			var_dump($form->scenario);

//			var_dump($form->attributes);

			if($form->validate())

			{

				$pass = sha1($_POST['Wsmembers']['WSLoginPassword']);

				#$form->WSEmailAddress = $_POST['Wsmembers']['WSEmailAddress'];

				#$form->WSLoginName = $_POST['Wsmembers']['WSLoginName'];

				$form->WSLoginPassword = $pass;

				#form->WSMembershipContactName = $_POST['Wsmembers']['WSMembershipContactName'];

				#$form->WSMembershipSecretQuestion = $_POST['Wsmembers']['WSMembershipSecretQuestion'];

				#$form->WSMembershipSecretAnswer = $_POST['Wsmembers']['WSMembershipSecretAnswer'];

				#$form->WSMembershipDateSetup = new CDbExpression('NOW()');

				$form->save();



and added




	public function onBeforeSave()

	{

		$form->WSMemberShipDateSetup = new CDbExpression("NOW()");

		$form->save();

	}



still no dice :(

You have to pass FALSE as parameter of the save function, like that:




$form->save(false);

No on before save are needed, at your first posted code you have to add false to the save.

problem solved, you are both right

what i did is I removed these lines




//				$form->WSEmailAddress = $_POST['Wsmembers']['WSEmailAddress'];

//				$form->WSLoginName = $_POST['Wsmembers']['WSLoginName'];

//				$form->WSMembershipContactName = $_POST['Wsmembers']['WSMembershipContactName'];

//				$form->WSMembershipSecretQuestion = $_POST['Wsmembers']['WSMembershipSecretQuestion'];

//				$form->WSMembershipSecretAnswer = $_POST['Wsmembers']['WSMembershipSecretAnswer'];



then everything went back to normal , yii yii yii …thanks for the help guys :)

You did 100% right.

There is the function attributes() for do the assignament, never write again something like $_POST[‘Wsmembers’][‘WSEmailAddress’]!

Just trust yii and use his instrument as you did today ( $form->attributes = $_POST[‘Wsmembers’];)

Thanks! One more thing sasori, make sure you do the onbeforesave function like this:




public function onBeforeSave(){

    if(parent::onBeforeSave(){

      if($this->isNewRecord)

         $form->WSMemberShipDateSetup = new CDbExpression("NOW()");

    }

    return false;

}



It doesnt need to save on it