2

I need some help with required attribute on PHP version. Required field works fine, but problem is with oninvalid element to show custom text like in HTML form.

This is HTML version:

<select required="" oninvalid="this.setCustomValidity('Custom TXT!')">

This is my PHP version of select drop-down box:

$options               = $args['options'];
    $product               = $args['product'];
    $attribute             = $args['attribute'];
    $name                  = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
    $id                    = $args['id'] ? $args['id'] : sanitize_title( $attribute );
    $class                 = $args['class'];
    $show_option_none      = $args['show_option_none'] ? true : false;
    $show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' ); // We'll do our best to hide the placeholder, but we'll need to show something when resetting options.

    if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
        $attributes = $product->get_variation_attributes();
        $options    = $attributes[ $attribute ];
    }

    $html = '<select required="" oninvalid="this.setCustomValidity('Custom TXT!')" id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '"' . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
    $html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';

Can someone help me with this line to make custom text show, now it gives only error. Thank you!

$html = '<select required="" oninvalid="this.setCustomValidity('Custom TXT!')"

http://prntscr.com/earmll

zte813
  • 21
  • 3
  • Possible duplicate of [HTML5: Why does my "oninvalid" attribute let the pattern fail?](http://stackoverflow.com/questions/16867407/html5-why-does-my-oninvalid-attribute-let-the-pattern-fail) – Asons Feb 19 '17 at 18:32
  • nope... I need in PHP code this element... – zte813 Feb 19 '17 at 18:46
  • Well, you have to convert it to your PHP string – Asons Feb 19 '17 at 18:49
  • @LGSon maby some help? :)) – zte813 Feb 19 '17 at 18:53
  • If you know PHP syntax this should be simple .. I don't, I'm a .NET developer – Asons Feb 19 '17 at 18:56
  • You will have to escape the inner single quotation marks if that's exactly what you want in your code. – Firearrow5235 Feb 19 '17 at 19:11
  • @Firearrow5235 Parse error: syntax error, unexpected... I tried this many times... – zte813 Feb 19 '17 at 19:17
  • Check and make sure every single and double quotation mark has a match. In your editor you should be able to stick your cursor next to the mark and it's match will be highlighted. Syntax errors usually have nothing to do with the actual code, just the way it's written. – Firearrow5235 Feb 19 '17 at 19:24
  • @Firearrow5235 but maby it is impossible to use HTML event on php line? – zte813 Feb 19 '17 at 19:26
  • I don't see why not. It's just a text string that PHP as adding to the HTML document before serving it to the user. – Firearrow5235 Feb 19 '17 at 19:28
  • @Firearrow5235 nothing... Tried every possible move.. Maby someone else could help? – zte813 Feb 19 '17 at 20:02

1 Answers1

0

Well... This is lesson for me to use normal IDE not np++...

oninvalid="this.setCustomValidity(\'Custom TXT!\')"

Closed.

zte813
  • 21
  • 3