Solved

Summernote and form validation

Posted on 2016-10-13
10
285 Views
Last Modified: 2016-11-24
I am trying to add code to validate summernote and other fields in a form. I added code to validate summernote field and it seems to work fine, but then the rest of the validation script doesn't work. below is my code. Not sure how to clean it up so that all fields are validated.

  <script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$(document).ready(function() {
	
	$('#form1')
        .formValidation({
            framework: 'bootstrap',
            excluded: [':disabled'],
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                title: {
                    validators: {
                        notEmpty: {
                            message: 'The title is required and cannot be empty'
                        }
                    }
                },
                summernote: {
                    validators: {
                        callback: {
                            message: 'The content is required and cannot be empty',
                            callback: function(value, validator, $field) {
                                var code = $('[name="summernote"]').summernote('code');
                                // <p><br></p> is code generated by Summernote for empty content
                                return (code !== '' && code !== '<p><br></p>');
                            }
                        }
                    }
                }
            }
        })
        .find('[name="summernote"]')
            .summernote({
                height: 400
            })
            .on('summernote.change', function(customEvent, contents, $editable) {
                // Revalidate the content when its value is changed by Summernote
                $('#form1').formValidation('revalidateField', 'summernote');
            })
            .end();


    $('#form1').formValidation({
        framework: 'bootstrap',
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
           			
						subject: {
                validators: {
                    notEmpty: {
                        message: 'Please enter a short description'
                    },
                }
            },
			getForm: {
                validators: {
                    notEmpty: {
                        message: 'Please make a selection'
                    },
                }
            },
			
						bug_type: {
                validators: {
                    notEmpty: {
                        message: 'Please make a selection'
                    },
                }
            },
			
				browser: {
                validators: {
                    notEmpty: {
                        message: 'Please select a browser'
                    },
                }
            },
			
		
					
  }
		
    });
});
});//]]>  

</script> 

Open in new window

0
Comment
Question by:amucinobluedot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 25

Expert Comment

by:lenamtl
ID: 41843611
Hi, check with chrome dev tool (right click / inspect) to see if you have any JS  error in the console.

Also add server side validation.
0
 

Author Comment

by:amucinobluedot
ID: 41847607
I actually don't get any errors because the validation for the summer note works, but the rest of the validation doesn't, it does not validate those other fields

Here is the code I put together for the form:

The first part of the validation works fine, the summer note validation doesn't work at all, no errors though. Simply as if there was no validation for that field.

 <script>
$(document).ready(function(){

$('.summernote').summernote( { focus: true } );

       });
       			
    </script>
   <script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$(document).ready(function() {
	



    $('#form1').formValidation({
        framework: 'bootstrap',
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
           			
						subject: {
                validators: {
                    notEmpty: {
                        message: 'Please enter a short description'
                    },
                }
            },
			getForm: {
                validators: {
                    notEmpty: {
                        message: 'Please make a selection'
                    },
                }
            },
			
						bug_type: {
                validators: {
                    notEmpty: {
                        message: 'Please make a selection'
                    },
                }
            },
			
				browser: {
                validators: {
                    notEmpty: {
                        message: 'Please select a browser'
                    },
                }
            },
			
		
					
  }
		
    });
});
});//]]>  

</script> 



  <script>
    $(function() { // RUN ON PAGE LOAD
      // HIDE ALL
      $('[class^="getForm"]').hide(); //NOTE WILD CARD

      $('select#getForm').on('change', function() {
          $('[class^="getForm"]').hide(); //HIDE ALL
          // NOW SHOW ONLY THE SELECTED
          var formName = $(this).val(); // VALUE OF SELECTED
          $('.' + formName).show();  // SHOW IT

      });

  });
  </script>
  
  <script>
    $(".chosen-select").chosen({
    disable_search_threshold: 10,
    no_results_text: "Oops, the form is not in the list!",
    width: "95%",
	inherit_select_classes: "false"
	
  });
  </script>
  
<script>
$(document).ready(function() {
    $('#form')
        .formValidation({
            framework: 'bootstrap',
            excluded: [':disabled'],
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {

                summernote: {
                    validators: {
                        callback: {
                            message: 'The content is required and cannot be empty',
                            callback: function(value, validator, $field) {
                                var code = $('[name="summernote"]').summernote('code');
                                // <p><br></p> is code generated by Summernote for empty content
                                return (code !== '' && code !== '<p><br></p>');
                            }
                        }
                    }
                }
            }
        })
        .find('[name="summernote"]')
            .summernote({
                height: 400
            })
            .on('summernote.change', function(customEvent, contents, $editable) {
                // Revalidate the content when its value is changed by Summernote
                $('#summernoteForm').formValidation('revalidateField', 'summernote');
            })
            .end();
});
</script>

