Yii Framework Forum: Create User Fields Validation - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Create User Fields Validation Validation in yii Rate Topic: -----

#1 User is offline   Latha 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 17-June 13

Posted 24 June 2013 - 03:40 AM

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

#2 User is offline   kiran sharma 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 492
  • Joined: 21-May 11
  • Location:India

Posted 24 June 2013 - 03:57 AM

Check Yii Validation

Below code in Model file,
public function rules()
{
    return array(
        array('your_required_field_here', 'required'),


View PostLatha, on 24 June 2013 - 03:40 AM, said:

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

Thanks,
Kiran Sharma.
0

#3 User is offline   Latha 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 17-June 13

Posted 24 June 2013 - 04:55 AM

View Postkiran sharma, on 24 June 2013 - 03:57 AM, said:

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

#4 User is offline   Lal Zada 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 06-November 12
  • Location:Lahore, Pakistan

Posted 24 June 2013 - 06:21 AM

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 ...
Is it fast? Is it secure? Is it professional? Is it right for my next project? Yes, it is

Linkedin Facebook Twitter Google+
0

#5 User is offline   Latha 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 17-June 13

Posted 24 June 2013 - 06:56 AM

View PostLal Zada, on 24 June 2013 - 06:21 AM, said:

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

#6 User is offline   Lal Zada 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 06-November 12
  • Location:Lahore, Pakistan

Posted 24 June 2013 - 07:12 AM

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
Is it fast? Is it secure? Is it professional? Is it right for my next project? Yes, it is

Linkedin Facebook Twitter Google+
0

#7 User is offline   Latha 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 17-June 13

Posted 24 June 2013 - 07:17 AM

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

#8 User is offline   Lal Zada 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 06-November 12
  • Location:Lahore, Pakistan

Posted 24 June 2013 - 07:23 AM

View PostLatha, on 24 June 2013 - 07:17 AM, said:

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'; 

Is it fast? Is it secure? Is it professional? Is it right for my next project? Yes, it is

Linkedin Facebook Twitter Google+
0

#9 User is offline   Latha 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 17-June 13

Posted 24 June 2013 - 07:27 AM

View PostLal Zada, on 24 June 2013 - 07:23 AM, said:

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

#10 User is offline   Latha 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 17-June 13

Posted 25 June 2013 - 12:01 AM

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

#11 User is offline   Lal Zada 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 06-November 12
  • Location:Lahore, Pakistan

Posted 25 June 2013 - 02:03 AM

You can get Logged in user ID like this


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



for more info about User check below link

http://www.yiiframew...o-yii-app-user/

Is it fast? Is it secure? Is it professional? Is it right for my next project? Yes, it is

Linkedin Facebook Twitter Google+
0

#12 User is offline   Latha 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 17-June 13

Posted 25 June 2013 - 02:10 AM

View PostLal Zada, on 25 June 2013 - 02:03 AM, said:

You can get Logged in user ID like this


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



for more info about User check below link

http://www.yiiframew...o-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...??
0

#13 User is offline   Lal Zada 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 06-November 12
  • Location:Lahore, Pakistan

Posted 25 June 2013 - 03:20 AM

Can U plz paste ur full code of inserting User into DB ?
Is it fast? Is it secure? Is it professional? Is it right for my next project? Yes, it is

Linkedin Facebook Twitter Google+
0

#14 User is offline   Latha 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 17-June 13

Posted 25 June 2013 - 03:36 AM

View PostLal Zada, on 25 June 2013 - 03:20 AM, said:

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'=>array('index'),
'Create',
);

$this->menu=array(
array('label'=>'List Demo', 'url'=>array('index')),
array('label'=>'Manage Demo', 'url'=>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..
0

#15 User is offline   Lal Zada 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 06-November 12
  • Location:Lahore, Pakistan

Posted 25 June 2013 - 03:54 AM

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();


Is it fast? Is it secure? Is it professional? Is it right for my next project? Yes, it is

Linkedin Facebook Twitter Google+
0

#16 User is offline   Latha 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 17-June 13

Posted 25 June 2013 - 04:11 AM

View PostLal Zada, on 25 June 2013 - 03:54 AM, said:

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 :(
0

#17 User is offline   Lal Zada 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 06-November 12
  • Location:Lahore, Pakistan

Posted 25 June 2013 - 04:35 AM

have u tested

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



that username is returning well...

Is it fast? Is it secure? Is it professional? Is it right for my next project? Yes, it is

Linkedin Facebook Twitter Google+
0

#18 User is offline   Latha 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 17-June 13

Posted 25 June 2013 - 04:45 AM

View PostLal Zada, on 25 June 2013 - 04:35 AM, said:

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

#19 User is offline   Latha 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 17-June 13

Posted 25 June 2013 - 05:11 AM

View PostLatha, on 25 June 2013 - 04:45 AM, said:

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

#20 User is offline   Lal Zada 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 06-November 12
  • Location:Lahore, Pakistan

Posted 25 June 2013 - 05:17 AM

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();



Is it fast? Is it secure? Is it professional? Is it right for my next project? Yes, it is

Linkedin Facebook Twitter Google+
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users