creating a Yii calculator - yii

I'm new to Yii and MVC, and still trying to figure things out.
I am trying to create a calculator in Yii, below is my file directory for my calculators modules.
Now in my DefaultController.php I have this
<?php
class DefaultController extends Controller
{
public function actions() {
return array(
'page' => array('class' => 'CViewAction'),
);
}
public function actionIndex()
{
$this->render('index');
}
public function actionLoanCalculator()
{
/* loan calculations goes here?? */
}
}
And in my CalculatorsModule.php
<?php
class CalculatorsModule extends CWebModule
{
public function init()
{
// this method is called when the module is being created
// you may place code here to customize the module or the application
// import the module-level models and components
$this->setImport(array(
'calculators.models.*',
'calculators.components.*',
));
$model = new CalculatorsForm();
}
public function beforeControllerAction($controller, $action)
{
if(parent::beforeControllerAction($controller, $action))
{
// this method is called before any module controller action is performed
// you may place customized code here
return true;
}
else
return false;
}
}
Here is my loan-calculator.php
<div class="form-box ">
<div class="form-body">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'calc-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
'validateOnChange'=>true,
),
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="form-group">
<?php echo $form->labelEx($model,'price'); ?>
<?php echo $form->textField($model,'price', array('class'=> 'form-control')); ?>
</div>
<div class="form-group">
<?php echo $form->labelEx($model,'downpayment'); ?>
<?php echo $form->textField($model,'downpayment', array('class'=> 'form-control')); ?>
</div>
<div class="form-group">
<?php echo CHtml::submitButton('Submit', array('class' => 'btn btn-lg btn-primary btn-block')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<?php endif; ?>
</div>
in my models/CalculatorsForm.php folder i have this
<?php
class CalculatorsForm extends CFormModel
{
public $price;
public $downpayment;
/**
* Declares the validation rules.
* title is required
*/
public function rules()
{
return array(
);
}
/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
'price'=>'Price',
'downpayment'=>'Down Payment',
);
}
}
?>
Using ajax, how would I get the input from my loan-calculator.php calculate it in the backend and send the results back to loan-calculator.php
I know i could use JavaScript, but i do not want to share my calculation formula's.
Secondly i'm getting this error when viewing domainname.com/dev/loan-calculator
Undefined variable: model
Any ideas?
Thanks in advance.

Use ajaxSubmitButton:
http://www.yiiframework.com/doc/api/1.1/CHtml#ajaxSubmitButton-detail
You can use this to send the form data to your action and then post back some result and use it with Javascript.
Should look something like this:
<?php
echo CHtml::ajaxSubmitButton('blah txt', array('/default/loanCalculator'), array(
'success' => 'function(data) {
// 'data' will be the output of the loanCalculator action
}',
));
?>

Related

Yii2 How can I display Activeform radioList data on screen?

I have not experience with Yii2.
I would like to be able to show the result of a form on the screen.
With the fields name and email I have no problem but I can not show the selection
that I make of my radioList.
I have tried many methods but none works.
Can you help me please?
These are my files.
SiteController.php
public function actionEntry()
{
$this->layout = 'print';
$model = new EntryForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
return $this->render('entry-confirm', ['model' => $model]);
} else {
return $this->render('entry', ['model' => $model]);
}
}
EntryForm.php
namespace app\models;
use Yii;
class EntryForm extends \yii\db\ActiveRecord
{
public $name;
public $email;
public $category;
public function rules()
{
return [
[['name', 'email'], 'required'],
['email', 'email'],
];
}
entry.php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'category')->radioList([
1 => 'radio 1',
2 => 'radio 2'
]);
?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
entry-confirm.php
<p>You have entered the following information:</p>
<ul>
<li><label>Name</label>: <?= Html::encode($model->name) ?></li>
<li><label>Email</label>: <?= Html::encode($model->email) ?></li>
<li><label>Category</label>: <?= Html::encode($model->category) ?></li>
</ul>
This is the image with the problem:
In model extending ActiveRecord you must never explicitly define properties that are the same as names of columns in DB.
You must define at least one validation rule for each attribute that is set by the end user, otherwise system will not allow to set it.