Open in new window


Any ideas ?
0
 
LVL 25

Assisted Solution

by:lenamtl
lenamtl earned 500 total points
ID: 41848766
For the part of your js that start like this
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$(document).ready(function() {

Open in new window


have you tried to use only one at the time
$(document).ready(function() {
or
$(window).load(function(){
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

Author Comment

by:amucinobluedot
ID: 41849964
If I delete either I get syntax errors. Also, that scripts works fine, is the summer note script that doesn't
0
 
LVL 25

Expert Comment

by:lenamtl
ID: 41849993
Why are you using two set of validation? try to regroup into one set.
Do you have a live link or codepen so we can test it?
0
 

Author Comment

by:amucinobluedot
ID: 41850204
I tried to put it in one set of validation but I was unable to, if you can help do this that would be great :)
I will try and setup a test site for you to see.
0
 
LVL 25

Assisted Solution

by:lenamtl
lenamtl earned 500 total points
ID: 41850727
Could you try this:
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
	$(document).ready(function() {
	
	$('#form1').formValidation({
            framework: 'bootstrap',
            excluded: [':disabled'],
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                title: {
                    validators: {
                        notEmpty: {
                            message: 'The title is required and cannot be empty'
                        }
                    }
                },
                summernote: {
                    validators: {
                        callback: {
                            message: 'The content is required and cannot be empty',
                            callback: function(value, validator, $field) {
                                var code = $('[name="summernote"]').summernote('code');
                                // <p><br></p> is code generated by Summernote for empty content
                                return (code !== '' && code !== '<p><br></p>');
                            }
                        }
                    }
                }
				
				subject: {
					validators: {
						notEmpty: {
							message: 'Please enter a short description'
						},
					}
				},
				
				getForm: {
					validators: {
						notEmpty: {
							message: 'Please make a selection'
						},
					}
				},
			
				bug_type: {
					validators: {
						notEmpty: {
							message: 'Please make a selection'
						},
					}
				},
			
				browser: {
					validators: {
						notEmpty: {
							message: 'Please select a browser'
						},
					}
				}

            }
        })
        .find('[name="summernote"]')
            .summernote({
                height: 400
            })
            .on('summernote.change', function(customEvent, contents, $editable) {
                // Revalidate the content when its value is changed by Summernote
                $('#form1').formValidation('revalidateField', 'summernote');
            })
            .end();

});
});//]]>  

</script> 

Open in new window

or this
 <script type='text/javascript'>//<![CDATA[ 

	$(document).ready(function() {
	
	$('#form1').formValidation({
            framework: 'bootstrap',
            excluded: [':disabled'],
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                title: {
                    validators: {
                        notEmpty: {
                            message: 'The title is required and cannot be empty'
                        }
                    }
                },
                summernote: {
                    validators: {
                        callback: {
                            message: 'The content is required and cannot be empty',
                            callback: function(value, validator, $field) {
                                var code = $('[name="summernote"]').summernote('code');
                                // <p><br></p> is code generated by Summernote for empty content
                                return (code !== '' && code !== '<p><br></p>');
                            }
                        }
                    }
                }
				
				subject: {
					validators: {
						notEmpty: {
							message: 'Please enter a short description'
						},
					}
				},
				
				getForm: {
					validators: {
						notEmpty: {
							message: 'Please make a selection'
						},
					}
				},
			
				bug_type: {
					validators: {
						notEmpty: {
							message: 'Please make a selection'
						},
					}
				},
			
				browser: {
					validators: {
						notEmpty: {
							message: 'Please select a browser'
						},
					}
				}

            }
        })
        .find('[name="summernote"]')
            .summernote({
                height: 400
            })
            .on('summernote.change', function(customEvent, contents, $editable) {
                // Revalidate the content when its value is changed by Summernote
                $('#form1').formValidation('revalidateField', 'summernote');
            })
            .end();


});//]]>  

</script> 

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 41850864
Neither worked. the summer note field stopped working and  also it only validated the summer note and none of the other fields  :$
0
 
LVL 25

Accepted Solution

by:
lenamtl earned 500 total points
ID: 41850879
Please set up a test site so I can check the whole code including the form
The code I gave you is based on the code you provide in your question.

The latest post  (2016-10-17 at 22:12:06) you added extra code like this don't remove it

 <script>
$(document).ready(function(){

$('.summernote').summernote( { focus: true } );

       });
       			
    </script>

Open in new window

0
 

Author Closing Comment

by:amucinobluedot
ID: 41900935
Summernote was replaced, for the time being this won't be necessary. Thanks for the help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn how to dynamically set the form action using jQuery.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question