Hi guys,
I have a question about form validation, why is it that validation messages still appear even if I already enter the right value in a form, i.e. an email textbox , i entered [email protected]
and hit the send button but still error message is there instead it should disappear. I want to achieve this by using phalcon form validation and not by javascript or jquery. thnks :)
here is my code
<?php
use \Phalcon\Forms\Form,
\Phalcon\Forms\Element\Text,
\Phalcon\Forms\Element\Password,
\Phalcon\Forms\Element\Email,
\Phalcon\Forms\Element\Radio,
\Phalcon\Forms\Element\Select,
\Phalcon\Forms\Element\Submit,
\Phalcon\Validation,
\Phalcon\Validation\ValidatorInterface,
\Phalcon\Validation\Validator\PresenceOf,
\Phalcon\Validation\Validator\StringLength,
\Phalcon\Validation\Validator\Confirmation,
\Phalcon\Validation\Message,
\Phalcon\Escape,
\Phalcon\Mvc\Model,
\Phalcon\Db\RawValue,
\Phalcon\Db\Column,
\Phalcon\Http\Request as FormRequest;
class RegisterForm extends Form{
private static $reg_frm = [];
private static $request;
public function initialize(){
/* Fullname */
self::$reg_frm['fullname'] = new Text('fullname',[
'maxlength' => 255,
'placeholder' => '',
'class' => 'form-control txt-name',
'autocomplete' => 'off',
'autofocus' => 'autofocus'
]
);
/* Email */
self::$reg_frm['email'] = new Email('email', [
'maxlength' => 25,
'placeholder' => '',
'class' => 'form-control txt-email',
'autocomplete' => 'off'
]);
/* Password */
self::$reg_frm['password'] = new Password('password', [
'maxlength' => 25,
'placeholder' => '',
'class' => 'form-control txt-password',
'autocomplete' => 'off'
]);
/* Re Password */
self::$reg_frm['repassword'] = new Password('repassword', [
'maxlength' => 25,
'placeholder' => '',
'class' => 'form-control txt-password',
'autocomplete' => 'off'
]);
/* Male */
self::$reg_frm['gender'] = new Radio('gender1', [
'value' => 'male',
'name' => 'gender',
'class' => 'male'
]);
/* Female */
self::$reg_frm['gender'] = new Radio('gender2', [
'value' => 'female',
'name' => 'gender',
'class' => 'female'
]);
/* Country */
self::$reg_frm['country'] = new Select('country',
TblCountry::find(array(
'columns' => 'country_name'
)),
array(
'useEmpty' => true,
'emptyText' => '',
'using' => array('country_name', 'country_name'))
);
self::$reg_frm['submit'] = new Submit('submit', [
'value' => 'Submit',
'class' => 'btn btn-primary',
'id' => 'btn'
]);
/* Form labels */
self::$reg_frm['fullname']->setLabel('Fullname :');
self::$reg_frm['email']->setLabel('Email :');
self::$reg_frm['password']->setLabel('Password :');
self::$reg_frm['repassword']->setLabel('Re-Password :');
self::$reg_frm['gender']->setLabel('Male :');
self::$reg_frm['gender']->setLabel('Female :');
self::$reg_frm['country']->setLabel('Country :');
/* Add all form element */
foreach (self::$reg_frm as $formelement) {
$this->add($formelement);
}
self::$request = new FormRequest;
$validation = new Validation();
$validation
->add(self::$reg_frm['fullname'], new PresenceOf(array(
'message' => 'Fullname is required'
)))
->add(self::$reg_frm['email'], new PresenceOf(array(
'message' => 'Email is required'
)))
->add(self::$reg_frm['password'], new PresenceOf(array(
'message' => 'Password is required'
)))
->add(self::$reg_frm['password'], new Confirmation(array(
'message' => 'Password doesn\'t match confirmation',
'with' => 'confirmPassword'
)))
->add(self::$reg_frm['repassword'], new PresenceOf(array(
'message' => 'Confirm is required'
)))
->add(self::$reg_frm['country'], new PresenceOf(array(
'message' => 'Country is required'
)));
$messages = $validation->validate($_POST);
if (count($messages)) {
foreach ($messages as $message) {
echo'<p class="p-error">'.$message.'</p>';
}
}
}
}