1

我试图从模型中获取错误以显示在表单提交中。我不会将此数据保存在数据库的任何位置,因此不使用 save 方法。问题是我无法看到 Model 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'), 
    );

}?>

我可以看到模型规则正在围绕 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>

Tnc 条款和条件

4

1 回答 1

2

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

手册

您所要做的就是:

if ($this->ModelName->validates()) {
    // it validated logic
} else {
    // didn't validate logic
    $errors = $this->ModelName->validationErrors;
}
于 2013-10-11T10:12:01.623 回答