YiiBooster modal Dataprovider Undefined

I am trying to load the index view from another model view in a modal window using yiibooster but the dataprovider of this model looks like it is undefined:
<?php $this->beginWidget(
'bootstrap.widgets.TbModal',
array('id' => 'documentacion')
); ?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4>Documentación</h4>
</div>
<?php
$doc = new ZfDocumentacionInmueble;
$this->renderPartial('//ZfDocumentacionInmueble/index', array('model'=>$doc));
?>
<div class="modal-footer">
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'label' => 'Cerrar',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)
); ?>
</div>
<?php $this->endWidget(); ?>
ERROR: Undefined variable: dataProvider
thanks!
ERROR:
C:\APP\htdocs\yii\yiitest\protected\views\zfDocumentacionInmueble\index.php(6)
1 <?php
2 /* #var $this ZfDocumentacionInmuebleController */
3 /* #var $dataProvider CActiveDataProvider */
4
5 $this->widget('zii.widgets.CListView', array(
6 'dataProvider'=>$dataProvider,
7 'itemView'=>'_view',
8 )); ?>
CONTROLLER CODE:
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('ZfDocumentacionInmueble');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
If the "real url" works, then the ZfDocumentacionInmueble::actionIndex()-function is creating a $dataProvider and passing it to the view.
In your renderPartial call, you do not pass on a $dataProvider. If you have created it in your controller action, be sure to pass it along (or otherwise you might have to copy the code from the other controller):
$this->renderPartial('//ZfDocumentacionInmueble/index', array('model'=>$doc, 'dataProvider' => $dataProvider));

kohana, can't load orm model

I have problem saving data to database in Kohana using ORM. I am not an expert but i did more complex stuff than this. I guess the lack of basics lead to this.
I will add the model, form, and controller action code below. If anything else is needed you can ask. ps. This is my first time posting so I hope no rules are broken.
model:
class Model_Polloptionvote extends ORM {
protected $_table_name = "poll_option_votes";
protected $_primary_key = "id";
protected $_belongs_to = array(
'option' => array(
'model' => 'polloption'
)
);
protected $_table_columns = array(
'id' => array('type' => 'int'),
'option_id' => array('type' => 'int'),
'cookie' => array('type' => 'string')
);
}
form calling action:
<form action="<?php echo Route::url("home", array(
"controller" => "polls",
"action" => "vote"
)); ?>" method="POST">
<?php
if(isset($optionmodel)) {
foreach ($optionmodel as $option) { ?>
<label><?php
echo Form::radio('option_id', $option->id, FALSE);
echo $option->name; ?> </label> <?php
echo "</br>";
}
} ?>
</br>
<input type="submit" name="submit" id="submit" value="<?php echo __("Vote"); ?>" />
</form>
poll controler action:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Polls extends Controller_Base {
public function action_vote() {
$votemodel = ORM::factory("polloptionvote");
$id = $this->request->post("option_id");
if ($id != "") {
$optionmodel = ORM::factory("polloption", $id);
if ($votemodel->loaded()) {
$votemodel->option_id = $id;
$votemodel->cookie = "cookie";
$votemodel->save();
}
$this->request->redirect(Route::url("home", array(
"controller" => "index"
)));
}
The view loads fine but it seems that $votemodel->loaded() is always false. If change that condition to 1==1 I get error. Probably because action doesn't know what to save where.
UPDATE AFTER LOOKING error.log:
error log:
PHP Fatal error: Call to a member function route() on a non-object in /home/mywebsite/public_html/new/application/views/kohana/error.php on line 144
<?php if (Model_Korisnik::current() !== null) : ?>
<ul>
<li>Korisnik/ca: <span><?php echo Model_Korisnik::current()->username; ?></span></li>
<li class="profile"><a href="<?php echo Route::url("viewUser", array(
"ignore" => Model_Korisnik::current()->username,
"id" => Model_Korisnik::current()->id
)); ?>" title="<?php echo __("Profile"); ?>"><?php echo __("Profile"); ?></a></li>
<li class="settings"><?php echo __("Settings"); ?></li>
<li class="logout"><a href="<?php echo Route::url("auth", array(
"action" => "logout"
)); ?>?redirect=<?php echo rawurlencode(Request::current()->route()->uri(Request::current()->param()) . URL::query()); ?>" title="<?php echo __("Log out"); ?>"><?php echo __("Log out"); ?></a></li>
</ul>
<?php else : ?>
Last line is 146

