It's true that validation occurs, then model update occurs.
The text input field follows the same rules.  If you're seeing a model
value, then it's an old one.

It's also true that you cannot use getSubmittedValue and getLocalValue
on components in a dataTable unless you are in the middle of the
processValidators loop.   But there's nothing really magic about this.
 The loop simply does this:

for (int index=0; index < #ofrows; ++index) {
   dataTable.setRowIndex(index);
   [...do your processing here, once for each row...]
}
dataTable.setRowIndex(-1);  // reset the datatable

Bind your dataTable to the backing bean, and perform the iteration
yourself in your validation method.  It's pretty simple.

You can find an example of how to read the value of a component (be it
in the submitted state or in the local state) by looking at the
CompareToValidator code in the sandbox.   In your case, you can simply
assume everything will be in the local state (or that the component
will be marked invalid) because your hidden field with validation will
be validated after all of the things above it.




On 9/25/06, Wally Hartshorn <[EMAIL PROTECTED]> wrote:

re: "validation occurs before model update"

I had a feeling that I would get a reply like that. :-)

However, I don't understand why the text input field that is
OUTSIDE of the table has its UIInput field updated in time for
the validator to examine it, but the text input fields that are
INSIDE the table do not. Why would they be treated differently?
Simply because one is a "DataModel" object while the other is not?
That seems quite counter-intuitive. Both have "binding='#{...}'"
attributes, both are validated at the same time by the same
method. That one would have an updated UIInput element while
the other does not seems... weird.

I've not used an actionListener before. If I do validation AFTER
the validation phase, how do I prevent the action from being
processed? Does the mere presence of UIInput fields that
were marked invalid AFTER the validation phase prevent the
action from being processed?


Andrew Robinson-5 wrote:
>
> That is correct. If you look at the lifecycle of JSF, validation
> occurs before model update. Validation is meant to occur on submitted
> values, not model values. Therefore, you can only use values from the
> request parameters to determine validity during validation.
>
> If you need to validate the model, then you should put code in an
> actionListener (or action) and if invalid, add faces message(s) to the
> faces context and if desired, mark your UIInput components as invalid
> (setValid(false)) using component binding.
>
> You cannot use getSubmittedValue and getLocalValue on components in a
> dataTable unless you are in the middle of the processValidators loop
> of the UIData object. This is because each of those components are
> processed 1 time per row of your data table, and thus the submitted
> value and local value cannot be stored on the component as there are
> many rows, but only one local & submitted member variables for a
> component.
>
> You probably want to validate after the update models phase from your
> problem description.
>
> -Andrew
>
> On 9/25/06, Wally Hartshorn <[EMAIL PROTECTED]> wrote:
>>
>> I'm trying to validate input fields within t:columns backed by a
>> DataModel
>> object. These input fields are validated by a custom validator method
>> attached to a hidden input field (so that I can do things like verify
>> that
>> if something is entered in THIS field, then THAT field is blank).
>>
>> What appears to be happening is that the UIInput fields in the DataModel
>> do
>> not get updated until AFTER the validation phase. This means that the
>> validator can't access the values in the UIInput fields to do the
>> validation.
>>
>> After much mystification and pulling out of hair, I finally made a small
>> webapp to test various combinations -- none of which work properly.
>>
>> Suppose I have a 3-by-3 table of input fields backed by a DataModel, plus
>> an
>> additional input field that is NOT part of the DataModel, plus a hidden
>> field that contains the hook to the validator.
>>
>>  <t:dataTable id="myTable" var="oneRow"
>>      value="#{myBean.tableBody}">
>>
>>      <t:columns id="oneCol" value="#{myBean.tableHeadings}"
>>          var="oneHeading">
>>
>>          <f:facet name="header">
>>              <h:outputText value="#{oneHeading.value}" />
>>          </f:facet>
>>
>>          <h:inputText value="#{myBean.cellValue}"
>>              binding="#{myBean.cellInput}" />
>>
>>      </t:columns>
>>
>>  </t:dataTable>
>>
>>  <h:inputText binding="#{myBean.someNameInput}" />
>>
>>  <t:commandButton value="Submit" action="submit" />
>>
>>  <h:inputHidden value="needed" required="true"
>>      validator="#{myBean.validateAllCells}" />
>>
>> The method myBean.getCellValue() returns the object entered in the field,
>> while myBean.getCellInput() returns the UIInput component that is bound
>> to
>> the field.
>>
>> When myBean.validateAllCells() runs, it loops through the rows and
>> columns
>> of the DataModel, accessing the UIInput fields and doing its thing. After
>> that, it accesses the UIInput field for the stand-alone input field.
>>
>> When it does this, I've found that the UIInput fields that were bound to
>> input fields within t:columns do NOT have the new values of the fields
>> that
>> were in the 3-by-3 table, but the UIInput field that is bound to the
>> standalone input field (which is outside of the t:dataTable) DOES have
>> its
>> new value. I determined this by having the validator consider all of the
>> fields to be in error, displaying their UIInput values in the error
>> messages. I've tried accessing UIInput.getLocalValue(),
>> UIInput.getSubmittedValue, and UIInput.getValue; none provide the
>> validator
>> with the value that was just submitted.
>>
>> AFTER the validator has run, the UIInput fields in the DataModel object
>> are
>> updated, so if I click the Submit button again, then it sees those
>> values.
>> In other words, the validator always sees what was entered on the
>> PREVIOUS
>> button click.
>>
>> If I attach a validator method directly to the input fields and access
>> the
>> value of the object passed to it, the validator sees the proper value,
>> but
>> that doesn't allow me to do the type of validation I need to do (where
>> one
>> field's legal values are dependent upon another field's values).
>>
>> This has been EXTREMELY confusing and frustrating! I'm using Java 5,
>> MyFaces
>> 1.1.3, and Tomahawk 1.1.3. Has anyone found a way to make validation with
>> t:columns and DataModel work properly? Is this an unreported bug in
>> Tomahawk
>> or am I just missing something?
>> --
>> View this message in context:
>> 
http://www.nabble.com/Validation-with-DataModel-and-t%3Acolumns-tf2332358.html#a6488960
>> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>>
>>
>
>

--
View this message in context: 
http://www.nabble.com/Validation-with-DataModel-and-t%3Acolumns-tf2332358.html#a6493834
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Reply via email to