CakePHP Validation Error Message - cakephp

I am trying to modify the way CakePHP shows the error message. Below is the generic template that it uses to display errors
<div class="error-message">Please enter at least 3 characters.</div>
I want to style the error like below.
<label class="error">Please enter at least 3 characters.</label>

this did the job. i was missing the text parameter.
<?php echo $this->Form->error('name', null, array('wrap' => 'label', 'class' => 'error')); ?>

echo $this->Form->label("Please enter at least 3 xter",array('class'=>'error');

Related

How to give id to an image in CakePHP?

I'm inserting an image in my template file as
<?php echo $this->Html->image('profile_picture.png', array('alt'=>'frame partner avatar'), array('id'=>'framePartnerAvatar')); ?>
Now I'm trying to give style to this image by using the id mentioned, but on checking through Firebug only source and alternate name is visible. Image id is not coming there.
You have to use the correct syntax:
<?php echo $this->Html->image(
'profile_picture.png',
array(
'alt'=>'frame partner avatar',
'id'=>'framePartnerAvatar'
)); ?>
See HtmlHelper::image()
According to the documentation the method used to create images requires two parameters. The first one being the source link and the second one being the options parameter
$this->Html->image('cake_logo.png', ['alt' => 'CakePHP']);
Your code should be something along the lines of this
$this->Html->image('profile_picture.png', array(
'alt'=>'frame partner avatar',
'id'=>'framePartnerAvatar')
);
$thumb_img = $this->Html->image('yourimage.png',array('alt'=>'yoursite.com','id'=>'yourId'));
Please ensure that all HTML attributes such as alt, id, class etc needs to be written within a single array, and not multiple arrays.
<?php echo $this->Html->image('profile_picture.png', array(
'alt'=>'frame partner avatar',
'id'=>'framePartnerAvatar'
)
); ?>
This will certainly generate the following HTML:
<img src="/img/profile_picture.png" alt='frame partner avatar' id='framePartnerAvatar' />
Peace! xD

Cakephp Validation

I am using an Account Controller which doesnt have its own table but uses User Model.
All works fine except - when I validate any form. It says validation fails (when I try to fail the validation to check) but doesnt throw the error below the field
View
<?php echo $this->Form->input('id'); ?>
<label for="UserPassword">New Password:</label>
<?php echo $this->Form->text('password', array('type' => 'password', 'value' => 'harsha')); ?><em>Password must be min 6 characters.</em> <?php echo $form->error('password'); ?>
Controller Action
if($this->User->saveField('password', $this->data['User']['password'], array('validate' => 'first'))) {
$this->Session->setFlash('Password has been changed.', 'flash-success');
} else {
$this->Session->setFlash('There was some problem with our system. Please try after some time.', 'flash-warning');
}
Try debug()ing the contents of $this->validationErrors in your view, as well as $this->data in your controller just after a form submission. This should give you a lot more information to work from.
I suspect that your problem is Cake is building form inputs based on the wrong model -- building form fields for Account.id and Account.password instead of User.id and User.password. This is because FormHelper takes its default model from the controller/view it's invoked from, which in your case appears AccountsController.
In order to generate the User.id and User.password fields your controller's submission handling expects, you'll need to prepend User. in your FormHelper calls. Thus:
$this->Form->input('User.id');
$this->Form->text('User.password');
Have you tried:
echo $session->flash();
Note that whatever the manual says, it returns, not echoes. I logged this a while back and it has been changed in the 1.3 manual, but not the 1.2.
Hi you who's asking If you want to show error-message that return from UserModel's validate So you can add line code bellow after input form password
<?php
if ($this->Form->isFieldError('password')) {
echo $this->Form->error('password', array('class' => 'error'));
?>
and if you want to show error-message that set by method setFlash
you must redirect page and then use $this->Session->flash('flash-name') in page you want to show it
<?php
//in UsersController
$this->Session->setFlash('message here', 'flash-name');
//in view
echo $this->Session->flash('flash-name');
?>
Good luck!

how to create form in cake php

I am very new in cake php, i want to know how to create form in cake php,please describe,when we go to create a form then what i have to do,like create model and controller everything
In the view file, something like this would work:
<?php
echo $this->Form->create();
echo $this->Form->input('firstname', array('label' => 'Enter your first name:'));
echo $this->Form->input('email', array('label' => 'Enter your email address:'));
echo $this->Form->input('password', array('label' => 'Enter your password:'));
echo $this->Form->end('Save');
?>
In your controller:
if($this->request->is('post')){
$this->User->save( $this->request->data );
}
You can apply some sort of validation in your model, look in the documentation for that.
Best option to learn about cakephp is it's own doc book
But I'm providing you some basic code to create form :
$this->Form->create('ModelName');
$this->Form->input('ModelName.fieldname', array('type'=>'text', 'label'=>'Modified-Name'));
$this->Form->end(__('Submit'));
Here array('type'=>'text'...): type shows which type of input field you want.
(...'label'=>'Modified-Name'): By default it shows field text as fieldname but by using 'label' you can modify your field text.
$this->form->create('controlpage',
array(
'action'=>'controll',
'class'=>'class',
'enctype' => 'multipart/form-data',
'onsubmit'=>'return valid()'
));
Block quote
Create form in html save it as ctp
Block quote
And call it in view. enter code hereUse cake php book to read further.

CakePHP - Have the label and form in Form helper on different line

I'm using this code now
echo $form->input('username');
How do I make sure the label shows up on a different line than the input field?
I managed to imitate what I'm trying to do, just want to make sure that I'm using it the right way.
echo $form->label('username', 'Username');
echo $form->input('username', array('label' => false));
Thanks,
Tee
The core of your request is putting a line-break between the <label> and <input> tags created by the FormHelper::input method. You can accomplish this in several ways. Probably the simplest option is the following:
echo $form->input('User.username', array('between'=>'<br />'));
Or you could also use a pure CSS solution, something like:
<style type="text/css">
div.input label { display: block; }
</style>
<?php echo $form->input('User.username'); ?>
This second option would leave you with cleaner PHP in your views, at the cost of more potential layout/stylesheet headaches.
Try this out.
<p>Username</p>
<?php echo $form->input('username', array('div' => false, 'label' => false)) ?>

cakePHP basic ajax form problem

i am trying to do a simple form that adds a new comment using ajax to a blog post (actually this is a part of a cakePHP tutorial)
but the problem is that the submit button do nothing at all
here is the part of code that generates the form in the view.ctp file
<?php echo $ajax->form('/comments/add', 'post', array('url' => '/comments/add', 'update' => 'PostsComments', 'indicator' => 'commentSaved'));?>
<fieldset>
<legend><?php __('Add Comment');?></legend>
<?php
echo $form->hidden('Comment.post_id', array('value' => $post['Post']['id']));
echo $form->input('Comment.name');
echo $form->input('Comment.content');
?>
</fieldset>
<?php echo $form->end('Submit');?>
and here is the add comment action in the comments_controller.php
if (!empty($this->data)) {
$this->Comment->create();
if ($this->Comment->save($this->data)) {
$comments = $this->Comment->find('all',array('conditions'=>array('post_id'=>$this->data['Comment']['post_id']),'recursive'=>-1));
$this->set(compact('comments'));
$this->render('add_success','ajax');
} else {
$this->render('add_failure','ajax');
}
}
the problem is that the action add isnt called from the view ... when i viewed the generated html source code i found something like that
<form id="form304217296" onsubmit="event.returnValue = false; return false;" method="post" action="/php-cake/blog/comments/add"><fieldset style="display:none;">
if i removed the on submit tag manually then the action is called but the add_success.ctp is generated as a new page not as an ajax call.
so what could be the problem ?
finally i have discovered the problem
there was more than one mistake in the tutorial (chapter 8 in CakePHP from novice to professional) after fixing those errors i found another problem is that cakephp v1.2.6 is not compatible with the latest version of prototype (v1.6.1)
as it gives the following error in firebug
Event.observe is not a function
so i used version 1.6.0.3 of prototype and the problem was solved.
for a list of mistakes in this chapter see this
Do you include prototype and scriptaculus in your View/Layout (both are needed for the Ajax-helper)? Also there might be an interference with jquery if you use that too

Resources