I have a little problem with a form in cakephp. When the view render the first time I can introduce info in all the inputs but after a submit if a validation fail the first input blocks, I can't introduce more info in there but in the rest I can. here is the view:
<?php
echo $this->Form->create('User');
echo $this->Form->input('phone');
echo $this->Form->input('complete_name');
echo $this->Form->input('adress');
echo $this->Form->input('state');
echo $this->Form->input('city');
echo $this->Form->input('zip_code');
echo $this->Form->button('Save', array('name' =>'save'));
echo $this->Form->end();
?>
In the controller:
public function user_info(){
if($this->request->is('post'))
{
if($this->User->save($this->request->data))
{
$this->redirect(array('controller' => 'users', 'action' => 'user_store'));
}
else
{
$this->Session->setFlash(__('problem saving'), 'flash_bad');
}
}
Validations of the model:
public $validate = array(
'complete_name' => array(
'notempty' => array(
'rule' => array('custom','/^[a-z A-ZáéíóúÁÉÍÓÚñÑäëïöüÄËÏÖÜ]{5,}$/i'),
'message' => 'fail the name'
),
'maxlength' => array(
'rule' => array('maxlength', 45)
)
),
'adress' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'fail adress',
),
'maxlength' => array(
'rule' => array('maxlength', 60),
),
'minlength' => array(
'rule' => array('minlength', 10),
'message' => 'fail adress'
)
),
'state' => array(
'notempty' => array(
'rule' => array('custom', '/^[a-z A-ZáéíóúÁÉÍÓÚñÑäëïöüÄËÏÖÜ]{5,}$/i'),
'message' => 'fail state'
),
'maxlength' => array(
'rule' => array('maxlength', 30)
)
),
'city' => array(
'notempty' => array(
'rule' => array('custom', '/^[a-z A-ZáéíóúÁÉÍÓÚñÑäëïöüÄËÏÖÜ]{5,}$/i'),
'message' => 'fail city'
),
'maxlength' => array(
'rule' => array('maxlength', 30)
)
),
'zip_code' => array(
'notempty' => array(
'rule' => '/^[1-9]{4,4}$/i',
'message' => 'fail zip code'
)
)
);
when a validation fail I can see the message in the view and the session flash too and I can edit everything except the phone. if I exchange lines in the view code like this:
<?php
echo $this->Form->create('User');
echo $this->Form->input('complete_name'); // change position with phone
echo $this->Form->input('phone');
echo $this->Form->input('adress');
echo $this->Form->input('state');
echo $this->Form->input('city');
echo $this->Form->input('zip_code');
echo $this->Form->button('Save', array('name' =>'save'));
echo $this->Form->end();
?>
Then I can't edit complete_name. someone know what is happening?
Based on what you describe, I think css or jQuery is placing a div that extends to that input preventing you from editing it, perhaps a div with an error class or even padding on that first input can cause issues. Regardless, use a developer tool like Firebug in Firefox to investigate. You will be able to see whether that is what is happening.
Related
I am trying to add a form field for gender below is my code :
$options = ['m' => 'Male', 'f' => 'Female'];
echo $this->Form->select('gender', $options);
But in my view file I am unable to see lebel gender is there any other code which can help me suggest .
From->select doesn't give a proper label. Just do:
echo $this->Form->input('gender', array(
'options' => $options,
'type' => 'select',
'empty' => 'Select the gender',
'label' => 'Gender'
)
);
<?= $this->Form->select('gender', ['m' => 'Male', 'f' => 'Female'], ['class' => 'form-control', 'required' => true])?>
In module.config.php
'router' => array(
'routes' => array(
'blog' => array(
'type' => 'Literal',
'options' => array(
'route' => '/page',
'defaults' => array(
'controller' => 'Blog\Controller\List',
'action' => 'index'
)
),
'may_terminate' => true,
'child_routes' => array(
'detail' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:id',
'defaults' => array(
'action' => 'detail'
),
'constraints' => array(
'id' => '[0-9]+'
)
)
)
)
)
)
),
...
'navigation' => array(
'default' => array(
array(
'label' => 'Home',
'route' => 'home',
),
array(
'label' => 'About Us',
'route' => 'blog',
'params' => array('id' => '1'),
),
),
),
...
And the ListController.php
public function detailAction()
{
$id = $this->params()->fromRoute('id');
try {
$post = $this->postService->findPost($id);
} catch (\InvalidArgumentException $ex) {
return $this->redirect()->toRoute('blog');
}
return new ViewModel(array(
'post' => $post
));
}
View:
<?php echo $this->navigation('navigation')->menu()->setUlClass('menu menu-home')->escapeLabels(false); ?>
Result only show
<ul class="menu menu-home">
<li class="active">
Home
</li>
<li>
About Us
</li>
</ul>
How to fix this route url, to show url http://domain.com/page/1
I think you are referencing the parent route blog instead of the child route blog/detail. So even though you specified a param it'll get ignored as the parent route does not expect/need a param. Try this:
array(
'label' => 'About Us',
'route' => 'blog/detail', // instead of 'blog'
'params' => array('id' => '1'),
),
I have a function to reset a password.
At the point where I have to compare the password and the password_confirm field, I get a strange behavior:
My form
echo $this->Form->hidden('tkn', array('value' => $tkn));
echo $this->Form->hidden('uid', array('value' => $uid));
echo $this->Form->input('password',array('type' => 'password', 'name' => 'data[Appuser][password]'));
echo $this->Form->input('password_confirm',array('type' => 'password', 'name' => 'data[Appuser][password_confirm]'));
My model validation:
var $validate = array(
'password' => array(
'rule' => array('minLength', '6'),
'message' => '{password} minLength 6!'
),
'password_confirm' => array(
'rule' => array('equaltofield','password'),
'message' => '{password_confirm} not equal!'
),
);
function equaltofield($val1, $val2){
return $this->data[$this->alias][key($val1)] == $this->data[$this->alias][$val2];
}
My Controller:
if($this->Appuser->save($this->data)){
$this->Session->setFlash(__('Password has been updated'));
}else{
debug($this->Appuser->invalidFields());
}
Now:
When I submit an empty form I get the following returned from the invalidFields()
array(
'password' => '*****',
'password_confirm' => array(
(int) 0 => '{password_confirm} not equal!',
(int) 1 => '{password_confirm} not equal!'
)
)
Question 1: Why do I not get the message, that the password has not its minlength?
Question 2: Why do I get the second message twice for comparing the password?
When typing in 2 different password with min length, I get this again:
array(
'password_confirm' => array(
(int) 0 => '{password_confirm} not equal!',
(int) 1 => '{password_confirm} not equal!'
)
)
When debug($this->data) I also get this (if that helps somehow)
array(
'Api' => array(
'tkn' => '6837d241bf1076c3c55a95abbcfafa04dc19a33c',
'uid' => '1'
),
'Appuser' => array(
'password' => '*****',
'password_confirm' => 'asdfgh'
)
)
Any ideas regarding my two questions above?
Thanks in advance!!
Try this
var $validate = array(
'password' => array(
'rule' => array('minLength', '6'),
'message' => '{password} minLength 6!'
),
'password_confirm' => array(
'rule' => 'equaltofield',
'message' => '{password_confirm} not equal!'
),
);
function equaltofield($data){
return $this->data[$this->alias]['password'] == $this->data[$this->alias]['password_confirm'];
}
I am adding a consent checkbox to an existing form. I am not able to render the label to the right of the checkbox. What am I doing wrong?
Please note that the check box has created using $this->addElement( because the rest of the form was created this way.
I thank you in advance.
$this->addElement('checkbox', 'isUserConsent', array(
'options' => array(array('placement' => 'APPEND')),
'label' => 'Plz activate',
'validators' => array(
array('InArray', false, array(
'hay' => array(1),
'messages' => 'Please check the consent box'),
)
),
'decorators' => array('ViewHelper','Errors','Label'),
));
The default is to prepend the label, but you can change this by modifying the decorator's 'placement' option:
$this->getElement('isUserConsent')->getDecorator('label')->setOption('placement', 'append');
Edit: I never use this syntax for decorators but it should be something like this:
$this->addElement('checkbox', 'isUserConsent', array(
'options' => array(array('placement' => 'APPEND')),
'label' => 'Plz activate',
'validators' => array(
array('InArray', false, array(
'hay' => array(1),
'messages' => 'Please check the consent box'),
)
),
'decorators' => array(
'ViewHelper',
'Errors',
'Label' => array(
'placement' => 'append'
)
),
));
This is the code for rendering lable of a form element.
$this->form->name->renderLabel() ;
I'm trying to create a login form for my web application.
Form validation errros are not showing even though I'm using the $validate Array.
user.php
public $validate = array(
'email' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'notEmpty',
'required' => true
),
'isEmail' => array(
'rule' => 'email'
),
'isUnique' => array(
'rule' => 'isUnique'
)
),
'password' => array(
'notEmpty' => array(
'rule' => 'notEmpty'
),
'minLength' => array(
'rule' => array('minLength', 8)
)
)
);
I can't see an error in my user model, so I show you my controller and my view.
users_controller.php
class UsersController extends AppController {
public $name = 'Users';
public $helpers = array(
'Form'
);
public function login() {
if(!empty($this->data)) {
if ($this->Auth->user() != null) {
$this->Session->setFlash('You are now logged in.', 'flash/success');
$this->redirect('/');
} else {
$this->Session->setFlash('You could not get logged in. Please see errors below.', 'flash/error');
}
}
}
login.ctp
echo $this->Form->create('User', array('action' => 'login'));
echo $this->Form->input('User.email', array(
'label' => __('email address:', true),
'error' => array(
'notEmpty' => __('Email address must not be blank.', true),
'isEmail' => __('Email address must be valid.', true),
)
));
echo $this->Form->input('User.password', array('label' => __('password:', true)));
echo $this->Form->end('Log in');
I hope you can help me. I can't find my mistake since hours. Is there maybe a component or an helper which I need to include?
put echo $this->Session->flash('auth'); before form->create. You don't have to validate login form, Auth will take care of that for you. Read the cookbook: http://book.cakephp.org/view/1250/Authentication
Since you are using Auth, the minLength validation for password is useless.
Validation doesn't occur automatically unless you're saving into the database. Change the first line of the login method in the controller to
if( !empty( $this->data ) && $this->User->validates() ) {
...