Validation error not displayed CakePHP - validation

I'm trying to display validation errors with CakePHP (Newbie) but I'm stuck. I get this error "Delimiter must not be alphanumeric or backslash". Don't know if the logic is respected, I'm starting from scratch.
Nothing is displayed. Here's my code:
User model
class User extends AppModel {
public $validate = array(
'nom' => array(
'message' => 'Saisie obligatoire',
'required' => true
),
'prenom' => array(
'message' => 'Saisie obligatoire',
'required' => true
),
'date_naissance' => array(
'rule' => array('date','dmy'),
'message' => 'Veuillez respecter le format de la date (jour/mois/année)',
'allowEmpty' => true
),
'email' => array(
'rule' => 'email',
'message' => 'Veuillez introduire une adresse mail valide',
'required' => true
),
'password' => array(
'rule' => 'password',
'message' => 'Un mot de passe est requis'
)
);
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
}
add function into UsersController
public function add() {
if ($this->request->is('post')) {
$this->User->set($this->request->data);
if ($this->User->validates()) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Auth->login($this->User);
return $this->redirect('/index');
}
}
} else {
return $this->User->validationErrors;
}
}
add.ctp
<?= $this->element('navbar');?>
<div class="formcontainer">
<div class="page-header">
<h1>Rejoignez-nous</h1>
</div>
<form action="/users/add" id="UserAddForm" method="post" accept-charset="utf-8">
<div style="display:none;">
<input type="hidden" name="_method" value="POST"/>
</div>
<div class="form-group input text">
<label for="UserNom">Nom:</label>
<input name="data[User][nom]" maxlength="20" type="text" id="UserNom" class="form-control" placeholder="requis">
</div>
<div class="form-group input text">
<label for="UserPrenom">Prénom:</label>
<input name="data[User][prenom]" maxlength="20" type="text" id="UserPrenom" class="form-control" placeholder="requis">
</div>
<div class="form-group input text">
<label for="UserDateNaissance">Date de naissance:</label>
<input name="data[User][date_naissance]" maxlength="20" type="text" id="UserDateNaissance" class="form-control">
</div>
<div class="form-group input email">
<label for="UserEmail">Email:</label>
<input name="data[User][email]" maxlength="100" type="email" id="UserEmail" class="form-control" placeholder="requis"/>
</div>
<div class="form-group input password">
<label for="UserPassword">Mot de passe:</label>
<input type="password" name="data[User][password]" class="form-control" id="UserPassword" placeholder="requis">
</div>
<button type="submit" class="btn btn-default bSub">M'inscrire</button>
</form>
</div>

Your validation should be like this(You must add rule for field and there is no inbuilt passowrd rule in cakephp).
public $validate = array(
'nom' => array(
'rule' => 'notEmpty', //add rule here
'message' => 'Saisie obligatoire',
'required' => true
),
'prenom' => array(
'rule' => 'notEmpty', //add rule here
'message' => 'Saisie obligatoire',
'required' => true
),
'date_naissance' => array(
'rule' => array('date', 'dmy'),
'message' => 'Veuillez respecter le format de la date (jour/mois/année)',
'allowEmpty' => true
),
'email' => array(
'rule' => 'email',
'message' => 'Veuillez introduire une adresse mail valide',
'required' => true
),
'password' => array(
'rule' => 'notEmpty', //there is no inbuilt validation rule with name *password*
'message' => 'Un mot de passe est requis'
)
);
If you want to show error message then you should use Form Helper for create form inputs like this
echo $this->Form->input("User.nom", array("class"=>"form-control", "placeholder"=>"requis", 'label'=>false));
Or you can display message by using isFieldError method of Form helper
if ($this->Form->isFieldError('nom')) {
echo $this->Form->error('nom');
}

