jquery 모달 뷰에서 내 양식의 유효성을 검사하기 위해 client_side_validations gem을 얻으려고했으나 유효성 검사가 발생하지 않았습니다 (적어도 오류가 나타나지 않는 한 인라인).Client_side_validations jquery 모달 뷰에서 formtastic과 gem이 작동하지 않습니다.
저는 formtastic gem도 사용하고 있습니다. 따라서 formtastic 지원을 위해 client_side_validations 및 client_side_validations-formtastic gem을 설치했습니다.
참고 :
응용 프로그램/뷰/홈/index.html을 : 나는 내 응용 프로그램/뷰/레이아웃/application.html.erb에
여기 내 코드는 자바 스크립트 rails.validations을 포함한다. ERB :
<script type="text/javascript">
$(document).on("click", "btn-signup", function trans() {
Modal.prototype.hideWithTransition = function(){
$("#signup").fadeIn();
};
});
</script>
<script type="text/javascript">
$('#new_user').enableClientSideValidations();
$('#new_user').on('shown', function() {
$(ClientSideValidations.selectors.forms).validate();
});
</script>
<div class="row">
<div class="twelvecol last">
<div id="vidplaceholder">
<div id="landing-action">
<p></p>
<fieldset>
<!-- The sign up modal view -->
<div id="signup" class="modal hide fade in" style="display: none;">
<% @user = User.new %>
<%= semantic_form_for @user, :remote => true, :validate => true do |f| %>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Test header </h3>
</div>
<div class="modal-body">
<div class="field">
<%= f.input :email %>
</div>
<div class="field">
<%= f.input :website %>
</div>
<div class="field">
<%= f.input :projects %>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
<% end %>
<!-- The modal view is triggered here -->
<a data-toggle="modal" href="#signup" class="btn btn-primary
btn-large btn-signup">Sign up</a>
</fieldset>
<p></p>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$("#new_user").on("submit", function hide_form() {
$("#signup").modal('hide');
});
</script>
응용 프로그램/모델/user.erb :
class User < ActiveRecord::Base
attr_accessible :email, :website, :projects
before_save { |user| user.email = email.downcase }
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true
validates :email, format: { with: VALID_EMAIL_REGEX }
validates :email, uniqueness: { case_sensitive: false }
validates :website, :format => URI::regexp(%w(http https)), :allow_blank => true
end
설정/초기화/client_side_validations.rb은 :
# ClientSideValidations Initializer
require 'client_side_validations/formtastic' if defined?(::Formtastic)
# Uncomment to disable uniqueness validator, possible security issue
# ClientSideValidations::Config.disabled_validators = [:uniqueness]
# Uncomment the following block if you want each input field to have the validation messages attached.
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
unless html_tag =~ /^<label/
%{<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe
else
%{<div class="field_with_errors">#{html_tag}</div>}.html_safe
end
end
어떤 도움이나 제안은 많이 주시면 감사하겠습니다!
아니, 차이 – Mason