0

I am learning JSF and starting to write a custom component in JSF 1.2. Can someone please shed light on how JSF 'required' attribute works under the hood? I looked at the html generated and it doesnot show anything with required.

I am writing a JSF component with multiple input texts but only some of them will be required ( eg. writing 2 input text Zipcode component where the last input text is ALWAYS optional whereas the first input text is to be controlled by required attribute defined in xhtml.

How do I handle such selective 'required' behavior?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
phewataal
  • 1,107
  • 4
  • 12
  • 23

1 Answers1

1

The required attribute of components is stored server side ONLY, the client has no idea of which fields are required by default. When you post a form with empty required fields, JSF will compare the fields to its server side model and do validation from there, rendering error messages for empty required fields. You can witness this in the JSF lifecycle (which is processed server side only). If required fields are empty, the lifecycle will halt at step 3, and skip the model update.

  1. Restore view
  2. Apply request values; process events
  3. Process validations; process events
  4. Update model values; process events
  5. Invoke application; process events
  6. Render response

Regarding your second question, if the number of input texts is static, you can simply set some of them to required="true", and some to required="#{cc.attrs.required}".

teobais
  • 2,820
  • 1
  • 24
  • 36
Rasmus Franke
  • 4,434
  • 8
  • 45
  • 62