Create User Fields Validation

hi in my first application of yii., create user operation z der., its accepting null values also., how to validate it so that it shd contain values in field/…!! plz help me soon…

Check Yii Validation

Below code in Model file,




public function rules()

{

    return array(

        array('your_required_field_here', 'required'),

thanks., its working…, but caught by one more problem dat cost field z taking default value so that wen i submit form widout entering cost., its taking 0 value. in db i dint set any default value.

before you are going to check if model is set like


$model = new User();

$model->cost = "default value";


if(isset($_POST['User])){

	$model->attributes = $_POST['User'];

}

so if you didn’t assign any value in form field it will take defalut value

if you gave value in form so default value will be replaced by form value inside IF …

hi as i am new to yii can u expand your answer where i need to do and why…?? plz explain steps.

In Ur Controller actionCreate edit just like below code

lets User is ur Model Name for User Table and cost is Model Attribute for for User Table


$model = new User();

$model->cost = "default value";


if(isset($_POST['User])){

        $model->attributes = $_POST['User'];

}

when you click on form Submit btn the control goes to actionCreate in ur Controller

first it will create an instance of ur Model then it will assign a default value(which u have to change)

to cost field

then it check if all required fields are set , if all required fields are set so it will assign all form values

to thier respective model attributes(fields)

so lets you have not given any value to cost field in ur form so it will save default value to ur db which

is before IF statement

and if you assign any value to cost field in form so inside IF Statement cost value will be replaced by form value

no., dint get., still its taking 0 only if i submit without entering cost value… :(

sorry dear…

B/c in IF statement , it is





$model->attributes = $_POST['User'];




so cost is also included in this…

so you have to chck cost with just one line





if(isset($_POST['User])){

        $model->attributes = $_POST['User'];

        if(!isset($_POST['cost'])){

            $model->cost = 'default value'; 

 		}

	$model->save();

}




remove this line above IF Statement




$model->cost = 'default value'; 

hi thank you so much., working now…:)

hi.,

i need to insert the logged in user into database and i have one more option as operation that my products, when user click that option it should show the products added by him… plz help me to achieve this.,

You can get Logged in user ID like this





$id=Yii::app()->user->id;




for more info about User check below link

http://www.yiiframework.com/wiki/6/how-to-add-more-information-to-yii-app-user/

ya got that and its inserting username also but the problem getting is., its inserting 2 rows, 1st row with username only and second row with created details. so here id is incrementing twice… how to resolve this…?? my id attribute is auto increment.,but here incrementing twice… can anybody plzzzzzzzzzzzzzzz help me… wts d issue here…??

Can U plz paste ur full code of inserting User into DB ?

my code z here in create.php

<?php

/* @var $this DemoController */

/* @var $model Demo */

$this->breadcrumbs=array(

'Demos'=&gt;array('index'),


'Create',

);

$this->menu=array(

array('label'=&gt;'List Demo', 'url'=&gt;array('index')),


array('label'=&gt;'Manage Demo', 'url'=&gt;array('admin')),

);

?>

<h1>Create Demo</h1>

<?php echo $this->renderPartial(’_form’, array(‘model’=>$model));

$connection = Yii::app()->db;

$username = Yii::app()->user->name;

$sql = "INSERT INTO tbl_demo(id,Productname, Brand, Cost,User)

VALUES(:id, :Productname, :Brand, :Cost,:User)";

$command=$connection->createCommand($sql);

$command->bindValue(":id", $_REQUEST);

$command->bindValue(":Productname", $_REQUEST);

$command->bindValue(":Brand", $_REQUEST);

$command->bindValue(":Cost", $_REQUEST);

$command->bindValue(":User", $username);

$command->execute();

?>

as my id z auto increment., i deleted n executed but stl d same effect., incrementing twice…

If Ur Scenario is like this that

when u submit form then u want to save User into db

then plz write the below code in ur

controller->actionCreate->IF statement (if(isset($_POST[‘model’])))





$connection = Yii::app()->db;

$username = Yii::app()->user->name;

$sql = "INSERT INTO tbl_demo(id,Productname, Brand, Cost,User)

VALUES(:id, :Productname, :Brand, :Cost,:User)";

$command=$connection->createCommand($sql);

$command->bindValue(":id", $_REQUEST);

$command->bindValue(":Productname", $_REQUEST);

$command->bindValue(":Brand", $_REQUEST);

$command->bindValue(":Cost", $_REQUEST);

$command->bindValue(":User", $username);

$command->execute();




username is not inserting., only productname, brand, cost is inserting :(

have u tested





$username = Yii::app()->user->name;

echo $username;




that username is returning well…

ya tested and it was printing…, in db also its inserting but only username in 1 row and all the contents in other row.

i think it was my mistake that when i genereted crud n model at that time i dint added user field., now i changed complete db and genereted model n crud and i should insertusername into db… now getting error that inserting 2 times only username., 2 times created contents…

try this





$sql="INSERT INTO tbl_user (username, email) VALUES(:username,:email)"; 

$command=$connection->createCommand($sql); 

$command->bindParam(":username",$username,PDO::PARAM_STR); 

$command->bindParam(":email",$email,PDO::PARAM_STR);

$command->execute();