实际上,我的HTML表单包含名称相同的文本框,例如CakePHP 2.8.5中的name="data[User][username]"
。
如何为此表单字段编写jQuery表单验证代码。为什么,因为当我使用验证时,我必须使用文本框字段名称,我不能将整个文本框名称用作data[User][username]
。
我的代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<?php echo $this->Html->css('jquery-ui.min.css'); ?>
<?php echo $this->Html->script('jquery.js'); ?>
<?php echo $this->Html->script('jquery-ui.min.js'); ?>
<?php echo $this->Html->script('jquery.validate.min'); ?>
<script>
$(function() {
// Setup form validation on the #register-form element
$("#UserAddForm").validate({
// Specify the validation rules
rules: {
username: "required",
password: {
required: true,
minlength: 5
},
full_name: "required",
email: {
required: true,
email: true
},
phone: "required",
role: "required"
},
// Specify the validation error messages
messages: {
username: "Please enter the Username",
full_name: "Please enter your Full Name",
password: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long"
},
email: "Please enter a valid email address"
},
});
});
</head>
<body>
<div class="col-lg-6">
<form role="form" name="add_userform" role="form" accept-charset="utf-8" enctype="multipart/form-data" method="post" id="UserAddForm" action="/invl_exams/users/add">
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" required="required" id="UserUsername" maxlength="255" name="data[User][username]" >
<!--<p class="help-block">Example block-level help text here.</p> -->
<label id="UserUsername-error" class="error" for="UserUsername"></label>
</div>
<div class="form-group">
<label>Password:</label>
<input type="password" class="form-control" required="required" id="UserPassword" name="data[User][password]">
<label id="UserPassword-error" class="error" for="UserPassword"></label>
</div>
<div class="form-group">
<label>Full Name</label>
<input type="text" class="form-control" required="required" id="UserFullName" maxlength="255" name="data[User][full_name]">
<label id="UserFullName-error" class="error" for="UserFullName"></label>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" required="required" id="UserEmail" maxlength="255" name="data[User][email]">
<label id="UserEmail-error" class="error" for="UserEmail"></label>
</div>
<div class="form-group">
<label>Secondary Email</label>
<input type="email" class="form-control" required="required" id="UserSecEmail" maxlength="255" name="data[User][secondary_email]">
<label id="UserSecEmail-error" class="error" for="UserSecEmail"></label>
</div>
<div class="form-group">
<label>Phone</label>
<input type="text" class="form-control" required="required" id="UserPhone" maxlength="255" name="data[User][phone]">
<label id="UserPhone-error" class="error" for="UserPhone"></label>
</div>
<div class="form-group">
<label>Secondary Phone</label>
<input type="text" class="form-control" required="required" id="UserSecPhone" maxlength="255" name="data[User][secondary_phone]">
<label id="UserSecPhone-error" class="error" for="UserSecPhone"></label>
</div>
<div class="form-group">
<label>Location</label>
<input type="text" class="form-control" required="required" id="UserSecLoc" maxlength="255" name="data[User][location]">
<label id="UserLocation-error" class="error" for="UserLocation"></label>
</div>
<div class="form-group">
<label>Business Name</label>
<input type="text" class="form-control" required="required" id="UserBusinessName" maxlength="255" name="data[User][business_name]">
<label id="UserBusiness-error" class="error" for="UserBusinessName"></label>
</div>
<div class="form-group">
<label for="UserDocument">Document</label>
<select class="form-control" required="required" id="UserDoc" name="data[User][document]">
<option value="">-- Select --</option>
<option value="Passport">Passport</option>
<option value="License Certificate">License Certificate</option>
<option value="Aggrement Copy">Aggrement Copy</option>
</select>
<label id="ExamName-error" class="error" for="UserDocument"> </label>
</div>
<!--<div class="form-group">
<label>File input</label>
<input type="file">
</div> -->
<div class="form-group" id="ShowDoc" style="display:none">
<label>File</label>
<!--<input name="data[Document][docfile]" id="DocumentSubmittedfile" type="file"> -->
<!--<input type="file" name="data[User][docfile]" id="usersFile"/>-->
<?php //echo $this->Form->input('doc_file',array( 'type' => 'file','error' => false,'placeholder'=>'Upload Image')); ?>
<?php echo $this->Form->file('Document.submittedfile'); ?>
</div>
<div class="col-lg-12">
<h4 class="page-header">Point of Contact</h4>
</div>
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" required="required" id="pname" maxlength="255" name="data[User][pname]">
<label id="UserPName-error" class="error" for="UserPName"></label>
</div>
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" required="required" id="pemail" maxlength="255" name="data[User][pemail]">
<label id="UserPEmail-error" class="error" for="UserPEmail"></label>
</div>
<div class="form-group">
<label>Phone</label>
<input type="text" class="form-control" required="required" id="pPhone" maxlength="255" name="data[User][pOfc_phone]">
<label id="UserpPhone-error" class="error" for="UserPEmail"></label>
</div>
<div class="form-group">
<label>Designation</label>
<input type="text" class="form-control" required="required" id="pDesign" maxlength="255" name="data[User][pdesignation]">
<label id="UserpPhone-error" class="error" for="UserpDesign"></label>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">NEXT</button>
</div>
</form>
</div>
</body>
答案 0 :(得分:0)
在您的名字$forecast->document
答案 1 :(得分:0)
您可以使用CakePHP Helper将所需的ID添加到字段,只需在创建时传递其他参数ID并使用该ID进行绑定验证。
PHP
<?php echo $this->Form->input('company_name', array('label'=>false,'div'=>false,'type' => 'text','id'=>'companyname','class' => 'form-control','autocomplete'=>'off','required')); ?>
JS
$("#companyname").rules("add", {
required: true,
minlength:2,
maxlength:50,
messages: {
required:"Please enter Company Name",
}
});