How to check if a particular row has a validation error to validate multiple rows in Spring using BindingResult

Current logic checks for BindingResult errors and the mapping of data and errors in jsp. The necessary logic is checking errors for each line and displaying only those lines containing verification errors, and updating lines that do not have verification errors. @Autowired private incident IncidentExtractStgServiceExtractStgService;

@RequestMapping(value = "/validatingIncidentList", method = RequestMethod.POST)
public String ValidateIncidentList( @Valid @ModelAttribute("incidentsForm") IncidentsForm incidentsForm,
        BindingResult bindingResult,RedirectAttributes redirectAttributes) {
    if (bindingResult.hasErrors()) {


        for(ObjectError error: bindingResult.getAllErrors()){

            System.out.println(error);
        }

        redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.incidentsForm", bindingResult);
        redirectAttributes.addFlashAttribute("incidentsForm", incidentsForm);

        return "redirect:/validateIncidentList";
    }
    else
    {
        for(IncidentExtractStg ie : incidentsForm.getIncidents()) {

            ie.setValidated(1);
            incidentExtractStgService.update(ie);

            System.out.println(ie.getNumber()+"     "+ie.getWaitTime());
        }


    return  "redirect:/validateIncidentList";

    }

The code snippet below will check if the model contains the "incidetsForm" attribute if it is sent to example.jsp, which in turn will display data and validation errors.

@RequestMapping(value = "/validateIncidentList", method = RequestMethod.GET)
 public String incidentList(Model model) {
    if (!model.containsAttribute("incidentsForm")) {
            List<IncidentExtractStg> incidents = incidentExtractStgDao.validateList();
            incidentsForm.setIncidents(incidents);
            model.addAttribute("incidentsForm", incidentsForm);
            return "example";
    }

     model.addAttribute("errormessage","Please Check the Validation Errors column for Errors");
     return "example";
}

Example code snippet example.jsp

<c:forEach var="ie" items="${incidentsForm.incidents}" varStatus="status">
             <tr>
                  <td><form:input path="incidents[${status.index}].id" value="${ie.id}" readonly ="true"/></td>
                 <td><form:errors path="incidents[${status.index}].id" cssClass="error" /></td> 

                <td><form:input path="incidents[${status.index}].number" value="${ie.number}"/></td>
                <td><form:errors path="incidents[${status.index}].number" cssClass="error" /></td> 
            </tr>

IncidentsForm.java:

import java.util.List;
import javax.validation.Valid;

import com.infosys.sla.model.IncidentExtractStg;

public class IncidentsForm {

@Valid
private List<IncidentExtractStg> incidents;



public List<IncidentExtractStg> getIncidents() {
    return incidents;
}


public void setIncidents(List<IncidentExtractStg> incidents) {

    this.incidents = incidents;
}
}

IncidentExtractStg.java script

@Entity
@Table(name="incident_extract_stg")
public class IncidentExtractStg {

@Id
@Column(name="ies_id")
private int id;

@NotBlank(message="number cannot be empty")
@Pattern(regexp="[A-Za-z0-9]*",message="number can contain only alphabets and numbers")
@Column(name="ies_number")
private String number;
+4
2

, , , .

BindingResult . ( , ).

, jsp , " [$ {status.index}]. id".  - i,  - j - BindingResult.

BeanPropertyBindingResult result2 = new BeanPropertyBindingResult(incidentsForm, bindingResult.getObjectName();

    List<IncidentExtractStg> incidents= new ArrayList<IncidentExtractStg>();
    int i=0;// to get the row count
    int j=0;// to set the index 
    for(IncidentExtractStg ies : incidentsForm.getIncidents()) 
    {    
            int count=0;
            Field[] declaredFields = IncidentExtractStg.class.getDeclaredFields();
            for (Field field : declaredFields) 
            {
                if (bindingResult.hasFieldErrors("incidents["+i+"]."+field.getName()))
                {
                  for (FieldError error: bindingResult.getFieldErrors("incidents["+i+"]."+field.getName()))
                    {
                     result2.addError(new FieldError(error.getObjectName(), "incidents["+j+"]."+field.getName(), error.getRejectedValue(), error.isBindingFailure(), error.getCodes(), error.getArguments(), error.getDefaultMessage()));   
                    }
                 count++;
                }
            }

            if(count>0)
            {
                 j++;
                 incidents.add(ies);    
            }
            else 
            {
                ies.setValidated(1);
                incidentExtractStgService.update(ies);  
            }

        i++;
    }

    i=0;
    j=0;

    if (bindingResult.hasErrors()) {

        incidentsForm.setIncidents(incidents);
        System.out.println("error block");

        for (FieldError error: result2.getFieldErrors()) {
           System.out.println("field errors are  "+error.getField());
           System.out.println("field errors are  "+error);
        }

        redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.incidentsForm", result2);
        redirectAttributes.addFlashAttribute("incidentsForm", incidentsForm);
        return "redirect:/validateIncidentList";
    }       

: 30, [30].number . = 30 count > 0 j = 0.

, [30] 0 , ​​ 0. J result2, bindresult, 30, , 0.

IncidentsForm result2 ​​ jsp, , .

0

, , . , IncidentService IncidentServiceImpl, . , , , .

, ? " , , "

:

public void handleErrors(IncidentsForm incidentsForm, BindingResult bindingResult){ 

    List<String> fieldsInErrorState = new ArrayList<String>(10);

    if (bindingResult.hasErrors()) { //
        Map<String, Object> bindingModel = bindingResult.getModel();

        for (Map.Entry<String, Object> entry : bindingModel.entrySet()) {
            String key = entry.getKey();
            //Object value = entry.getValue(); you don't need to parse that unless you want specific domain model handlers to run

            //you need to store the key as a form field which is in error state
            fieldsInErrorState.add(key);

            //you already have all the stuff to parse and display errors in your JSP
            //thanksfully to bindingResult and JSTL tags.
        }

        ContactMessageForm cmForm2 = new ContactMessageForm();
        // get the list of the fields inside your form
        Field[] declaredFields = ContactMessageForm.class.getDeclaredFields();
        for (Field field : declaredFields) {
            if (!fieldsInErrorState.contains(field.getName())) {
                if (field.getName().equalsIgnoreCase("firstname")) {
                    cmForm2.setFirstname(contactMessageForm.getFirstname());
                }
                if (field.getName().equalsIgnoreCase("lastname")) {
                    cmForm2.setLastname(contactMessageForm.getLastname());
                }

                //etc for each properties of your form object.
            }

            // then store your dbmodel object
            // BUT i think you must be carefull to your data integrity... It is maybe not safe to save an object like that with bypassing some stuff... 
            // Your form was built like that maybe for a good reason looking at your objects graph.
            // If your form is too big, then split it in small parts, it will be much easy to handle, to update, and to work with daily.
        }

    }


}

, , throws IntrospectionException , .

!

+1

Source: https://habr.com/ru/post/1652693/


All Articles