// Model
class User extends AppModel {
public $validate = array(
'password' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Un mot de passe est requis'
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'email' => array(
'email' => array(
'rule' => array('email'),
'message' => 'Veuillez introduire une adresse mail valide',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'date_naissance' => array(
'date' => array(
'rule' => array('date'),
'message' => 'Veuillez respecter le format de la date (jour/mois/année)',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'prenom' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Saisie obligatoire',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'nom' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Saisie obligatoire',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
}
// Controller
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
// add.ctp
<div class="users form">
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Add User'); ?></legend>
<?php
echo $this->Form->input('password');
echo $this->Form->input('email');
echo $this->Form->input('date_naissance');
echo $this->Form->input('prenom');
echo $this->Form->input('nom');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Users'), array('action' => 'index')); ?></li>
</ul>
</div>

Related

send an email with attachment using a form

I can't understand what the problem may be:
I state that the code without the part of the attachment works
function submit(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'file' => 'mimes:pdf,doc,docx'
]);
$data = array(
'name_data' => $request->name,
'cognome_data' => $request->cognome,
'luogo_data' => $request->luogo,
'date_data' => $request->date,
'telefono_data' => $request->telefono,
'email_data' => $request->email,
'citta_data' => $request->citta,
'provincia_data' => $request->provincia,
'studio_data' => $request->studio,
'lingua_data' => $request->lingua,
'livello_data' => $request->livello,
'lingua2_data' => $request->lingua2,
'livello2_data' => $request->livello2,
'file_data' => $request->file,
'agree_data' => $request->agree
);
Mail::send('mail', $data, function($message) use ($request,$data){
$message->to('pipo#gmail.com', 'piooi')->subject('Send mail ' . $request->name);
$message->from($request->email, $request->name);
if ( isset($data['file_data']))
{
$message->attach($data['file_data']->getRealPath(), array(
'as' => $data['file_data']->getClientOriginalName(),
'mime' => $data['file_data']->getMimeType()));
}
});
Session::flash('success', 'Mail spedita con sucesso');
}
}
I put the piece of the form in question:
<form class="text-left form-email"action="#" enctype="multipart/form-data" method="POST">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupFileAddon01">Curriculum Vitae:</span>
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" id="file" name="file"
aria-describedby="inputGroupFileAddon01">
<label class="custom-file-label" for="inputGroupFile01">Seleziona il file</label>
</div>
</div>
the error that gives the summit is the following:
local.ERROR: Call to a member function getRealPath() on null {"exception":"[object] (Error(code: 0):
Change your mail part to this and see if it works
Mail::send('mail', $data, function($message) use ($request,$data){
$message->to('pipo#gmail.com', 'piooi')->subject('Send mail ' . $request->name);
$message->from($request->email, $request->name);
if($request->hasFile('file')){
$message->attach($request->file->getRealPath(), array(
'as' => $request->file->getClientOriginalName(),
'mime' => $request->file->getMimeType())
);
}
});

Send email in php using codeigniter

ok so here i'm looking for some help. i have a contact us form of a website. It has three fields name, email and message. What i need to do is when user fills the form email should be send on admin email which display message of the user and its email and name too. I have a code which is working fine it gives me the message of email sent but when i open mail there is no email. Kindly i need your help. Here is the code
<h3 class="title-big wow fadeIn">Contact Us</h3>
<br>
</div>
<div class="col-md-12 text-center">
<?php if($success != ""): ?>
<span class="col-md-4 col-md-offset-4" style="color:#D91E18"> <?php echo $success;?></span>
<?php endif; ?>
<div class="form-group col-md-offset-4 col-md-4 col-md-offset-4 wow fadeInDown">
<form name="myform" role="form" action="sendmail" method="post">
<div class="form-group">
<?php echo form_input($first_name);?>
</div>
<div class="form-group">
<?php echo form_input($email);?>
</div>
<div class="form-group">
<?php echo form_textarea($message1);?>
<br>
<a class="sign-up-button btn btn-border btn-lg col-md-offset-3 col-md-6 col-md-offset-3 wow fadeInUp" data-wow-delay="1s" href="javascript: submitform()">Submit Query</a>
</div>
</form>
controller.php
public function contact()
{
$data['success'] = (validation_errors() ? validation_errors() : ($this->session->flashdata('message')));
//$data['message'] = (validation_errors() ? validation_errors() : ($this->session->flashdata('message')));
$data['message1'] = array(
'name' => 'message1',
'id' => 'message1',
'class' => 'form-control',
'placeholder' => "Enter message here...",
'size' => 32,
'maxlength' => 500,
'rows' => 5,
'cols' => 41,
);
$data['first_name'] = array(
'name' => 'first_name',
'id' => 'first_name',
'type' => 'text',
'class' => 'form-control',
'placeholder' => "First Name...",
'size' => 32,
'maxlength' => 50,
);
$data['email'] = array(
'name' => 'email',
'id' => 'email',
'type' => 'text',
'class' => 'form-control',
'placeholder' => "Email Address (Mandatory)",
'size' => 32,
'maxlength' => 128,
);
$this->load->view('contact',$data);
}
public function sendmail()
{
$this->form_validation->set_rules('first_name', 'Name', 'required');
//$this->form_validation->set_rules('last_name', 'Name', 'required');
$this->form_validation->set_rules('message1', 'Message', 'required');
$this->form_validation->set_rules('email', 'Email Address', 'required');
//$this->form_validation->set_rules('email_confirm', 'Reenter Email Address', 'required');
if ($this->form_validation->run() == true)
{
$data = array(
'first_name' => $this->input->post('first_name'),
//'last_name' => $this->input->post('last_name'),
'message1' => $this->input->post('message1'),
'email' => $this->input->post('email'),
);
//if ($this->form_validation->run() == true)
//{
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'auth' => true,
'smtp_user' => '********#gmail.com',
'smtp_pass' => '********'
);
$emailsubject = $data['first_name']." ".$data['last_name']." has sent a Query message.";
$this->load->library('email',$config);
$this->email->set_newline("\r\n");
// $this->email->initialize($config);
$this->email->from('********#gmail.com', 'AOTS Lahore Regional Center');
//$this->email->to('********#gmail.com');
$this->email->to('********#gmail.com');
$this->email->cc('********#gmail.com');
$this->email->subject($emailsubject);
$this->email->message($data['message1']."\nEmail ID: ".$data['email']);
if ($this->email->send())
{
$data['success'] = "Your Query has been sent successfully... !!";
//$data['message'] = (validation_errors() ? validation_errors() : ($this->session->flashdata('message')));
$data['message1'] = array(
'name' => 'message1',
'id' => 'message1',
'class' => 'form-control',
'placeholder' => "Enter message here...",
'size' => 32,
'maxlength' => 500,
'rows' => 5,
'cols' => 41,
);
$data['first_name'] = array(
'name' => 'first_name',
'id' => 'first_name',
'type' => 'text',
'class' => 'form-control',
'placeholder' => "First Name...",
'size' => 32,
'maxlength' => 50,
);
$data['email'] = array(
'name' => 'email',
'id' => 'email',
'type' => 'text',
'class' => 'form-control',
'placeholder' => "Email Address (Mandatory)",
'size' => 32,
'maxlength' => 128,
);
$this->load->view('contact',$data);
}
else
{
$data['success'] = show_error( $this->email->print_debugger());
}
}
//}
else
{
$data['success'] = (validation_errors() ? validation_errors() : ($this->session->flashdata('message')));
$data['message1'] = array(
'name' => 'message1',
'id' => 'message1',
'class' => 'form-control',
'placeholder' => "Enter message here...",
'size' => 32,
'maxlength' => 500,
'rows' => 5,
'cols' => 41,
);
$data['first_name'] = array(
'name' => 'first_name',
'id' => 'first_name',
'type' => 'text',
'class' => 'form-control',
'placeholder' => "First Name...",
'size' => 32,
'maxlength' => 50,
);
$data['email'] = array(
'name' => 'email',
'id' => 'email',
'type' => 'text',
'class' => 'form-control',
'placeholder' => "Email Address (Mandatory)",
'size' => 32,
'maxlength' => 128,
);
$this->load->view('contact',$data);
}
}
********#gmail.com is the email where mail should be send
You need to use :
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => '********#gmail.com',
'smtp_pass' => '********',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
In order to send an email from (say) youremailid#gmail.com to an email id *****#gmail.com follow the steps below:
step 1) the config array as below
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'auth' => true,
'smtp_user' => 'youremailid*****#gmail.com', // email id of sender in this case 'youremailid#gmail.com' is the sender
'smtp_pass' => 'passwordofyouremailid', // password of sender gmail id ie. password of 'youremailid#gmail.com'
'newline' => "\r\n",
);
step 2) Load the email library and call initialize
$this->load->library('email',$config);
$this->email->initialize($config);
step 3) After setting subjects and other info into variables do the following
$this->email->from('youremailid****#gmail.com', 'Sender Name'); // sender emailid and name in this case `youremailid#gmail.com` is sending email
$this->email->to('*****#gmail.com'); // receiver email id in this case '*****#gmail.com' is to whome you want to send . so *****#gmail.com is the email id
$this->email->cc('*****#gmail.com'); // same as above if you want cc to include
$this->email->subject($emailsubject); // subject of email
if ($this->email->send())
{
// rest of your code
}
remember there youremailid#gmail.com is the sender and *****#gmail.com is the receiver.

