Hi,
i started my first project in phalcon (1.3.0) and i must say that I am really fascinated of it ! I worked for the last 2 years with CakePHP but Phalcon is absolutely outstanding.
So, my problem: I have a a form which i want to validate by extending the Form class. The form is shown correctly, but the validation failed of PresenceOf, InclusionIn, Email, StringLength and Confirmation. I tried everything the last 3 days, but could't fix this problem.
<?php
use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Select;
use Phalcon\Forms\Element\Password;
use Phalcon\Forms\Element\Submit;
use Phalcon\Validation;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\InclusionIn;
use Phalcon\Validation\Validator\Email;
use Phalcon\Validation\Validator\StringLength;
use Phalcon\Validation\Validator\Confirmation;
class KundenRegistrationForm extends Form {
public function initialize()
{
### Adresses[anrede]
$anrede = new Select ("anrede" , array("Frau" => "Frau" , "Mann" => "Mann") , array(
'using' => array(
'id',
'name'
),
'name' => 'Adresses[anrede]',
'useEmpty' => true,
'emptyText' => 'Bitte auswählen...',
'emptyValue' => '',
'class' => 'formular',
'required' => 'required'
));
$anrede->setDefault("Frau"); ## SELECTED VALUE
##############################
##############################
##### THIS IS NOT WORKING #####
### Allways shows the message ! ###
##############################
$anrede->addValidators(array(
new InclusionIn(
array(
'field' => 'anrede',
'domain' => array("Frau", "Herr"),
'message' => "Die Auswahl Ihrer ANREDE war falsch."
)
)
));
$this->add($anrede);
#### Adresses[vorname]
$vorname = new Text('vorname', array(
'placeholder' => 'Vorname',
'class' => 'formular',
'required' => 'required',
'name' => 'Adresses[vorname]'
));
$vorname->setDefault("mein nam");
##############################
##############################
##### THIS IS NOT WORKING #####
### Allways shows the message ! ###
##############################
$vorname->addValidators(array(
new PresenceOf(array(
'message' => 'Vorname fehlt'
))
));
$vorname->addFilter('string');
$this->add($vorname);
#### User[email]
$email = new Text('email',array(
'placeholder' => 'Email-Adresse',
'class' => 'formular',
'required' => 'required',
'name' => 'User[email]'
));
//$email->setDefault("[email protected]");
##################################
###################################
##### THIS IS NOT WORKING ###########
### Allways shows the BOTH message ! ###
###################################
$email->addValidators(array(
new PresenceOf(array(
'message' => 'Ihre Email fehlt'
)),
new Email(array(
'message' => 'Ihre Email-Adresse ist falsch'
))
));
$this->add($email);
### Adresses[telefon]
$telefon = new Text( "telefon" , array(
'placeholder' => '0309282733',
'class' => 'formular',
'required' => 'required',
'name' => 'Adresses[telefon]'
));
$telefon->setDefault("4567890");
// $telefon->addValidators(array(
// new Numericality(
// array(
// 'field' => 'telefon'
// )
// )
// ));
$this->add($telefon);
#### User[passwort]
$passwort = new Password("passwort", array(
'class' => 'formular',
'required' => 'required',
'name' => 'User[passwort]'
));
$passwort->setDefault("asdfsafd");
##############################
##############################
##### THIS IS NOT WORKING #####
### Allways shows the message ! ###
##############################
$passwort->addValidators(array(
new PresenceOf(array(
'message' => 'Bitte tragen Sie ein Passwort ein.'
)),
new StringLength(array(
'min' => 6,
'messageMinimum' => 'Das Passwort muss mindestens aus 6 Zeichen bestehen.'
)),
new Confirmation(array(
'message' => 'Die Passwörter stimmen nicht überein.',
'with' => 'passwort2'
))
));
$this->add($passwort);
#### User[passwort2]
$passwort2 = new Password("passwort2", array(
'class' => 'formular',
'required' => 'required',
'name' => 'User[passwort2]'
));
$passwort2->setDefault("asddfsafd");
##############################
##############################
##### THIS IS NOT WORKING #####
### Allways shows the message ! ###
##############################
$passwort2->addValidators(array(
new PresenceOf(array(
'message' => 'Bitte wiederholen Sie Ihr Passwort'
))
));
$this->add($passwort2);
}
}
in My Controller:
<?php
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Controller;
class UsersController extends ControllerBase
{
public function registerAction()
{
$test = new KundenRegistrationForm();
if(!$test->isValid($this->request->getPost("Adresses"))){
foreach ($test->getMessages() as $message) {
echo "<br />Adresses - Message: ", $message->getMessage();
echo "<br />Field: ", $message->getField();
echo "<br />Type: ", $message->getType();
echo "<br />";
}
}
if(!$test->isValid($this->request->getPost("User"))){
foreach ($test->getMessages() as $message) {
echo "<br />Users -- Message: ", $message->getMessage();
echo "<br />Field: ", $message->getField();
echo "<br />Type: ", $message->getType();
echo "<br />";
}
}
$this->view->form = $test;
}
}
What i am doing wrong?
I would be very grateful for any help.
I also compared my code with Pahlcon Vakuro , but without success to get it work. :-(