yii modal dialog login

I've been looking all over the internet and I can't seem to find the solution to just a simple login via header. I tried all sorts of way. The problem I have right now just reloads every time i hit login. It's only when I go to ?r=site/login it'll let me login. Is there some kind of controller for headers!??! Not sure what is wrong anymore.
Right now i have this in my header:
<?php $this->beginWidget(
'bootstrap.widgets.TbModal',
array('id' => 'login')
); ?>
<?php
$model=new LoginForm;
$this->renderPartial('//site/login', array('model'=>$model));
?>
<?php $this->endWidget(); ?>
//stuff here
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'label' => 'Signin',
'type' => 'warning',
'htmlOptions' => array(
'data-toggle' => 'modal',
'data-target' => '#login',
),
)
);
?>
And then in site/login:
<div class="spacing">
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<div class="form" id="login-form">
<div class="login-container">
<div class="login-header">
<h1 title="login">Sign In</h1>
</div>
<div class="login-form">
<div class="input-position">
<?php echo $form->labelEx($model,'Email',array('class'=>'span3','style'=>'text-align:left')); ?>
<?php echo $form->textField($model,'email', array('class'=>'span3')); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="input-position">
<?php echo $form->labelEx($model,'password',array('class'=>'span3','style'=>'text-align:left')); ?>
<?php echo $form->passwordField($model,'password',array('class'=>'span3',)); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="input-position ">
<div class="rememberMe">
<?php echo $form->checkBox($model,'rememberMe'); ?>
<?php echo $form->label($model,'rememberMe'); ?>
<?php echo $form->error($model,'rememberMe'); ?>
</div>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Login'); ?>
</div>
<?php $this->endWidget(); ?>
</div>
</div>
</div>
</div>
</div>
controller
public function actionLogin()
{
$model=new LoginForm;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login())
$this->redirect(Yii::app()->user->returnUrl);
}
// display the login form
$this->render('login',array('model'=>$model));
}
As per my understanding, please keep your login script inside some loop like this to escape it from reloading every time.
if(Yii::app()->user->isGuest and (Yii::app()->controller->id=='site' and Yii::app()->controller->action->id = 'login')){
$this->beginWidget('bootstrap.widgets.TbModal', array('id' => 'login'));
---
$this->endWidget();
}else{
// keep some log out or other script here.
}
Here is the login what i have implemented on Modal login style:
Component/Header:
<?php
Yii::import('zii.widgets.CPortlet');
class Header extends CPortlet
{
protected function renderContent()
{
$this->render('Header');
}
}
Components/view/Header.php
<?php if(Yii::app()->user->isGuest){?>
<?php echo TbHtml::link('<i class="sign-in"></i>Sign in', '#', array(
'data-toggle' => 'modal',
'data-target' => '#loginModal',
));?>
<?php } else{ echo 'Hi!'.$user; } ?>
In View/Layout/Main.php
<?php
$model = new LoginForm;
$this->widget('bootstrap.widgets.TbModal', array(
'id' => 'loginModal',
'header' => '<h3 id="loginModal">Sign In</h3>',
'content'=> $this->renderPartial('userlogin',array('model'=>$model),true),
'remote' => $this->createUrl('site/userlogin'),
'onShow'=>'function(){$("#login-form")[0].reset(); }',
'show'=>false,)); ?>
View/Site/Userlogin.php
<?php $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'login-form',
//'helpType'=>'help-none',
'action'=>Yii::app()->createUrl('userlogin'),
//'enableAjaxValidation' => false,
'enableClientValidation'=>true,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange'=>false,
'afterValidate' => 'js:function(form, data, hasError) {
if (!hasError){
str = $("#login-form").serialize() + "&ajax=login-form";
$.ajax({
type: "POST",
url: "' . Yii::app()->createUrl('site/userlogin') . '",
data: str,
dataType: "json",
beforeSend : function() {
$("#login").attr("disabled",true);
},
success: function(data, status) {
if(data.authenticated)
{
window.location = data.redirectUrl;
}
else
{
$.each(data, function(key, value) {
var div = "#"+key+"_em_";
$(div).text(value);
$(div).show();
});
$("#login").attr("disabled",false);
}
},
});
return false;
}
}',
),
));?>
<?php echo $form->textFieldControlGroup($model,'username',array('span'=>5,'maxlength'=>255,'required'=>true)); ?>
<?php echo $form->passwordFieldControlGroup($model,'password',array('span'=>5,'maxlength'=>255,'required'=>true)); ?>
<?php echo TbHtml::submitbutton('Sign In',array('class'=>'btn btn-primary')); ?>
<?php $this->endWidget(); ?>
This will Authenticate via Ajax
SiteController:
$model=new LoginForm;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
$errors = CActiveForm::validate($model);
if ($errors != '[]')
{
echo $errors;
Yii::app()->end();
}
}
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if ($model->validate() && $model->login()) {
if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
$user_id = Yii::app()->user->id;
$arr = array();
$record = Users::model()->findByPk($user_id);
if ($record) {
echo CJSON::encode(array( 'authenticated' => true,
'redirectUrl' => CController::createUrl('users/trainerprofile'),
"id" => $record->id,));
Yii::app()->end();
}
}

