Solved

Summernote and form validation

Posted on 2016-10-13
10
227 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
Need help with this homework problem. 5 58
ModalPopup  question 22 38
compare date to getdate() 8 16
REST call Failing 1 10
Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn how to count occurrences of each item in an array.

756 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