Zend fw, custom validation errors - validation

I am creating a website with Zend_Form. In my controller, I assign a form object to the view. In the view, I use the following code to render the form:
<?php if ( isset( $this->success ) ): ?>
<div class="message success"><p>Thanks!</p></div>
<?php elseif ( sizeof( $this->form->getMessages( ) ) > 0 ): ?>
<div class="message success"><p>Something went wrong..</p></div>
<?php endif; ?>
<form>
<label>Name:</label>
<?php echo $this->form->name; ?>
<label>E-mail:</label>
<?php echo $this->form->name; ?>
</form>
Until now, this way of checking if there were form errors was good enough. But my client asked me if I could also specify the field that was not correct. So, for example: "Warning: you forgot to fill in your name". I've really got no idea on how to do this with Zend. Does anybody know where to start?
Thanks,
Martijn

You should be able to see it by requesting getErrors(); on the Zend_Form object.
It will return a array of errors where the key is the field name
returns something like this.
array(
'fieldname' => array('error1', 'error2'),
'username' => array('errors')
);
You could also call $form->getElements() and iterate over each element to see which one is throwing errors

Related

How to implement model validation on autocomplete field in yii2?

I am using a jui autocomplete widget in my form. Model validation is not working properly in here.
This is my view
<?php $form = ActiveForm::begin();?>
<div class="members-form">
<div class="col-md-5">
<?php
$data = FamilyName::find()
->select(['name as value', 'name as label','id as id'])
->asArray()
->all();
echo 'Family Name' .'<br>';
echo AutoComplete::widget([
'name' => 'family',
'id' => 'family_name',
'clientOptions' => [
'source' => $data,
// 'minLength'=>'3',
'autoFill'=>true,
'select' => new JsExpression("function( event, ui ) {
$('#members-family_name_id').val(ui.item.id);//#City-state_name is the id of hiddenInput.
}")],
]);
?>
<?= Html::activeHiddenInput($model, 'family_name_id')?>
<?= $form->field($model, 'remarks')->textInput() ?>
<?php ActiveForm::end(); ?>
</div>
</div>
my model code
public function rules()
{
return [
[['family_name_id', 'first_name',
'date_of_birth', 'relation_id', 'is_head','marital_status','remarks',
'gender','address_id'], 'required'],
];
}
Now if i try to create a new member with no data selected in any of the fields, then required fields will show like " ... cannot be blank" in red. But the family_name_id is not showing such validation. The data is not getting saved if leave the auto complete field empty but no validation message is being displayed. How can i show validation messages with jui auto complete ?
I believe you faced the same problem as I had few weeks ago. I experienced similar behavior: I saw autocomplete suggestions and could interact with them, but when I left the input field the client-site validation kept silent.
If you look at the html produced by your view, at the bottom you can see some javascript code, which is responsible for the client-side validation. It looks like:
jQuery(document).ready(function () {
....
jQuery('#app-new-form').yiiActiveForm(
[
{
"id":"application-familyname",
"name":" familyname ",
"container":".field-application-familyname",
"input":"#application-familyname",
"enableAjaxValidation":true,
"validate":function (attribute, value, messages, deferred, $form) {
yii.validation.required(value, messages, {"message":"Field cannot be empty"});
}
}
], []);
.....
});
This code assigns an event handler to your autocomplete field to start the client-side validation. But you have to pay attention, which id is used for the field. Unfortunately, yii2 takes the id provided by you only to construct the <input> element in html. When it produces javascript code the id is always generated from few permanent parts. The most important two are the name of the model and the name of the attribute. In your case id should be something like 'your_modelname -family_name_id'.
Because your explanation is not complete, this is only a guess. So, look at your html source and be sure you have the same id for the input you try to validate and the id in the JavaScript (see above).
You are using Html Hidden Input .. You have to use ActiveForm HiddenInput to show error validation..
use
<?= $form->field($model, 'family_name_id')->hiddenInput()->label(false);>
Why don't you use the family_name_id instead of creating a new select?
<?= $form->field($model, "family_name_id")->dropDownList(
ArrayHelper::map(FamilyName::find()->all(), 'id', 'name'),
['prompt' => 'Select']
) ?>
And you can change the label by using ->label('Family Name'), but i would recommend changing in model (unless you need "Family Name Id" as a label elsewhere).
I know you didn't said anything about that, but can i recommend you to see this answer about using methods that perform sql queries inside your view?

Multiple validation on one attribute in Yii

I want to validate an attribute on multiple CValidator classes.
To be more specific, I want an email address to be validated by the email validator, but I also want it to be required.
I can of course define two separate rules, like so:
array('email', 'email'),
array('email', 'required'),
But when I leave the input blank the validation only returns an error saying the field is required, but it doesn't return an error saying it has to be an email address. When I fill in a non-email string, it then returns the email validation error.
I tried to combine the validators in an array, and a comma separated string but that doesn't work. So I guess the only option is to use a custom validation method.
But how can I use the built-in CValidator validators in this method? And how can I build it, that the two rules are validated together at once instead of one at a time?
If I am getting you right you have an issue with error message. I think you can use following approach to show message.
array('email', 'email', 'message' => 'Please provide valid email.'),
array('email', 'required', 'message' => 'Email is required. Please provide valid email.'),
Hope this will help you....
User interface wise this makes little sense to specify that an email is invalid when left empty. You tell them that the email is required when empty or invalid when it isn't empty but not an email. Doing both seems very confusing to me.
I personally just use:
array('email', 'email', 'allowEmpty' => FALSE),
Use the errorsummary() method:
EDIT:
Changed reference to CHTML
http://www.yiiframework.com/doc/api/1.1/CHtml#errorSummary-detail
In your view, add
<?php echo CHtml::errorSummary($model, NULL, NULL, array ('firstError' => false));
...
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email'); ?>
<?php echo $form->error($model,'email'); ?>
</div>
From the documentation
additional HTML attributes to be rendered in the container div tag. A
special option named 'firstError' is recognized, which when set true,
will make the error summary to show only the first error message of
each attribute. If this is not set or is false, all error messages
will be displayed. This option has been available since version 1.1.3.
Note that if you are using Ajax validation then you will get the first error only, next to the fields
Actually model validation returns all errors of validation, but CActiveForm::error method shows only first one for selected attribute. I guess you use now something like this:
<?php echo $form->error($model,'attr'); ?>
but instead should use
<?php if ($model->hasErrors('attr')) : ?>
<?php $errorList = $model->getErrors('attr'); ?>
<?php foreach ($errorList as $error) : ?>
// display error
<?php endforeach ?>
<?php endif ?>
Also you can write your own helper method for displaying all errors for single attribute.
You can use skipOnError property of the validator.
array('email', 'email', 'skipOnError' => false),
array('email', 'required', 'skipOnError' => false),

Ajax is not working in Cakephp 2.2.3

I want to implement Ajax on ContactUs form, here is my code.
Controller:
class ContactsController extends AppController {
public $layout = 'default';
public $helpers = array('Html', 'Form', 'Paginator','Js');
public $components = array('RequestHandler');
public function index(){
if(!empty($this->data)){
if($this->Contact->save($this->data)){
if($this->RequestHandler->isAjax()){
$this->render('success','ajax');
} else {
$this->Session->setFlash('Message sent');
$this->redirect(array('action'=>'index'));
}
}
}
}
}
View file
<?php echo $this->Html->script('jquery', FALSE); ?>
<div id="success"></div>
<h2>Contact Us</h2>
<?php
echo $this->Form->create();
echo $this->Form->input('name',array('id'=>'name'));
echo $this->Form->input('email',array('id'=>'email'));
echo $this->Form->input('message',array('id'=>'message'));
echo $this->Js->submit('Send',array(
'before'=>$this->Js->get('#sending')->effect('fadeIn'),
'success'=>$this->Js->get('#sending')->effect('fadeout'),
'update'=>'#success'
));
echo $this->Form->end();
?>
<div id="sending" style=" display: none;background-color: #90ee90;">Sending...</div>
in view/layouts/ajax.ctp file contain:
<?php //echo $this->fetch('content'); ?>
<?php echo $content_for_layout; ?>
I have tried with both of these. And default.ctp layout contains in head section:
echo $scripts_for_layout;
echo $this->Js->writeBuffer(array('cache'=>TRUE));
in source file jquery is included but PHPStorm shows error in jquery file, and firebug shows
hope everything will be clear, any help greatly appreciated.
ou should disable the buffer on the before and success callback.
echo $this->Js->submit('Send',array(
'before'=>$this->Js->get('#sending')->effect('fadeIn', array('buffer' => false)),
'success'=>$this->Js->get('#sending')->effect('fadeOut', array('buffer' => false)),
'update'=>'#success'
));
EDIT:
The typo could cause this issue also: 'fadeout' instead of 'fadeOut'.
I am having the exact same problem. I see you were using the Cakephp tutorial by Andrew Perkins (I am using the exact same one so my code is identical) http://www.youtube.com/watch?v=dQ71psonQx0
I believe the problem is that he made this video tutorial based on Cakephp 1.3. The syntax has changed a lot in 2.0. I haven't figured out how to get it working in 2.2.3 yet either but I think that this is the reason.

Action not running with CakePHP Js->submit()

I'm using CakePHP 1.3, and trying to make a simple message posting board with ajax. I'm trying to use the Js helper to submit a form on the index page, then refresh the message board's div to include the new message. This is all on a single page.
I have previously posted on this, but I wanted to rephrase the question and include some updates. The previous question can be seen here How to use Js->submit() in CakePHP?
When I came back to this project after a couple days, I immediately tested and the form worked (sort of). Submitting the form added a message to the database (it didn't display the message, but I haven't attacked that part yet). It worked 2 times, adding 2 messages. Then I opened the controller file and commented out some debug code, and it stopped working. It appears the action is not being called.
Here is my messages_controller.php:
<?php
class MessagesController extends AppController {
function index() {
$messages = $this->Message->find('all');
$this->set('messages',$messages);
}
function add() {
$this->autoRender = false;
$this->Session->setFlash('Add action called');
if($this->RequestHandler->isAjax()) {
$this->Session->setFlash('Ajax request made');
$this->layout = 'ajax';
if(!empty($this->data)) {
if($this->Message->save($this->data)) {
$this->Session->setFlash('Your Message has been posted');
}
}
}
}
}
?>
Here is the index.ctp for my Message class
<div id="guestbook" class="section_box">
<h3 id="toggle_guestbook"><div class="toggle_arrow"></div>Sign our Guestbook</h3>
<?php
echo $this->Form->create('Message');
echo $this->Form->input('name', array('label' => 'From:'));
echo $this->Form->input('text', array('label' => 'Message:'));
echo $this->Js->submit('Post Your Message', array(
'url' => array(
'controller' => 'messages',
'action' => 'add'
),
'update' => '#message_board'
));
echo $this->Form->end();
echo $this->Js->writeBuffer(array('inline' => 'true'));
?>
<div id="message_board">
<?php foreach($messages as $message) { ?>
<div class="message">
<p class="message_txt">
<?php echo $message['Message']['text']; ?>
</p>
<div>
<div class="message_name">
<?php echo $message['Message']['name']; ?>
</div>
<div class="message_date">
<small>
<?php echo $message['Message']['date']; ?>
</small>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
When the submit button is clicked, I can see in the console that a POST is made to http://localhost/messages/add with the correct data. But there doesn't appear to be a response. The flash message "Add action called" is NOT set from the controller (or any of the flash messages, for that matter) and the contents of #message_board are emptied.
If I refresh the page at this point, the SECOND flash message appears ("Ajax request made"), and the contents of the #message_board are restored. However the new message was not saved, its the same 2 messages from before.
I'm stumped. I have a feeling maybe there are bigger issues causing my problem, but I can't see it. Any help would be appreciated.
But there doesn't appear to be a
response ... and the
contents of #message_board are
emptied.
That is because you haven't set what action/view to render. You have to do this manually since you have $this->autoRender set to false. You could use render() to do this. More info can be found at its respective cookbook page.
If you have $this->autoRender set to true, then it'll replace the contents of #message_board with the contents of add.ctp
The flash message "Add action called"
is NOT set from the controller (or any
of the flash messages, for that matter)
I think you have to refresh the page or the part which contains the $this->Session->flash() bit for flash messages to appear.
The fact that the flash message appeared when you refreshed the page means that it did call and run the action.
AFAIK, you can only put/print one message from the flash key in the Messages array. The flash key is where the flash messages are stored by default. Each call to setFlash() will overwrite the flash message set by older calls.
Since only the second flash message was displayed, we could say that it failed at passing at least one of the conditions following the second call to setFlash() in the controller. You might want to put debug($this->data) statements near the conditions related to $this->data to help yourself in debugging your problem.
You could also use debug() to know if your application went through a certain action or path since it will almost always be displayed.
So you could do the following to check if it passed this condition:
if(!empty($this->data)) {
debug('Passed!');
If 'Passed!' will be printed after submitting the form, we would know that it passed that condition.
However the new message was not saved
It might be because $data is empty or it failed at validation. If your $data is not empty, it might have failed at validation and since your form doesn't display the validation errors; you might never have noticed them. One way to know if it passed validation is to do the following:
$this->Message->set($this->data);
if ($this->Message->validates()) {
debug('it validated logic');
} else {
debug('didn't validate logic');
}
Ramon's solutions worked for me. Here's the updated code.
Controller add function
function add() {
$this->autoRender = false;
if($this->RequestHandler->isAjax()) {
$this->layout = 'ajax';
if(!empty($this->data)) {
if ($this->Message->validates()) {
if($this->Message->save($this->data)) {
$this->render('/elements/message_board');
} else {
debug('didn\'t validate logic');
}
}
}
}
}
Heres the add form view:
<?php
echo $this->Form->create('Message');
echo $this->Form->input('name', array('label' => 'From:'));
echo $this->Form->input('text', array('label' => 'Message:'));
echo $this->Js->submit('Post Your Message', array(
'url' => array(
'controller' => 'messages',
'action' => 'add'
),
'update' => '#message_board'
));
echo $this->Form->end();
echo $this->Js->writeBuffer(array('inline' => 'true'));
?>
<?php pr($this->validationErrors); ?>
<div id="message_board">
<?php echo $this->element('message_board'); ?>
</div>
I tried to use the same solution as you used but it's not working. Ajax is ok when I access it directly in the URL, and I have the impression that the click is doing nothing. When I use
<fieldset><legend><?php __(' Run 1');?></legend>
<div id="formUpdateID"><div id="#error-message"></div>
<?php
$orders=array_merge($emptyarray,$orders['r1']['Order']);
echo $this->Form->create('Order');
echo $this->Form->input('id', array('value'=>$orders['id'],'type' =>'hidden'));
echo $this->Form->input('race_id', array('value'=> $orders['race_id'],'type' =>'hidden'));
echo $this->Form->input('driver_id', array('value'=>$session->read('Auth.User.driver_id'),'type' =>'hidden'));
echo $this->Form->input('run', array('value'=>$run,'type' =>'hidden'));
echo $this->Form->input('pm', array('value'=>$orders['pm'],'error'=>$err[$run]));
echo $this->Form->input('pr', array('value'=>$orders['pr'],'error'=>$err[$run]));
echo $this->Form->input('fuel', array('value'=>$orders['fuel'],'error'=>$err[$run]));
echo $this->Form->input('pit', array('value'=>$orders['pit'],'label' => __('Pit on lap '),'error'=>$err[$run]));
echo $this->Form->input('tyre_type', array('value'=>$orders['tyre_type'],'error'=>$err[$run]));
echo $this->Js->submit('Modify', array(
'url' => array(
'controller' => 'orders',
'action' => 'ajax_edit'
),
'update' => '#error_message'
));
echo $this->Form->end();
?>
<?php pr($this->validationErrors); ?>
</div></fieldset>
in view and in controller "orders":
function ajax_edit($id=null){
$this->autoRender = false;
if($this->RequestHandler->isAjax()) {
die(debug('In Ajax'));
$this->layout = 'ajax';
debug('didn\'t validate logic');
}
echo 'hi';
}
None of the messages are displayed.
I have some hard coded JS/ajax before which is not targeting this code part.
I did copy ajax layout in th webroot/view folder.
I can see the AJAX code displayed in formatted source code
<div class="submit"><input id="submit-1697561504" type="submit" value="Modify" /></div> </form><script type="text/javascript">
//<![CDATA[
$(document).ready(function () {$("#submit-1697561504").bind("click", function (event) {$.ajax({data:$("#submit-1697561504").closest("form").serialize(), dataType:"html", success:function (data, textStatus) {$("#error_message").html(data);}, type:"post", url:"\/Webmastering\/form1C\/frame\/orders\/ajax_edit\/1"});
return false;});});
//]]>
</script>
BTW, I start getting bored of the lack of doc in cakephp and its non efficacity to realize task more complicated than just posting a post in a blog. So thank you for your help before I start destroying my computer ;)
I know it's an old topic, but I stumbled acros the same problem in my application, so now I think what Thrax was doing wrong, namely he didn't put echo $this->Js->writeBuffer(array('inline' => 'true')); in the view (or in the layout) file, like Logic Artist did, so the scripts for handling the submit button's click weren't generated.

Getting form data in Yii to a CActiveRecord model works for one model but not for another

I'm getting a submitted form in this way:
$resume->attributes = $_POST['ResumeModel'];
$profile->attributes = $_POST['UserProfile'];
Both CActiveRecord models are correctly populated before this from the corresponding tables, they have the correct data and all.
Both models' data is present on $_POST as modified by the form.
But it seems that the assignment to the attributes property works only for $profile and not for $resume.
If I check their values after that assignment, $profile doesn't get the edits from the form.
Is there something in the definition of the model that can cause that? As far as I can see, both models are similarly implemented
I don't understand why this happens, does anyone have a clue?
Thanks!
The problem is that some fields on the $resume model didn't have any validation rules and weren't declared as safe either, so they couldn't be safely mass assigned.
Reference:
http://www.yiiframework.com/doc/guide/form.model#securing-attribute-assignments
Have you checked the $_POST variables closely? For the mass "attributes" assignment to work the array should be of the form:
$_POST = (
'ResumeModel' => (
'data1' => 'something',
'data2' => 'something else',
),
'UserProfile' => (
'data3' => 'yo ho ho',
'data4' => 'bottle of rum',
)
)
If it looks like this it's wrong:
$_POST = (
'ResumeModel' => (
'data1' => 'something',
'data2' => 'something else',
'data3' => 'yo ho ho',
'data4' => 'bottle of rum',
)
)
To ensure that the form is building the correct $_POST array for each model, make sure that you are passing both the $resume and $profile model into your form View like this:
<?php
$resume=new ResumeModel;
$profile=new UserProfile;
$this->render('yourFormView', array('resume'=>$resume,'profile'=>$profile));
?>
Then, in "yourFormView", make sure that you are creating the form fields appropriately with each model, like so:
<?php $form=$this->beginWidget('CActiveForm'); ?>
<?php echo $form->textField($resume,'data1'); ?>
<?php echo $form->textField($resume,'data2'); ?>
<?php echo $form->textField($profile,'data3'); ?>
<?php echo $form->textField($profile,'data4'); ?>
<?php $this->endWidget(); ?>

Resources