client side validation being ignored

client side validation being ignored

ehardingeharding Posts: 13Questions: 6Answers: 0

I have a form with a select field, I want to make another text field, required if the user selects Other from the list.

here are the field definitions

{
                label: "Subject: *",
                name: "alice_history.history_subject",
                attr: {
                    tabIndex: 2
                  },
                type: "select",
                options: [
                     { label: "New Business Pipeline", value: "New Business Pipeline" },
                     { label: "Current Account Review", value: "Current Account Review" },
                     { label: "Key Account Meeting", value: "Key Account Meeting" },
                     { label: "Planning Meeting", value: "Planning Meeting" },
                     { label: "Current Results vs. Plan", value: "Current Results vs. Plan" },
                     { label: "Planning meetings", value: "Planning meetings" },
                     { label: "Transition agency", value: "Transition agency" },
                     { label: "New appointment", value: "New appointment" },
                     { label: "Profit sharing", value: "Profit sharing" },
                     { label: "Wingspan", value: "Wingspan" },
                     { label: "2018 Business Plan", value: "2018 Business Plan" },
                     { label: "Other", value: "Other" }  
                ]
            },{
                label: "Other: *",
                name: "alice_history.history_title",
                attr: {
                    tabIndex: 4
                  },
            },

here is the validation code

   heditor.dependent( 'alice_history.history_subject', function ( val ) {
          return val === 'Other' ?
              { show: ['alice_history.history_title'] } :
              { hide: ['alice_history.history_title'] };
      } );


 heditor.on( 'preSubmit', function ( e, o, action ) {
        if ( action !== 'remove' ) {
            var subject = this.field( 'alice_history.history_subject' );
            var title = this.field( 'alice_history.history_title' );
 
            // Only validate user input values - different values indicate that
            // the end user has not entered a value
            if ( subject.val === 'Other' ) {
                if ( ! title.val() ) {
                    title.val.error( 'Cannot be blank of Other Selected' );
                }
          
            }
 
            // ... additional validation rules
 
            // If any error was reported, cancel the submission so it can be corrected
            if ( this.inError() ) {
                return false;
            }
        }
    
     } );

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    Answer ✓

    if ( subject.val === 'Other' ) {

    This is never going to be true as subject.val is a function. Try:

     if ( subject.val() === 'Other' ) {
    

    Likewise on line 17 it should be title.error( ... ).

    Allan

  • ehardingeharding Posts: 13Questions: 6Answers: 0

    thanks!

This discussion has been closed.