I am loading a view using renderpartial, but the validations not working and not show the error messages. I tried to resolve the problem actived processOUtput in renderpartial but isn´t work.
this is my index view where I load the form view
<?php echo CHtml::link('Crear Usuario', array('create'), array('id'=>'newUsuario')); ?>
<div id="modal" class="modal hide fade"></div>
<script>
$(document).ready(function() {
$("#newUsuario").click(function() {
event.preventDefault();
$("#modal").load($("#newUsuario").attr("href"));
$("#modal").modal({show:true});
});
});
</script>
This is my action that load the view
public function actionCreate() {
$model = new Usuarios;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Usuarios']))
{
$model->validate();
$model->attributes=$_POST['Usuarios'];
$_POST['Usuarios']['check_tipo'] == 1 ? $model->tipo = 'Administrador' : $model->tipo = 'Normal';
$model->pass_php = md5($model->pass);
$model->session = $model->generateSalt();
$model->pass_hash= $model->hashPassword($_POST['Usuarios']['pass'], $model->session);
$this->transaction = $model->dbConnection->beginTransaction();
try {
if($model->save()) {
$this->transaction->commit();
$this->actionIndex();
}
} catch(Exception $e) {
$this->transaction->rollBack();
}
}
$this->renderPartial('create', array('model'=>$model));
}
And this is the view with the form
<h4 align="center">Nuevo Usuario</h4>
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'usuarios-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
));
?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email',array('size'=>60,'maxlength'=>255, 'placeholder'=>'Escriba su email')); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'check_tipo'); ?>
<?php echo $form->checkBox($model,'check_tipo'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'nombre'); ?>
<?php echo $form->textField($model,'nombre',array('size'=>60,'maxlength'=>255, 'placeholder'=>'Escriba su nombre')); ?>
<?php echo $form->error($model,'nombre'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'pass'); ?>
<?php echo $form->passwordField($model,'pass',array('size'=>60,'maxlength'=>255, 'placeholder'=>'Escriba su contraseña')); ?>
<?php echo $form->error($model,'pass'); ?>
</div>
</div>
<center>
<?php echo CHtml::button('Close', array('class'=>'btn', 'data-dismiss'=>'modal', 'aria-hidden'=>true)); ?>
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array('class'=>'btn btn-primary')); ?>
</center>
<?php $this->endWidget(); ?>
When I used render the validations and error messages work, the problem is just with renderpartial.
Help me please and sorry if my english is bad.
Related
I created a ajax submit button to submit a form.
The form is working Good and all the functionalty which I need working perfectly.
I enabled Ajax validation and a success message also.
The problem occurs when I partial redirect the form.
The ajax function and validation is not working.
My Controller
public function actionCreate()
{
$model=new Comments;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Comments']))
{
$model->attributes=$_POST['Comments'];
// echo '<pre>';print_r($model->attributes);die();
$valid = $model->validate();
if($valid){
if($model->save()){
echo CJSON::encode(array('status'=>'success'
));
}
}else{
$error =CActiveForm::validate($model);
if($error != '[]')
echo $error;
Yii::app()->end();
}
}
}
View /_Form
<?php
$form = $this->beginWidget('CActiveForm',array('id'=>'comments-form',
'enableAjaxValidation'=>true,
'action'=>$this->createUrl('Comments/create'),
'enableClientValidation'=>true
));
?>
<div class="errorMessage" id="formResult"></div>
<div id="AjaxLoader" style="display:none" >
<img src="<?php echo Yii::app()->request->baseUrl;?>/theme/images/spinner.gif">
</div>
<div class="row-user-single-user-single" >
<?php echo $form->labelEx($model,'type'); ?>
<?php
echo $form->dropDownList($model,'type',
array(""=>"Select Type","0"=>"Offer","1"=>"Events"),
array('style' => 'width:220px;','class'=>'form-control','disabled'=>false,)); ?>
<?php echo $form->error($model,'type'); ?>
</div>
<div class="row-user-single-user-single" >
<?php echo $form->labelEx($model,'offereventid'); ?>
<?php echo $form->textField($model,'offereventid',array('style' => 'width:500px;','class'=>'form-control','disabled'=>false,)); ?>
<?php echo $form->error($model,'offereventid'); ?>
</div>
<div class="row-user-single-user-single">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name',array('style' => 'width:500px;','class'=>'form-control','disabled'=>false,)); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row-user-single">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email',array('style' => 'width:500px;','class'=>'form-control','disabled'=>false,)); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row-user-single">
<?php echo $form->labelEx($model,'comment'); ?>
<?php echo $form->textArea($model,'comment',array('style' => 'width:500px;','class'=>'form-control','disabled'=>false,)); ?>
<?php echo $form->error($model,'comment'); ?>
</div>
<div class="row-user-single">
<?php //echo $form->labelEx($model,'createdby'); ?>
<?php //echo $form->textField($model,'createdby'); ?>
<?php //echo $form->error($model,'createdby'); ?>
</div>
<div class="row-user-single">
<?php //echo $form->labelEx($model,'createdon'); ?>
<?php //echo $form->textField($model,'createdon'); ?>
<?php //echo $form->error($model,'createdon'); ?>
</div>
<div class="row-user-single">
<?php //echo $form->labelEx($model,'status'); ?>
<?php //echo $form->textField($model,'status',array('size'=>1,'maxlength'=>1)); ?>
<?php //echo $form->error($model,'status'); ?>
</div>
<div class="buttons">
<?php
echo CHtml::ajaxSubmitButton('Submit',CHtml::normalizeUrl(array('Comments/create','render'=>true)),
array(
'dataType'=>'json',
'type'=>'post',
'success'=>'function(data){
$("#AjaxLoader").hide();
if(data.status == "success"){
$("#formResult").html("Comment Submitted");
$("#comments-form")[0].reset();
}else{
$each(data,function(key.val){
$("#comments-form #"+key+"_em_").text(val);
$("#comments-form #"+key+"_em_").show();
});
}
}',
'beforeSend'=>'function(){
$("#AjaxLoader").show();
}'
),array('id'=>'submit','class'=>'btn btn-success'));
?>
<?php $this->endWidget();?>
</div>
Please help me...
Make sure that all required scripts (jQuery etc.) are registered. Debug it in JS console. If you render this form by renderPartial method maybe you should set $processOutput=true param in this method.
I have the form
TblRegistration/_form.php
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'tbl-registration-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'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary(array($model,$model1)); ?>
<div class="row">
<?php echo $form->labelEx($model,'director'); ?>
<?php echo $form->textField($model,'director',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'director'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'experience'); ?>
<?php echo $form->textField($model,'experience'); ?>
<?php echo $form->error($model,'experience'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'language'); ?>
<?php echo $form->textField($model,'language',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'language'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model1,'email'); ?>
<?php echo $form->textField($model1,'email'); ?>
<?php echo $form->error($model1,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model1,'password'); ?>
<?php echo $form->textField($model1,'password'); ?>
<?php echo $form->error($model1,'password'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
My actioncreate create contain
public function actionCreate()
{
$model=new TblRegistration;
$model1=new TblLogin();
// Uncomment the following line if AJAX validation is needed
//$this->performAjaxValidation($model1);
if(isset($_POST['TblRegistration']) && isset($_POST['TblLogin']))
{
$model->attributes=$_POST['TblRegistration'];
$model1->attributes=$_POST['TblLogin'];
$model->save();
if($model->save())
{
$model1->reg_id=$model->reg_id;
$model1->save();
}
if(($model->save() )&& ($model1->save()))
$this->redirect(array('view','id'=>$model->reg_id));
}
$this->render('create',array(
'model'=>$model,
));
}
my fields email and password is from the model TblLogin.I want to validate those fields before iam saving the data.how can i achive this?
After:
$model->attributes=$_POST['TblRegistration'];
$model1->attributes=$_POST['TblLogin'];
Add:
if($model->validate() && $model1->validate()){
$model->save();
...
I created app with yii and I need to load a view form in Fancy-box.
I did that but my problem is when I click on the submit button the form redirects me to the controller action without validating the form.
How to validate the form without the redirect inside Fancy-box?
My view:
<?php
$config = array(
);
$this->widget('application.extensions.fancybox.EFancyBox', array(
'target'=>'#getaction',
'config'=>$config,));
echo CHtml::link('Add Section',array('section/create'),array('id'=>'getaction'));
?>
FormView _from from call from another view
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'section-form',
'enableAjaxValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Controller :
public function actionCreate()
{
$model=new Section;
if(isset($_POST['Section']))
{
$model->attributes=$_POST['Section'];
if($model->validate())
//// Do Som code here
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
the problem was fixed by my bro seenivasan on Yii Forum , so I will share the code here
_form.php
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'section-form',
'enableAjaxValidation'=>true,
//'enableClientValidation'=>true,
'clientOptions'=>array('validateOnSubmit'=>true), //This is very important
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>64)); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
In controller
public function actionCreate()
{
$model=new Section;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);//You have enabled ajax validation. You have to uncomment this line.
if(isset($_POST['Section']))
{
$model->attributes=$_POST['Section'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
if(Yii::app()->request->getIsAjaxRequest())
echo $this->renderPartial('_form',array('model'=>$model),true,true);//This will bring out the view along with its script.
else $this->render('create',array(
'model'=>$model,
));
}
Reference link :
Yii Forum
Just add 'enableClientValidation'=>true, in the array inside the beginwidget.
I have the following form which is being used to create new records
<?php
/* #var $this ComponentsController */
/* #var $model Components */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'components-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'component_id'); ?>
<?php echo $form->textField($model,'component_id'); ?>
<?php echo $form->error($model,'component_id'); ?>
</div>
<div class="row">
<label class="required" for="FixedAsset_original_asset_number">
Asset Number
</label>
<input id="Components_original_asset_number" type="text" name="Components[original_asset_number]">
<?php //echo Chtml::textField('fixed_asset_id', FixedAsset::model()->FindByPk($model2->fixed_asset_id)->fixed_asset_id); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textField($model,'description',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'manufacturer'); ?>
<?php //echo $form->textField($model,'manufacturer'); ?>
<?php $manufacturer = Manufacturers::model()->findAll(array("order"=>"name"));
$list = CHtml::listData($manufacturer, 'manufacturer_id', 'name');
echo $form->dropDownList($model,'manufacturer', $list,array());
?>
<?php echo $form->error($model,'manufacturer'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'medium'); ?>
<?php //echo $form->textField($model,'medium'); ?>
<?php $medium = Medium::model()->findAll();
$list = CHtml::listData($medium, 'medium_id', 'type');
echo $form->dropDownList($model,'medium', $list,array());
?>
<?php echo $form->error($model,'medium'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'version'); ?>
<?php echo $form->textField($model,'version',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'version'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'serial_no'); ?>
<?php echo $form->textField($model,'serial_no',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'serial_no'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'purchase_date'); ?>
<?php //echo $form->textField($model,'purchase_date'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'id'=>'Components_purchase_date',
'name'=>'Components[purchase_date]',
//'value'=>CTimestamp::formatDate('d/m/Y',$item->validFrom),
// additional javascript options for the date picker plugin
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
));
?>
<?php echo $form->error($model,'purchase_date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'disposal_date'); ?>
<?php //echo $form->textField($model,'disposal_date'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'id'=>'Components_disposal_date',
'name'=>'Components[disposal_date]',
//'value'=>CTimestamp::formatDate('d/m/Y',$item->validFrom),
// additional javascript options for the date picker plugin
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
));
?>
<?php echo $form->error($model,'disposal_date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'model'); ?>
<?php echo $form->textField($model,'model',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'model'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'nol'); ?>
<?php echo $form->textField($model,'nol'); ?>
<?php echo $form->error($model,'nol'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
I want to send an error to the field Components_original_asset_number on the form. How would I achieve this. In the controller function I am checking to see if it exists if not I want to display error on form or message. original asset number is part of a separate model which I am displaying on this form.
public function actionCreate()
{
$model = new Components;
$model_fixedAsset = new FixedAsset;
$model_comAsset = new ComAsset;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Components']))
{
$model->attributes=$_POST['Components'];
$fixedAssetId = null;
// Check if asset exist and get PK
if( $_POST['Components']['original_asset_number'] != "" ){
//print_r($_POST['Components']);
$criteria = new CDbCriteria;
$criteria->condition = "(original_asset_number = :original_asset_number)";
$criteria->params = array(":original_asset_number" => $_POST['Components']['original_asset_number'] );
$fixedAssetRow = FixedAsset::model()->find($criteria);
//print_r($fixedAssetRow);
if($fixedAssetRow){
$fixedAssetId = $fixedAssetRow->fixed_asset_id;
}
//echo $fixedAssetId;
}
if($fixedAssetId){
/*if($model->save())
$this->redirect(array('view','id'=>$model->component_id));*/
// Create com_asset record
}else{
//no asset found return error message to input corect asset number or create asset in navision and run php script to update mysql db
}
}
$this->render('create',array(
'model'=>$model,
'model_fixedAsset'=>$model_fixedAsset,
'model_comAsset'=>$model_comAsset,
));
}
You should add the 'original_asset_number' in your model as variable and set required validation rule. Then you can use the following line to set error
$model->addError('original_asset_number', " no asset found return error message to input corect asset number or create asset in navision and run php script to update mysql ");
Also, you use the following way to display the field and error.
<?php echo $form->errorSummary($model); ?>
or
<div class="row">
<?php echo $form->labelEx($model,'original_asset_number'); ?>
<?php echo $form->textField($model,'original_asset_number',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'original_asset_number'); ?>
</div>
i am trying ajax based form submission in yii framework here is my view code
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'users-form',
'enableAjaxValidation'=>true,
'clientOptions'=>array('validateOnSubmit'=>true, 'validateOnType'=>false),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'user_username'); ?>
<?php echo $form->textField($model,'user_username',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'user_username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'user_password'); ?>
<?php echo $form->textField($model,'user_password',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'user_password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'user_role'); ?>
<?php echo $form->dropDownList($model,'user_role', CHtml::listData($dropDownData, 'role_id', 'role_title')); ?>
<?php echo $form->error($model,'user_role'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'user_company_id'); ?>
<?php echo $form->textField($model,'user_company_id'); ?>
<?php echo $form->error($model,'user_company_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::ajaxSubmitButton('Create User', CHtml::normalizeUrl(array(''),
array(
'beforeSend'=>'function() {
$(".createUser").attr("disabled","disabled");
}',
'success'=>'function(data) {
alert(data);
if( data == "sent" ) {
$(".ajaxMessage").text("User has been successfully created");
} else {
$(".ajaxMessage").text("Oops! It looks there is some error in your form");
}
$(".createUser").removeAttr("disabled");
}'
),
array('class'=>'createUser')
)); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<div class="ajaxMessage"></div>
I want to grab the ajax response in variable. Here is the controller code
if(isset($_POST['Users']))
{
if( Yii::app()->request->isAjaxRequest )
echo 'ajax get';
else
echo 'ajax not get';
}
In firebug i have seen that response is generated correctly but it is not stored in my variable. The view code is created with the help of this link:
http://www.yiiframework.com/forum/index.php/topic/8219-solved-ajax-form-submission/
The javascript data will be whatever you echo from controller. So if you want to check for data=="sent" in jquery's success callback, do echo 'sent'; from the action.
You have included the ajaxOptions inside the normalizeUrl function, change it to:
<div class="row buttons">
<?php echo CHtml::ajaxSubmitButton('Create User', CHtml::normalizeUrl(array('')),
array(
'beforeSend'=>'function() {
$(".createUser").attr("disabled","disabled");
}',
'success'=>'function(data) {
alert(data);
if( data == "sent" ) {
$(".ajaxMessage").text("User has been successfully created");
} else {
$(".ajaxMessage").text("Oops! It looks there is some error in your form");
}
$(".createUser").removeAttr("disabled");
}'
),
array('class'=>'createUser')
); ?>