I have a customized Create New Classified form and the input fields that should be required aren&#03

I have created a custom form to create new classifieds/edit classifieds using the do_shortcode to generate the input fields. A typical input field looks like this:

<div class=”editfield”>

<label for=”title”><?php _e( ‘Sale Price’, $this->text_domain ); ?></label>

<?php echo do_shortcode(‘[ct_in id=”_ct_text_4cfeb3eac6f1f” property=”input”]’:wink:; ?>

</div>

The problem is…this should be a required field, and I can submit the form even if it is empty!

Is there something I need to add in the shortcode to make it required?

Thanks!

jc

  • Tom Eagles
    • Syntax Hero

    Just spoke to the dev

    here are his comments

    The line

    jQuery(document).ready( function($) {
    jQuery('#ct_custom_fields_form').closest('form').validate();
    });

    has to be called to initialize the validation. Looks like he modified the template that builds the form for input and comment out the initialization.

  • jasoncarrigan
    • Flash Drive

    @Arnold

    That is helpful – that commented code is automatically generated by the following lines:

    <!–

    <?php if ( isset( $CustomPress_Core ) ) : ?>

    <?php echo do_shortcode(‘[custom_fields_input” style=”editfield”]’:wink:; ?>

    <?php endif; ?>

    –>

    So what I did was copy and paste the validation code from the source in to the update-classifieds.php file

    So…now when I create a post where required fields are missing then I submit, it returns to the my-classifieds page rather than producing the validation error on the create-new form.

    Is there something I am still doing wrong? It should stay on the create-new page and display the validation errors.

  • Arnold
    • El Macho WP

    Probably :slight_smile:

    You’ve got the script outside the form it applies to. It uses jQuery.closest() to find the form it’s in so it’s not validating.

    Also you have two validation statements

    <script type="text/javascript">
    jQuery(document).ready( function($) {
    jQuery('#ct_custom_fields_form').closest('form').validate();
    });
    </script>
    <script type="text/javascript">
    jQuery(document).ready( function($) {
    jQuery('#ct_custom_fields_form').closest('form').validate();
    jQuery('[name="_ct_text_4cfeb3eac6f1f"]').rules('add', { messages: {required: 'Price is a required field', regex: 'Numbers only' } });
    jQuery('[name="_ct_text_4cfeb3eac6f1f"]').rules('add', { required: true, regex: /^[0-9]{1,7}$/ } );
    jQuery('[name="_ct_text_51f6d9a6e2d35"]').rules('add', { messages: {required: 'Year is a required field' } });
    jQuery('[name="_ct_text_51f6d9a6e2d35"]').rules('add', { required: true } );
    jQuery('[name="_ct_text_51f6d9b86a014"]').rules('add', { required: true } );
    jQuery('[name="_ct_text_51f6d9ca57e10"]').rules('add', { messages: {required: 'Model is a required field' } });
    jQuery('[name="_ct_text_51f6d9ca57e10"]').rules('add', { required: true } );
    });
    </script>

    The first one has all it’s apostropes converted to ‘ which is fine in html but not in javascript.

    The target of the jQuery.validate (‘#ct_custom_fields_form’:wink: is commented out and should be a hidden field in the field set in the form.