Notice: Undefined index

I have this error message when I submit form.
Notice: Undefined index: title in
C:\xampp\htdocs\ameyaw\module\BusinessGhana\src\Service\AutosManager.php
on line 38
Notice: Undefined index: description in
C:\xampp\htdocs\ameyaw\module\BusinessGhana\src\Service\AutosManager.php
on line 39
Notice: Undefined index: featured in
C:\xampp\htdocs\ameyaw\module\BusinessGhana\src\Service\AutosManager.php
on line 58
Message:
An exception occurred while executing 'INSERT INTO auto (title,
description, featured, date_created) VALUES (?, ?, ?, ?)' with params
[null, null, null, "2017-06-15 05:04:44"]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title'
cannot be null
this is my form and fieldset
use Zend\Form\Fieldset;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use BusinessGhana\Entity\Autos;
class AddFieldset extends Fieldset
{
protected $objectManager;
public function init()
{
$this->add([
'type' => 'text',
'name' => 'title',
'attributes' => [
'id' => 'autoTitle'
],
'options' => [
'label' => 'Title',
'display_empty_item' => true,
'empty_item_label' => 'Maximum of 60 characters',
],
]);
$this->add([
'type' => 'textarea',
'name' => 'description',
'attributes' => [
'id' => 'autoDescription'
],
'options' => [
'label' => 'Description',
'display_empty_item' => true,
'empty_item_label' => 'description',
],
]);
$this->add([
'type' => 'radio',
'name' => 'featured',
'attributes' => [
'id' => 'autoFeatured'
],
'options' => array(
'label' => 'Featured',
'value_options' => array(
array('value' => '0',
'label' => 'No',
'selected' => true,
'label_attributes' => array(
'class' => 'col-sm-2 btn btn-default',
),
),
array(
'value' => '1',
'label' => 'Yes',
'label_attributes' => array(
'class' => 'col-sm-2 btn btn-danger',
),
),
),
'column-size' => 'sm-12',
'label_attributes' => array(
'class' => 'col-sm-2',
),
),
]);
}
}
use Zend\Form\Form;
//use Zend\InputFilter\InputFilter;
class AddForm extends Form
{
public function init()
{
$this->add([
'name' => 'dependentForm',
'type' => AddFieldset::class,
]);
$this->add([
'type' => 'submit',
'name' => 'submit',
'attributes' => [
'value' => 'Submit',
],
]);
}
}
This is my controller action
public function addAction()
{
// Create the form.
$form = new PostForm();
if ($this->getRequest()->isPost()) {
// Get POST data.
$data = $this->params()->fromPost();
// Fill form with data.
$form->setData($data);
if ($form->isValid()) {
// Get validated form data.
$data = $form->getData();
$this->AutosManager->addNewAutos($data);
return $this->redirect()->toRoute('retrieve');
}
}
return new ViewModel([
'form' => $form
]);
}
I know hydration can solve this problem but I don't know how to use it yet.
thanks for any help.
this is my autosManager
class AutosManager
{
/**
* Entity manager.
* #var Doctrine\ORM\EntityManager;
*/
private $entityManager;
/**
* Constructor.
*/
public function __construct($entityManager)
{
$this->entityManager = $entityManager;
}
public function addNewAutos($data)
{
$autos = new Autos();
$autos->setTitle($data['title']);
$autos->setDescription($data['description']);
$autos->setFeatured($data['featured']);
$currentDate = date('Y-m-d H:i:s');
$autos->setDateCreated($currentDate);
$this->entityManager->persist($autos);
$this->entityManager->flush();
}
}
i can retrieve data from database.
This is where i render my forms:
$form = $this->form;
$fieldset = $form->get('dependentForm');
$title = $fieldset->get('title');
$title->setAttribute('class', 'form-control');
$title->setAttribute('placeholder', 'Maximum of 60 characters');
$title->setAttribute('id', 'autoTitle');
$description = $fieldset->get('description');
$description->setAttribute('class', 'form-control');
$description->setAttribute('placeholder', 'Description here...');
$description->setAttribute('id', 'autoDescription');
$featured = $fieldset->get('featured');
$featured->setAttribute('id', 'autoRadio');
$form->get('submit')->setAttributes(['class'=>'btn btn-primary']);
$form->prepare();
echo $this->form()->openTag($form);
?>
<fieldset>
<div class="form-group">
<?= $this->formLabel($title) ?>
<?= $this->formElement($title) ?>
<?= $this->formElementErrors()->render($title,['class'=>'help-block'])?>
</div>
<div class="form-group">
<?= $this->formLabel($description) ?>
<?= $this->formElement($description) ?>
<?=$this->formElementErrors()->render($description,
['class'=>'help-block'])?>
</div>
<div class="form-group">
<?= $this->formLabel($featured) ?>
<?= $this->formElement($featured) ?>
<?= $this->formElementErrors()->render($featured,
['class' =>'help-block']) ?>
</div>
</div></div><div class="row">
<div class="col-md-4">
</fieldset>
<?= $this->formElement($form->get('submit')); ?>
In your function addNewAutos($data) : your $data variable don't have title, description and featured fields. So php consider these as null, and your $this->entityManager->flush() will try to write null values in database, but you have a constraint that at least title must not be null.
So check your $data array contains the title, description and featured fields before to initiate your Auto object...

