Solved

Summernote and form validation

Posted on 2016-10-13
10
46 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
  • 5
  • 5
10 Comments
 
LVL 24

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 24

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
 

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 24

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 24

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 24

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article discusses how to create an extensible mechanism for linked drop downs.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now