msoa
April 21, 2013, 9:11am
1
Hello,
I have a CActiveForm
with this summary:
.
.
.
'id'='email-form',
'enableAjaxValidation`=>true,
'clientOptions' => array('validateOnSubmit'=>true),
.
.
.
Now i intend collect form Errors in Server-Side and sent it via json object to the client. in Client-Side there
is and Jquery function that parse the json object(form Errors) and set data to errorSummary and at last
shows the errorSummary of form.
i have done it without any problem, my question is what following functions don’t collect form Errors:
protected function getErrorSummary($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='email-form'){
$errors=CActiveForm::validate($model);
if($errors !== '[]')
Yii::app()->end($errors);
}
}
But following collect form Errors:
protected function getErrorSummary($model)
{
$errors=CActiveForm::validate($model);
if($errors !== '[]')
Yii::app()->end($errors);
}
notice that both functions truly acts on validateOnChange .
alirz23
(Alirz23)
April 21, 2013, 12:31pm
2
Hi there
Why would you wanna invent your on ajax logic since yii does give you a method out of the box use that
here is Yii version of your code
protected function getErrorSummary($model)
{
if(Yii::app()->request->isAjaxRequest) {
$errors=CActiveForm::validate($model);
if($errors !== '[]')
Yii::app()->end($errors);
}
}
msoa
April 21, 2013, 1:28pm
3
alirz23:
Hi there
Why would you wanna invent your on ajax logic since yii does give you a method out of the box use that
here is Yii version of your code
protected function getErrorSummary($model)
{
if(Yii::app()->request->isAjaxRequest) {
$errors=CActiveForm::validate($model);
if($errors !== '[]')
Yii::app()->end($errors);
}
}
Excuse me, but my problem isn’t understanding what’s the request type! however your code didn’t work.
alirz23
(Alirz23)
April 21, 2013, 1:33pm
4
excuse you, then should describe you problem properly, I have replied according to what you have posted above
msoa
April 21, 2013, 2:24pm
5
I want know why the following method don’t collect model errors:
protected function getErrorSummary($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='email-form'){
$errors=CActiveForm::validate($model);
if($errors !== '[]')
Yii::app()->end($errors);
}
}
unless in form(CActiveForm) submission action dose not send ajaxVar(ajax) with form ID value, so why is not accessible here?
alirz23
(Alirz23)
April 21, 2013, 2:32pm
6
do a dump of your $_POST and see, if you are getting that $_POST[‘ajax’] with value that you checking in your condition
msoa
April 21, 2013, 4:24pm
7
In document is:(clientOptions)
ajaxVar: string, the name of the parameter indicating the request is an AJAX request. When the AJAX validation is triggered, a parameter named as this property will be sent together with the other form data to the server. The parameter value is the form ID. The server side can then detect who triggers the AJAX validation and react accordingly. Defaults to ‘ajax’.
but i don’t find ajaxVar in var_dump($_SERVER) !
alirz23
(Alirz23)
April 21, 2013, 4:52pm
8
then there is something wrong with your form
msoa
April 21, 2013, 6:55pm
9
Following var_dump($_POST) is for when a field(newEmail ) is left empty:
array
'User' =>
array
'email' => string 'user@gmail.com' (length=14)
'newEmail' => string '' (length=0)
Following var_dump($_POST) is for when all the fields are filled:
array
'User' =>
array
'email' => string 'user@gmail.com' (length=14)
'newEmail' => string 'admin@minudasht.net' (length=19)
'ajax' => string 'email-form' (length=10)
'yt0' => string 'update' (length=18)
This means ajaxVar is initialized only when form is in submission action ?
alirz23
(Alirz23)
April 22, 2013, 9:26am
10
try the double equals instead of triple
protected function getErrorSummary($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']=='email-form'){
$errors=CActiveForm::validate($model);
if($errors !== '[]')
Yii::app()->end($errors);
}
}
msoa
April 22, 2013, 10:47am
11
Here i understand what when ajaxVar initialized. When [color="#0000FF "]‘validateOnSubmit’=>true[/color] is set ajaxVar initialized onsubmit, and when[color="#0000FF "] validateOnChange=>true[/color] is set ajaxVar initialized on changing fileds.
and know understand what’s main problem: newEamil field as default is empty and when submitting the form without change on this field ajaxVar don’t initialized even if validateOnChange=>true is set. This problem can be solved?
The role is:
array('email,newEmail','required', 'on'=>'CEmail', 'message'=>$required_msg),