CakePHP 3 : Retrieving data from database using Ajax

I am working on a CakePHP 3 project which is having a Apply Coupon form.
I want to apply the coupon using Ajax.
The view of coupon form is
<?= $this->Form->create(null, [
'url' => ['controller' => 'Coupons', 'action' => 'checkCoupon'],
'name' => 'checkCoupon'
]) ?>
<?= $this->Form->input('coupon_code', [
'type' => 'text',
'placeholder' => 'Apply Coupon Code',
'label' => false
]) ?>
<?= $this->Form->submit('Apply Coupon') ?>
and the checkCoupon action in CouponsController is
public function checkCoupon()
{
$this->request->onlyAllow('ajax'); // No direct access via browser URL
if ($this->request->is('post')) {
$couponCode = $this->request->data['coupon_code'];
$couponCheck = $this->Coupons->find('all', [
'conditions' => [
'coupon_code' => $couponCode
]
]);
if ($couponCheck->count() === 1) {
$coupon = $couponCheck->first();
$valid_till = $coupon->valid_till;
$dt = new Time($valid_till);
$date = $dt->format('Y-m-d');
if ($date >= date('Y-m-d')) {
echo 'Coupon is Valid. Discount of '.$coupon->value.'has been applied';
} else {
echo 'Coupon is Expired';
}
} else {
echo 'This is not a valid coupon code';
}
}
}
I want the $coupon->value and $coupon->id to be retrieved and added to the checkout link as
<?= $this->Html->link(__('Confirm Checkout'), ['controller' => 'ServiceRequests', 'action' => 'confirmCheckout', $service->id, $primaryAddressId, $serviceArea->id, $coupon->id], ['class' => 'btn btn-block btn-success']) ?>
The Apply Coupon form is in checkout action of RequestsController
Also the form is working well. I have checked it by removing the onlyAllow('ajax') line and printing values in check_coupon.ctp view.
How could I do it using Ajax ?
Edit 2 : checkout.ctp
<div class="form-info coupon">
<?= $this->Form->create(null, [
'url' => ['controller' => 'Coupons', 'action' => 'ajax_checkCoupon'],
'name' => 'checkCoupon',
'id' => 'checkCoupon'
]) ?>
<?= $this->Form->input('coupon_code', [
'type' => 'text',
'placeholder' => 'Apply Coupon Code',
'label' => false
]) ?>
<label class="hvr-sweep-to-right">
<?= $this->Form->submit('Apply Coupon', ['id' => 'applyCoupon']) ?>
</label>
<label id="couponUpdate"></label>
<label id="loading" style="display:none;">Loading...</label>
<?php
$data = $this->Html->script('#checkCoupon')->serializeForm(['isForm' => true, 'inline' => true]);
$this->Html->script('#checkCoupon')->event(
'submit',
$this->Html->script(
[
'controller' => 'Coupons',
'action' => 'ajax_checkCoupon'
],
[
'update' => '#couponUpdate',
'data' => $data,
'async' => true,
'dataExpression' => true,
'before' => "$('#loading').fadeIn();$('#applyCoupon').attr('disabled','disabled');",
'complete' => "$('#loading').fadeOut();$('#applyCoupon').removeAttr('disabled');"
]
)
);
?>
</div>
Error : Call to a member function serializeForm() on string on line 18 in checkout.ctp

