Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

  • Cross field validation server side - not receiving all fields in record

    Problem:

    I am trying to validate based on another field (valid_to) from datasource, but the record in the server side validator only contains some of the fields from datasource (e.g. of course orgno, and org_name, type, not_effectuated etc.), but the valid_to field is not in the record. I have tried to use dependentFields to include this field, but it is still not in record. I have also tries to use applyWhen to try not to fire the validation based on the value in valid_to, but with no success. Is there some way I can reach this field in the server side validator?

    SmartClient Version: v9.1p_2018-05-03/PowerEdition Deployment (built 2018-05-03)

    Part of the datasource file:
    Code:
    <fields>
             <field name="id" primaryKey="true" type="sequence" detail="true"/>
             <field name="orgno" customSelectExpression=" case
                    when count(lor_beneficiary_approvals.organization_id) = 0 then null
                    when count(lor_beneficiary_approvals.organization_id) = 1 then o2.orgno
                    else null
                    end">
                 <validators>
                    <validator type="serverCustom">
                         <serverObject lookupStyle="spring" bean="orgnoValidator"/>
                         <errorMessage>$error</errorMessage>
                     </validator>
                 </validators>
             </field>
             <field name="org_name" customSelectExpression=" case
                    when count(lor_beneficiary_approvals.organization_id) = 0 then null
                    when count(lor_beneficiary_approvals.organization_id) = 1 then org_er.name
                    else null
                    end"/>
            <field name="type" type="integer" hidden="true"/>
            <field name="not_effectuated" title="Ikke-effektuert" type="boolean" detail="true">
                <validators>
                    <validator type="serverCustom">
                        <serverObject lookupStyle="spring" bean="financialStatementOnLotteryValidator"/>
                        <errorMessage>$error</errorMessage>
                    </validator>
                </validators>
            </field>
            <field name="valid_from" title="Gyldig fra" type="date" required="true" />
            <field name="valid_to" title="Gyldig til" type="date" required="true" />
            <field name="venue_id" type="integer" hidden="true" title="Id til lokale"/>
            <field name="venue_name" type="text" tableName="lor_venues" title="Lokale" detail="false" nativeName="name"/>        
        </fields>
    The server side validator:
    Code:
    package no.lottstift.lotteri.server;
    
    import java.rmi.RemoteException;
    import java.util.Date;
    import java.util.Map;
    
    import javax.xml.bind.JAXBException;
    
    import com.isomorphic.datasource.DSRequest;
    import com.isomorphic.util.DataTools;
    import no.lottstift.server.organization.OrganizationDao;
    import no.lottstift.shared.datasource.Fields;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.isomorphic.datasource.Validator;
    
    import static no.lottstift.shared.datasource.Fields.VALID_TO;
    
    /**
     * Validates that the provided orgno in {@link Fields#ORGNO} is set and maps to a organzation in ER.
     * If {@link Fields#DELETED_FROM} is set, the validation is ignored, since this is a delete.
     * The validation is also ignored if {@link Fields#VALID_TO} is NULL or before today.
     * @author rwo
     *
     */
    public class ValidateOrgno {
        private OrganizationDao mOrganizationDao = null;
        public Logger mLog = LoggerFactory.getLogger(this.getClass());
    
    
        @Autowired
        public void setOrganizationDao(OrganizationDao dao) {
            mOrganizationDao = dao;
        }
    
        public boolean condition(Object value, Validator validator, String fieldName, Map record) throws Exception {
            if(record.get(Fields.DELETED_FROM) != null) {
                mLog.debug("Ignoring validation, since delete date is set");
                return true;
            }
           Date validTo = getValidTo(record);
            if(validTo == null || validTo.before(new Date())) {
                mLog.debug("Ignoring validation since valid to is null or before today");
                return true;
            }
            String orgno = record.get(fieldName).toString();
            return validate(validator,orgno);
        }
    
       protected Date getValidTo(Map record) throws Exception {
            return getDate(record, VALID_TO);
        }
    
        private boolean validate(Validator validator, String orgno) throws RemoteException, JAXBException {
            boolean exists = mOrganizationDao.existsInEnhetsregisteret(orgno);
            if(exists) {
                return true;
            }
            setErrorMessage(validator, orgno+" er ikke en enhet i enhetsregisteret.");
            return false;
        }
    
        private void setErrorMessage(Validator validator, String message) {
            validator.addErrorMessageVariable("error", message);
        }
    
        private Date getDate(Map record, String fieldName) {
            try {
                return (Date) record.get(fieldName);
            } catch(Exception e) {
                //OK. Could be validation error on date.
            }
            return null;
        }
    }
    Try with dependent fields:
    Code:
                     <validator type="serverCustom">
                         <serverObject lookupStyle="spring" bean="orgnoValidator"/>
                         <errorMessage>$error</errorMessage>
                        <dependentFields>
                            <value>valid_to</value>
                        </dependentFields>
                     </validator>
    Try with applyWhen:
    Code:
                     <validator type="serverCustom">
                         <serverObject lookupStyle="spring" bean="orgnoValidator"/>
                         <errorMessage>$error</errorMessage>
                        <applyWhen fieldName="valid_to" operator="equals" value="new Date();"/>
                     </validator>
    Last edited by anderfim; 8th May 2018, 00:23. Reason: Typo

  • #2
    Hi anderfim,

    see here and the last post in the same thread (most likely the solution) and also the docs for Validator.dependentFields. I did not use the latter, but perhaps this is a more declarative approach to your problem than validateRecord().

    Best regards
    Blama

    Comment


    • #3
      Thank you for the fast reply Blama. I am having a look at this right now.

      Comment


      • #4
        Hi Blama,

        I need help in server side validation of a field. I am getting following error on UI:
        Validator DMI invocation threw exception: java.lang.NullPointerException with error: null

        My ds.xml file :

        <DataSource ID="Orders"
        schemaBean="com.aa.shared.OrderDto"
        beanFactory="org.springframework.beans.factory.BeanFactory">
        <fields>
        <field name="orderId" type="long" align="left" primaryKey="true"
        hidden="true" />

        <field name="poNumber" type="text" required="true">
        <validators>
        <validator type="serverCustom">
        <serverObject lookupStyle="spring" bean="orderService"/>
        <errorMessage>PO number should be unique</errorMessage>
        </validator>
        </validators>
        </field>
        </fields>

        </DataSource>



        Server side class:

        @Service("orderService")
        @Transactional(readOnly = false)
        public class OrderServiceImpl implements OrderService {


        @Autowired
        private OrderDao orderDao;


        public boolean condition(final Object value, final Validator validator, final String fieldName, final Map record, final DataSource ds) throws Exception {
        boolean isValid = true;
        if (null != value) {
        final String poNumber = value.toString();
        Order order = orderDao.checkIfPoNUmberExists(poNumber);
        if (null != order) {
        isValid = false;
        }
        }
        return isValid;
        }
        }



        Here, orderDao remains null.

        Comment


        • #5
          Hi Diksha,

          sorry, I don't know anything about Spring usage here, as I'm using SmartGWT SQLDataSource.

          Best regards
          Blama

          Comment

          Working...
          X