I am a newbie in Yii. I have created a page where user can change their password.
So in my changePassword view I have :
<div class="row">
<?php echo $form->labelEx($model,'oldpwd'); ?>
<?php echo $form->textField($model,'oldpwd'); ?>
<?php echo $form->error($model,'oldpwd'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'pwd'); ?>
<?php echo $form->textField($model,'pwd'); ?>
<?php echo $form->error($model,'pwd'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'pwd_repeat'); ?>
<?php echo $form->passwordField($model,'pwd_repeat'); ?>
<?php echo $form->error($model,'pwd_repeat'); ?>
</div>
Now obviously I am getting an error as only the field 'pwd' is in the table and thereby in the model. I am new to MVC frameworks and can use some help here. Thanks
Declare them in your model as property of model First..
public $old_pwd;
public $pwd;
public $pwd_repeat;
As you are asking model Labels of these attributes..define them in your attributeLabels function in model..
public function attributeLabels()
{
return array(
'old_pwd'=>'Old Passw....',
'.....same way for all those who are not already there..'
);
}
Declare them safe in rules if required...
add the following line to your model
public $old_pwd;
public $pwd_repeat;
we instruct Yii to use this field as virtual field instead of searching in database field
Related
I have three routes in web.php
Route::get('/secondary', 'SecondaryController#show');
Route::get('/primary', 'PrimaryController#show');
Route::get('/nursery', 'NurseryController#show');
But when i click on the respective menu link, it presents the first route, others just wont work.
pls what am i doing wrongly, i need help.
this is code for the controllers
for the nursery controller
public function show($slugs){
$NurseryPages = NurseryPages::findByURL($slugs);
return view('nursery.show', ['NurseryPages' =>$NurseryPages]);
}
for the primary controller
public function show($slugs){
$PrimaryPages = PrimaryPages::findByURL($slugs);
return view('primary.show', ['PrimaryPages' =>$PrimaryPages]);
}
for the secondary controller
public function show($slugs)
{
$SecondaryPages = SecondaryPages::findByURL($slugs);
return view('secondary.show', ['SecondaryPages' =>$SecondaryPages]);
}
it will only work well for the nursery section, but on others it displays error: trying to get object of non-property and refers me back to the nursery.show file
this is the error msg
ErrorException (E_ERROR)
Trying to get property of non-object (View: C:\xampp\htdocs\acadapp\resources\views\secondary\show.blade.php)
<?php echo $__env->make('inc.secondary.navbar', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
<div class="container">
<div class="row">
<div class="col-md-12">
<br/><br/><br/>
<b><h3><?php echo $SecondaryPage->title; ?></b></h3>
<?php echo $SecondaryPage->body; ?>
</div>
</div>
</div>
<?php echo $__env->make('inc.secondary.footer', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
I think you need to change your route like:
Route::get('/secondary/{slug}', 'SecondaryController#show');
Route::get('/primary/{slug}', 'PrimaryController#show');
Route::get('/nursery/{slug}', 'NurseryController#show');
I am new in Yii framework. I am using Chtml form to send data from view to controller and want to print the result and insert it to database. But in my case its showing an empty array. I couldn't able to understand where I am doing wrong. So please help me regarding this.
in controller
public function actionTest1()
{
$obj = new Pad;
if(isset($_POST))
print_r($_POST);// to check the desired result
if(isset($_POST['Pad']))
{
$obj->attributes=$_POST['Pad'];
if($obj->save()) // insert the data
$this->redirect(array('view','id'=>$obj->id));
}
}
in view
<?php echo CHtml::beginForm('','post',array('id'=>'step1Form')); ?>
<div class="stopPad">
<div class="floatLeft padTop5 marRight5">
<?php
echo CHtml::radioButton('stoppad',false,array('value'=>'Stop Pad after goal is reached'));
?>
</div>
<div class="currencyText padTop4 floatLeft">Stop Pad after goal is reached</div>
<div class="clearBoth"></div>
</div>
<div class="stopPad">
<div class="floatLeft padTop5 marRight5">
<?php
echo CHtml::radioButton('stoppad',false,array('value'=>'No limites'));
?>
</div>
<div class="currencyText padTop4 floatLeft">No limites</div>
<div class="clearBoth"></div>
</div>
<div class="nextText">
<?php echo CHtml::link('Next >',array('pad/test1'));?>
</div>
<?php echo CHtml::endForm(); ?>
Please help. Thanks in advance!
You are not submitting your form and of course $_POST will be empty. Line below is just a link:
<?php echo CHtml::link('Next >',array('pad/test1'));?>
You must change it to :
<?php echo CHtml::submitButton('Next >');?>
And also add an action to your form.
Updating the question with the changes I have done. Its redirect me to pad/test1 showing array() as result.
in controller
public function actionTest1()
{
$obj = new Pad;
if(isset($_POST))
CVarDumper::dump($_POST);// to check the desired result
if(isset($_POST['Pad']))
{
$obj->attributes=$_POST['Pad'];
if($obj->save()) // insert the data
echo "=====";
}
}
in view
<?php echo CHtml::beginForm( '','post'); ?>
<div class="stopPad">
<div class="floatLeft padTop5 marRight5">
<?php
echo CHtml::radioButton('stoppad',false,array('value'=>'Stop Pad after goal is reached'));
?>
</div>
<div class="currencyText padTop4 floatLeft">Stop Pad after goal is reached</div>
<div class="clearBoth"></div>
</div>
<div class="stopPad">
<div class="floatLeft padTop5 marRight5">
<?php
echo CHtml::radioButton('stoppad',false,array('value'=>'No limites'));
?>
</div>
<div class="currencyText padTop4 floatLeft">No limites</div>
<div class="clearBoth"></div>
</div>
<div class="nextText">
<?php echo CHtml::button('Next >', array('submit' => array('pad/test1'))); ?>
</div>
<?php echo CHtml::endForm(); ?>
A common source of confusion among new Yii users is how the 'safe' validator works, how it works with other validators, and why it's necessary in the first place. This article means to clear up this confusion, as well as explain the notion of Massive Assignment.
http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules/
I am using renderParial() function to render views->subscriber->_form from views->layouts->main.php
Problem i am getting is I am failed to insert data using this. Gii generated CRUD is working perfectly but when i render _form and want to insert data in database it's not working.
Here is my code.
main.php
<div class="col-lg-6" style="text-align:right;">
<span>Subscribe Newsletter</span>
<div style="float:right;margin-left:10px;">
<?php
$model=new Subscription();
$this->renderPartial("/subscription/_form",array('model'=>$model));?>
</div>
and _form.php
<?php
/* #var $this SubscriptionController */
/* #var $model Subscription */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'subscription-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->textField($model,'email',array('size'=>30,'maxlength'=>30,'placeholder'=>'Email Address')); ?>
<?php echo $form->error($model,'email'); ?>
<?php echo CHtml::submitButton($model->isNewRecord ? 'Subscribe' : 'Save',array('class'=>'btn btn-xs btn-success subscribeBtn')); ?>
</div>
<?php $this->endWidget(); ?>
</div>
As adamS and Michiel have commented, if you want to put a form or data in your main.php layout file, you should use a widget.
To create a widget, you need to do the following:
1: Create a php file in your /protected/components/ dir, something like SubscriptionWidget.php
2: Create a dir views in your components dir
3: Create your view .php file in your /protected/components/views/, something like subscriptionWidget.php
4: Put the following code in your SubscriptionWidget.php file:
<?php
class SubscriptionWidget extends CWidget
{
public function init()
{
}
public function run()
{
$model = new SubscriptionForm;
if(isset($_POST['SubscriptionForm']))
{
// proces the data
}
$this->render('subscriptionWidget', array('model'=>$model));
}
}
?>
Your widget is done. All you need to do now is call it in your main.php layout file, like so:
<!doctype html>
<html lang="en">
...
<?php $this->widget('SubscriptionWidget'); ?>
...
</html>
Also, don't forget to put the form in your newly created view file.
Hope this helps.
Try adding one more slash
$this->renderPartial("//subscription/_form",array('model'=>$model));
I have a user creation form for User model (User extends CActiveRecord), with some regular fields like username, password or e-mail.
The fields of the form are created in the view like:
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'superuser'); ?>
<?php echo $form->dropDownList($model, 'superuser', User::itemAlias('AdminStatus')); ?>
<?php echo $form->error($model,'superuser'); ?>
</div>
I actually can create users with this form, but now I need to add extra information.
What I need is to add a dropdown with 'City' list (which I would retrieve from DB via Ajax) and send the corresponding city_id to the UserController to do some stuff and add an extra field to the User based on the city_id, before saving. So this city_id does not belong to the model, I just need it in the controller to do some calculation.
Looks like a simple question, but right now I do not know ho to solve it. Thanks in advance.
You have two options:
Add a class variable in your model.
public $city_id;
Then you can call in view as below
<div class="row">
<?php echo $form->labelEx($model,'city_id'); ?>
<?php echo $form->dropDownList($model, 'city_id', $urcity_arr; ?>
<?php echo $form->error($model,'city_id'); ?>
</div>
2.You can do with CHTML
But you can access in controller as $_POST["city_id"];
<?php echo CHtml::dropdownlist('city_id', 'selected value',$urcity_arr); ?>
Option 1: as #kumar_v said
Option 2: to add city_id as an attribute to the User Model
class User extends CActiveRecord{
public $city_id ;
...
}
I just started working with yii and I stumbled upon a problem.
I have a table called "users" and a table called "messages".
I basically want to have a page where I can view a user's details but also send him a message that would be saved in the message table.
I have a view called "user/view.php" which consists of:
<h1>View User #<?php echo $model->id; ?></h1>
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'username',
'first_name',
'last_name',
),
)); ?>
<?php $message=new Message;
echo $this->renderPartial('_messagesend', array('model'=>$message)); ?>
the _messagesend form (created using gii) looks like:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'message-_messagesend-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
//for the sake of simplicity lets just insert the id's manually for now
<div class="row">
<?php echo $form->labelEx($model,'idFrom'); ?>
<?php echo $form->textField($model,'idFrom'); ?>
<?php echo $form->error($model,'idFrom'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'idTo'); ?>
<?php echo $form->textField($model,'idTo'); ?>
<?php echo $form->error($model,'idTo'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'msg'); ?>
<?php echo $form->textField($model,'msg'); ?>
<?php echo $form->error($model,'msg'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Send'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
I have a simple view in my UserController to display the details info:
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id)
));
}
And now I want to figure out how i can add the controller to save the message. After gii creation I get a code which I tried to use and modify a little bit:
public function actionMessagesend()
{
$model=new Message;
if(isset($_POST['Message']))
{
$model->attributes=$_POST['Message'];
if($model->save()){
$this->redirect(array('admin'));
}
}
$this->render('_messagesend',array('model'=>$model));
}
I tried to add this controller function in the UserController.php but it doesn't seem to work, I tried to add the same function to MessageController.php but it also doesn't seem to work. I tried to remove all the code and only add a redirect to show if the controllercode actually hits but it doesn't redirect (i tried it both in usercontroller and messagecontroller). So I have a feeling my code isn't reached. You guys have any idea what I'm missing here?
Maybe a little extra question: Is there a better suggestion to do what I want to do?
Thanks a lot!
Best regards,
WtFudgE
Your message sending action isn't reached because by default the CActiveForm submits to the current page, so you will have to add the message processing to the view action, or you will need to set the action property for your CActiveForm.
Here is the link to the property documentation: http://www.yiiframework.com/doc/api/1.1/CActiveForm#action-detail
So the form widget in the view should be something like:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'message-_messagesend-form',
'enableAjaxValidation'=>false,
'action' => array( '/message/messageSend' ), // change depending on your project
)); ?>
You can also achieve this by editing the submit button in the view page like this:
<?php echo CHtml::button('Send', array('submit' => array('message/messageSend')));?>