CakePHPform checkbox validation error not shown, but input error

I have a form with required fields, one field is a checkbox. All validation errors will be shown, but no error from checkbox field. I installed DebugKit-Plugin and see, that there will be a validation error, but the message wouldn't shown.
class Users extends AppModel {
public $name = 'Users';
public $validate = array(
'email' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Bitte geben Sie ihre Email-Adresse ein',
'allowEmpty' => false,
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'Diese Adresse ist bereits angemeldet',
),
'email' => array(
'rule' => array('email',true),
'message' => 'Bitte geben Sie eine gültige Email-Adresse ein',
'allowEmpty' => false,
),
),
'terms' => array(
'rule' => array('comparison','!=',0),
'message' => 'Sie müssen unseren Nutzungsbedingungen zustimmen',
'allowEmpty' => false,
),
);
}
Form:
<div class="right text-right" id="register_form">
<?php
echo $this->Form->create('Users', array('action' => 'register', 'inputDefaults' => array()));
echo $this->Form->input('gender', array('label' => 'Anrede: ', 'class' => 'inputField', 'options' => array('m' => 'Herr', 'f' => 'Frau')));
echo $this->Form->input('first_name', array('label' => 'Vorname: ', 'class' => 'inputField'));
echo $this->Form->input('last_name', array('label' => 'Nachname: ', 'class' => 'inputField'));
echo $this->Form->input('email', array('label' => 'Email: ', 'class' => 'inputField'));
echo $this->Form->input('terms', array('div' => true, 'type' => 'checkbox', 'value' => 0, 'label' => false, 'before' => ' <label for="UsersTerms">Ich akzeptiere die '.$this->Html->link('AGB', array('controller' => 'pages', 'action' => 'terms')).' </label>'));
echo $this->Form->submit('Anmelden', array('class' => 'button'));
echo $this->Form->end();
?>
</div>
Controller:
public function register() {
if ($this->Auth->user())
$this->redirect($this->referer('/'));
if ($this->request->is("post")) {
$this->Users->create();
$this->Users->set(Sanitize::clean($this->request->data));
#if ($this->Users->validates())
# die(debug($this->Users->validationErrors));
$this->_hash = $this->Auth->password($this->Users->data['Users']['email']);
$this->_password = $this->__randomString(8, 8);
$this->Users->set('password', $this->Auth->password($this->_password));
if ($this->Users->save()) {
$this->Users->id = $this->Users->getLastInsertID();
$this->_sendActivationMail();
$this->Session->setFlash('An die angegebene Emailadresse wurde soeben ein Aktivierungslink versendet.', 'default', array('class' => 'yellow'));
$this->redirect($this->referer());
} else {
$this->Session->setFlash('Ein Fehler ist aufgetreten', 'default', array('class' => 'red'));
}
}
}
I tried with a "$this->Form->error('terms');" but when I debug this line, in case of error there will be only
<div class="error_message"></div>
CakePHP is last version. Searching since 4 hours for help, but google has no answer. Have you?
Greetings
M.
If anyone has same problem, i could solve mine:
Problem was special char "ü". I tried to save my file with ISO 8859-1 but it needed to save with UTF8. Now error message will be shown.

Resources