Yii - How to display display captcha in CPortlet?

I'm using portlet to display contact form - the very same as is on static page which comes generated as standard. I'm using the same ContactForm model. The form displays but captcha is not displaying and reports "CCaptchaValidator.action "captcha" is invalid. Unable to find such an action in the current controller."
I'm new to yii so it takes awfully lot of time to figure everything out. Any quick advice what I shell do to make it working?
Best!
Yii::import('zii.widgets.CPortlet');
class ContactFormCard extends CPortlet
{
public $title='Contact';
protected function renderContent()
{
$model=new ContactForm;
if(isset($_POST['ContactForm']))
{
$model->attributes=$_POST['ContactForm'];
if($model->validate())
$this->controller->refresh();
}
$this->render('contactFormCard',array('model'=>$model));
}
public function actions()
{
return array(
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,
),
);
}
}
and in portlet view:
<?php if(CCaptcha::checkRequirements()): ?>
<div class="row">
<?php echo $form->labelEx($model,'verifyCode'); ?>
<div>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
</div>
<div class="hint">Please enter the letters as they are shown in the image above.
<br/>Letters are not case-sensitive.</div>
<?php echo $form->error($model,'verifyCode'); ?>
</div>
<?php endif; ?>
In you SiteController.php add:
public function actions() {
return array(
'captcha' => array(
'class' => 'CCaptchaAction',
'backColor' => 0xFFFFFF,
),
// [...]
);
}
In you ContactForm.php add:
public function rules() {
return array(
// [...]
array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(), 'message' => Yii::t('formsErros', 'Código de verificação incorreto.')),
// [...]
);
}
In you view.php
<!-- [...] -->
<?php if(CCaptcha::checkRequirements()): ?>
<div class="captcha">
<?=$form->labelEx($contactFormModel, 'verifyCode')?>
<?php $this->widget('CCaptcha', array('clickableImage' => true, 'showRefreshButton' => true, 'imageOptions' => array('id' => 'captchaContactForm', 'class' => 'clickableCursor'), 'buttonLabel' => '')); ?>
<?=$form->textField($contactFormModel, 'verifyCode', array('class' => 'verticalAlignBottom'))?>
<?=$form->error($contactFormModel, 'verifyCode')?>
</div>
<?php endif; ?>
<!-- [...] -->