Thứ Hai, 1 tháng 10, 2018

Cach hiển thị lỗi validation chi tiết đến từng thành phần của form

Ta có SignUpForm

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Password;
use Phalcon\Forms\Element\Submit;
use Phalcon\Forms\Element\Check;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Email;
use Phalcon\Validation\Validator\Identical;
use Phalcon\Validation\Validator\StringLength;
use Phalcon\Validation\Validator\Confirmation;
class SignUpForm extends Form
{
public function initialize($entity = null, $options = null)
{
$name = new Text('name');
$name->setLabel('Name');
$name->addValidators([
new PresenceOf([
'message' => 'The name is required'
])
]);
$this->add($name);
// Email
$email = new Text('email');
$email->setLabel('E-Mail');
$email->addValidators([
new PresenceOf([
'message' => 'The e-mail is required'
]),
new Email([
'message' => 'The e-mail is not valid'
])
]);
$this->add($email);
// Password
$password = new Password('password');
$password->setLabel('Password');
$password->addValidators([
new PresenceOf([
'message' => 'The password is required'
]),
new StringLength([
'min' => 8,
'messageMinimum' => 'Password is too short. Minimum 8 characters'
]),
new Confirmation([
'message' => 'Password doesn\'t match confirmation',
'with' => 'confirmPassword'
])
]);
$this->add($password);
// Confirm Password
$confirmPassword = new Password('confirmPassword');
$confirmPassword->setLabel('Confirm Password');
$confirmPassword->addValidators([
new PresenceOf([
'message' => 'The confirmation password is required'
])
]);
$this->add($confirmPassword);
// Remember
$terms = new Check('terms', [
'value' => 'yes'
]);
$terms->setLabel('Accept terms and conditions');
$terms->addValidator(new Identical([
'value' => 'yes',
'message' => 'Terms and conditions must be accepted'
]));
$this->add($terms);
// CSRF
$csrf = new Hidden('csrf');
$csrf->addValidator(new Identical([
'value' => $this->security->getSessionToken(),
'message' => 'CSRF validation failed'
]));
$csrf->clear();
$this->add($csrf);
// Sign Up
$this->add(new Submit('Sign Up', [
'class' => 'btn btn-success'
]));
}
/**
* Prints messages for a specific element
*/
public function messages($name)
{
if ($this->hasMessagesFor($name)) {
foreach ($this->getMessagesFor($name) as $message) {
$this->flash->error($message);
}
}
}
}


Note: chú ý function messages 

Trong code volt template signup.volt

<div align="center">
{{ form('class': 'form-search') }}
<div align="left">
<h2>Sign Up</h2>
</div>
<table class="signup">
<tr>
<td align="right">{{ form.label('name') }}</td>
<td>
{{ form.render('name') }}
{{ form.messages('name') }}
</td>
</tr>
<tr>
<td align="right">{{ form.label('email') }}</td>
<td>
{{ form.render('email') }}
{{ form.messages('email') }}
</td>
</tr>
<tr>
<td align="right">{{ form.label('password') }}</td>
<td>
{{ form.render('password') }}
{{ form.messages('password') }}
</td>
</tr>
<tr>
<td align="right">{{ form.label('confirmPassword') }}</td>
<td>
{{ form.render('confirmPassword') }}
{{ form.messages('confirmPassword') }}
</td>
</tr>
<tr>
<td align="right"></td>
<td>
{{ form.render('terms') }} {{ form.label('terms') }}
{{ form.messages('terms') }}
</td>
</tr>
<tr>
<td align="right"></td>
<td>{{ form.render('Sign Up') }}</td>
</tr>
</table>
{{ form.render('csrf', ['value': security.getToken()]) }}
{{ form.messages('csrf') }}
<hr>
</form>
</div>



Không có nhận xét nào:

Đăng nhận xét

Bài 4- Volt Filter -phalcon

  1/ Volt Filter Đây cũng là một bộ lọc giống như phần Filter ở bài 3 , tuy nhiên có bổ sung thêm một số phương thức mới để áp dụng vào ứng ...