CakePHP 2.3.0表单验证错误未显示不同模型中的上行字段

时间:2013-10-11 10:05:37

标签: validation cakephp formhelper

我正在尝试从模型中获取错误以显示表单提交。我不会将这些数据保存在数据库的任何位置,因此不使用save方法。问题是我无法看到模型AnonymousRider中设置的验证错误。代码如下 - ctp文件

<?php
    echo $this -> Form -> create(null, array('action' => 'confirmBooking'));

    echo $this -> Form -> input('Name', array('type' => 'text', 'id' => 'name', 'placeholder' => 'Name'));
    echo $this -> Form -> input('AnonymousRider.email', array( 'id' => 'email', 'placeholder' => 'Email'));
    echo $this -> Form -> input('AnonymousRider.phone_number', array( 'type' => 'tel', 'id' => 'number', 'placeholder' => 'Mobile Number'));
    echo $this -> Form -> input('address', array('type' => 'textarea', 'id' => 'pickup-point', 'placeholder' => 'pickup address and landmark details', 'required' => 'true'));
    echo $this -> Form -> input('tnc', array('type' => 'checkbox', 'id' => 'tnc', 'required' => 'true'));

    echo $this->Html->link('Terms and conditions', '/pages/home', array('class' => 'button', 'target' => '_blank'));
    $options = array('label' => 'Book Now', 'class' => 'btn btn-success .btn-large');
    echo $this -> Form -> end($options);
    ?>

以下是模特 -     

var $name = 'AnonymousRider';
public $validate = array
    (
        'phone_number' => array('rule' => array('phone', null, 'us'), 'message' => 'valid phone number required'), 
        'email' => array('rule' => 'email', 'message' => 'valid phone number required'), 
    );

}&GT;

我可以看到模型规则正在围绕HTML生成一个必需的。请参阅HTML -

<form action="/91/itinerary_requests/confirmBooking" id="ItineraryRequestConfirmBookingForm" method="post" accept-charset="utf-8">
<div style="display:none;">
    <input type="hidden" name="_method" value="POST">
</div>
<div class="input text">
    <label for="name">Name</label>
    <input name="data[ItineraryRequest][Name]" id="name" placeholder="Name" type="text">
</div>
<div class="input email required">
    <label for="email">Email</label>
    <input name="data[AnonymousRider][email]" id="email" placeholder="Email" maxlength="256" type="email" required="required">
</div>
<div class="input tel required">
    <label for="number">Phone Number</label>
    <input name="data[AnonymousRider][phone_number]" id="number" placeholder="Mobile Number" type="tel" required="required">
</div>
<div class="input textarea">
    <label for="pickup-point">Address</label>       <textarea name="data[ItineraryRequest][address]" id="pickup-point" placeholder="pickup address and landmark details" required="required" cols="30" rows="6"></textarea>

                          跨国公司     条款和条件                   

1 个答案:

答案 0 :(得分:2)

您可以在控制器中验证数据

请参阅manual

所有你需要做的就是:

if ($this->ModelName->validates()) {
    // it validated logic
} else {
    // didn't validate logic
    $errors = $this->ModelName->validationErrors;
}