Symfony 4 how to implement Doctrine XML ORM mapping - doctrine

Symfony 4 document is unclear about how to use XML orm mapping instead of annotations. It's rather frustrating to see no details for such important part in official documentation.

Imagine YourDomain\Entity\Customer domain object:
<?php declare(strict_types=1);
namespace YourDomain\Entity;
class Customer
{
private $id;
private $email;
private $password;
public function __construct(string $email)
{
$this->setEmail($email);
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): string
{
return $this->email;
}
public function setEmail(string $email): void
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException('Not a valid e-mail address');
}
$this->email = $email;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(?string $password): void
{
$this->password = $password;
}
}
Define your own mapping first:
orm:
mappings:
YourDomain\Entity:
is_bundle: false
type: xml
// this is the location where xml files are located, mutatis mutandis
dir: '%kernel.project_dir%/../src/Infrastructure/ORM/Mapping'
prefix: 'YourDomain\Entity'
alias: YourDomain
File name has to match the pattern [class_name].orm.xml, in your case Customer.orm.xml. If you have sub-namespaces inside, eg. value object YourDomain\Entity\ValueObject\Email, the file has to be named ValueObject.Email.orm.xml.
Example mapping:
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">
<entity name="YourDomain\Entity\Customer" table="customer">
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="email" type="email" unique="true"/>
<field name="password" length="72"/>
</entity>
</doctrine-mapping>
Good luck.
Version 2.6 XML mapping reference

Related

Struts 2 xml is not working. The Validation is not happenning

This is my controller Login.java
import com.opensymphony.xwork2.ActionSupport;
public class Login extends ActionSupport {
private String userName;
private String password;
public Login() {
}
public String execute() {
return SUCCESS;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
This is my Login-Validation.xml
<?xml version="1.0"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="userName">
<field-validator type="requiredstring">
<message>User Name is required.</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message key="password.required" />
</field-validator>
</field>
</validators>
This is my struts.xml
<action name="NewLogin">
<result>login.jsp</result>
</action>
<action name="LoginAction" class="com.strut.Login">
<result name="success">success.jsp</result>
<result name="input">fail.jsp</result>
</action>
I am using struts2-core 2.5.16 as the maven plugin.
The issue is the controller is not considering my validation.xml. It directly passes to the execute method and goes to the success page.
I have placed the controller and validation xml within the same package.
Could someone help me with it? Thanks in advance.
Your file that named "Login-Validation.xml" is error.
Solution:
1. The validation xml file should be named the "Login-validation.xml".
2. The <result name="input" >fail.jsp</result> had been changed to <result name="input" >login.jsp</result>. This will be better and logical.

Inifinite loop during model binding when using [ModelBinder] attribute

I initially posted this issue to GitHub here: https://github.com/aspnet/Mvc/issues/8723
There's a GitHub repository with a repro of the problem here:
https://github.com/Costo/aspnetcore-binding-bug
I'm using ASP.NET Core 2.2 Preview 3.
When using a custom model binder (with the [ModelBinder] attribute) on properties of an array of "child" models, the model binding phase of the request goes into an infinite loop. See this screenshot:
The custom model binder works well if used on top level model properties, but I'd like to understand why it doesn't work when used in an array of child models. Any help with this would be appreciated.
Thank you !
Here's the code of the model, controller, view and custom binder:
The Model:
public class TestModel
{
public TestInnerModel[] InnerModels { get; set; } = new TestInnerModel[0];
[ModelBinder(BinderType = typeof(NumberModelBinder))]
public decimal TopLevelRate { get; set; }
}
public class TestInnerModel
{
public TestInnerModel()
{
}
[ModelBinder(BinderType = typeof(NumberModelBinder))]
public decimal Rate { get; set; }
}
The custom model binder (intentionally simplified to do nothing special):
public class NumberModelBinder : IModelBinder
{
private readonly NumberStyles _supportedStyles = NumberStyles.Float | NumberStyles.AllowThousands;
private DecimalModelBinder _innerBinder;
public NumberModelBinder(ILoggerFactory loggerFactory)
{
_innerBinder = new DecimalModelBinder(_supportedStyles, loggerFactory);
}
/// <inheritdoc />
public Task BindModelAsync(ModelBindingContext bindingContext)
{
return _innerBinder.BindModelAsync(bindingContext);
}
}
The controller:
public class HomeController : Controller
{
public IActionResult Index()
{
return View(new TestModel
{
TopLevelRate = 20m,
InnerModels = new TestInnerModel[]
{
new TestInnerModel { Rate = 2.0m },
new TestInnerModel { Rate = 0.2m }
}
});
}
[HttpPost]
public IActionResult Index(TestModel model)
{
return Ok();
}
}
The Razor view:
#model TestModel;
<form asp-controller="Home" asp-action="Index" method="post" role="form">
<div>
<input asp-for="#Model.TopLevelRate" type="number" min="0" step=".01" />
</div>
<div>
#for (var i = 0; i < Model.InnerModels.Length; i++)
{
<input asp-for="#Model.InnerModels[i].Rate" type="number" min="0" step=".01" />
}
</div>
<input type="submit" value="Go" />
</form>
A solution was posted to the GitHub issue:
#Costo The problem is you're not informing the model binding system that the binder uses value providers. The ComplexTypeModelBinder always believes data is available for the next TestInnerModel instance and the outermost binder (CollectionModelBinder) keeps going -- forever. To fix this,
[MyModelBinder]
public decimal Rate { get; set; }
private class MyModelBinderAttribute : ModelBinderAttribute
{
public MyModelBinderAttribute()
: base(typeof(NumberModelBinder))
{
BindingSource = BindingSource.Form;
}
}
Put another way, the BindingSource.Custom default [ModelBinder] uses isn't correct in this scenario. Fortunately custom model binders on properties of POCO types in containers should be one of the very few cases where this matters.

How to implement validations using annotations in struts2 in action that implements ModelDriven interface?

I am trying to implement validation using annotations, but these validations don't work and the form gets submitted with null values.
I don't know what is missing, how to implement validations using annotations in struts2 in action that implements ModelDriven interface?
This is my struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="saveOrUpdateUser" method="saveOrUpdate1" class="com.vaannila.web.Userqqqq">
<result name="success" type="redirect">listUser</result>
<result name="input">/register.jsp</result>
</action>
<action name="listUser" method="list" class="com.vaannila.web.Userqqqq">
<result name="success">/register.jsp</result>
<result name="input">/register.jsp</result>
</action>
<action name="editUser" method="edit" class="com.vaannila.web.Userqqqq">
<result name="success">/register.jsp</result>
<result name="input">/register.jsp</result>
</action>
<action name="deleteUser" method="delete" class="com.vaannila.web.Userqqqq">
<result name="success" type="redirect">listUser</result>
<result name="input">/register.jsp</result>
</action>
</package>
</struts>
This is my jsp page
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%#taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Page</title>
<s:head />
<style type="text/css">
#import url(style.css);
</style>
</head>
<body>
<s:actionerror/>
<s:form action="saveOrUpdateUser" validate="true">
<s:push value="user">
<s:hidden name="id" />
<s:textfield name="name" label="User Name" required="true"/>
<s:radio name="gender" label="Gender" list="{'Male','Female'}" />
<s:select name="country" list="{'India','USA','UK'}" headerKey=""
headerValue="Select" label="Select a country" />
<s:textarea name="aboutYou" label="About You" />
<s:checkbox name="mailingList"
label="Would you like to join our mailing list?" />
<s:submit />
</s:push>
</s:form>
<s:if test="userList.size() > 0">
<div class="content">
<table class="userTable" cellpadding="5px">
<tr class="even">
<th>Name</th>
<th>Gender</th>
<th>Country</th>
<th>About You</th>
<th>Mailing List</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<s:iterator value="userList" status="userStatus">
<tr
class="<s:if test="#userStatus.odd == true ">odd</s:if><s:else>even</s:else>">
<td><s:property value="name" /></td>
<td><s:property value="gender" /></td>
<td><s:property value="country" /></td>
<td><s:property value="aboutYou" /></td>
<td><s:property value="mailingList" /></td>
<td><s:url id="editURL" action="editUser">
<s:param name="id" value="%{id}"></s:param>
</s:url> <s:a href="%{editURL}">Edit</s:a></td>
<td><s:url id="deleteURL" action="deleteUser">
<s:param name="id" value="%{id}"></s:param>
</s:url> <s:a href="%{deleteURL}">Delete</s:a></td>
</tr>
</s:iterator>
</table>
</div>
</s:if>
</body>
</html>
This is my action class
package com.vaannila.web;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.vaannila.dao.UserDAO;
import com.vaannila.domain.User;
public class UserAction extends ActionSupport implements ModelDriven<User> {
private static final long serialVersionUID = -6659925652584240539L;
private User user = new User();
private List<User> userList = new ArrayList<User>();
private UserDAO userDAO = new UserDAO();
#Override
public User getModel() {
return user;
}
/**
* To save or update user.
* #return String
*/
public String saveOrUpdate()
{
System.out.println(user.getName());
userDAO.saveOrUpdateUser(user);
return SUCCESS;
}
/**
* To list all users.
* #return String
*/
public String list()
{
userList = userDAO.listUser();
return SUCCESS;
}
/**
* To delete a user.
* #return String
*/
public String delete()
{
HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
userDAO.deleteUser(Long.parseLong(request.getParameter("id")));
return SUCCESS;
}
/**
* To list a single user by Id.
* #return String
*/
public String edit()
{
HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
user = userDAO.listUserById(Long.parseLong(request.getParameter("id")));
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}
This is my BEAN
package com.vaannila.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
#Entity
#Table(name="USER1")
public class User {
private Long id;
private String name;
private String gender;
private String country;
private String aboutYou;
private Boolean mailingList;
#Id
#GeneratedValue
#Column(name="USER_ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#RequiredFieldValidator(type = ValidatorType.SIMPLE, fieldName = "name", message = "User Name field is empty.")
#Column(name="USER_NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#RequiredFieldValidator(type = ValidatorType.SIMPLE, fieldName = "gender", message = "gender field is empty.")
#Column(name="USER_GENDER")
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
#Column(name="USER_COUNTRY")
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
#Column(name="USER_ABOUT_YOU")
public String getAboutYou() {
return aboutYou;
}
public void setAboutYou(String aboutYou) {
this.aboutYou = aboutYou;
}
#Column(name="USER_MAILING_LIST")
public Boolean getMailingList() {
return mailingList;
}
public void setMailingList(Boolean mailingList) {
this.mailingList = mailingList;
}
}
You need to add #VisitorFieldValidator annotation to user getter inside your action class.
#VisitorFieldValidator(appendPrefix = false)
public User getUser() {
return user;
}
Note that you need to set appendPrefix to false if your are using field names without user. prefix in JSP.
Also, you are probably want #RequiredStringValidator not #RequiredFieldValidator inside your bean.
#RequiredStringValidator(message = "User Name field is empty.")
#Column(name="USER_NAME")
public String getName() {
return name;
}

Can we call methods with parameters in Struts2 fieldexpression?

I've configured my sturts2 application to use the validation xml for my actions. I also have fieldexpression working.
Would it be possible to call a method from my action in the expression.
eg:
<field name="myField">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[
#com.test.MyClass#isCaptchaOk(keyString, userResponce)
]]></param>
<message>My credit limit should be MORE than my girlfriend's</message>
</field-validator>
</field>
Here is my actual test code, the simple fieldexpression works, but function call one does not (see the tbox1).
I'm not sure if the #class#method path is ok or not, but is not working
coz, I've added log in the functions but nothing comes up, so i presume the validator can't reach the functions.
Also, Is this possible, ie is it allowed or am i being too ambitious.
Thanks
PS I've corrected the message, I'm not trading my girlfriend ;-)
**** validation.xml
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="tbox1">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[#uk.co.nhbc.userRegistration.action.Test2Action#getString()]]></param>
<message>function call message here</message>
</field-validator>
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[#uk.co.nhbc.userRegistration.action.Test2Action#isCaptchaOk(tbox1, user.username)]]></param>
<message>function call message here</message>
</field-validator>
</field>
<field name="tbox2">
<field-validator type="stringlength">
<param name="maxLength">5</param>
<message>length messssage here</message>
</field-validator>
</field>
<field name="user.username">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[(!(tbox2 == "aa" && user.username.equals("")))]]></param>
<message>tbox2 eq aa and username is empty messssage2 here</message>
</field-validator>
</field>
</validators>
******* java class
package uk.co.nhbc.userRegistration.action;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import uk.co.nhbc.common.action.BaseAction;
import uk.co.nhbc.userRegistration.model.Users;
public class Test2Action extends BaseAction {
private String tbox1;
private String tbox2;
private Users user;
private static final Log log = LogFactory.getLog(Test2Action.class);
public String execute() {
return SUCCESS;
}
public String getTbox2() {
return tbox2;
}
public void setTbox2(String tbox2) {
this.tbox2 = tbox2;
}
public String getTbox1() {
return tbox1;
}
public void setTbox1(String tbox1) {
this.tbox1 = tbox1;
}
public Users getUser() {
log.debug("get user called");
return user;
}
public void setUser(Users user) {
log.debug("set user called");
this.user = user;
}
public boolean isCaptchaOk(String challenge, String response) {
//dummy test function
log.debug("captcha function called");
if (response.equals("true"))
return true;
return false;
}
public String getString (){
log.debug("getString function called");
return "hello";
}
}
*********and jsp page
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib prefix="sj" uri="/struts-jquery-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form name="formtest" action="Test2Action">
<s:actionerror/>
<s:fielderror></s:fielderror>
<s:textfield name="tbox1" label="box1"></s:textfield>
<s:textfield name="tbox2" label="box1"></s:textfield>
<s:textfield name="user.username" label="boxuser"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
In order to calling a static method in expression, which must be OGNL, you should enable struts.ognl.allowStaticMethodAccess by adding below constant to struts.xml file:
< constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
See my updated working validator file. for field tbox1 in the fieldexpression I'm referring to the method directly as it is my action, which would be on the VS.
tbox1 and user.username are items on the jsp page (and also exist in the action)
I tried to experiment with a static method, but that didn't work, (no time to investigate now).
Hope this helps
and thanks dave for the input.
***updated validation xml
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="tbox1">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[isCaptchaOk(tbox1, user.username)]]></param>
<message>function call message here</message>
</field-validator>
</field>
<field name="tbox2">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[(#uk.co.nhbc.userRegistration.action.Test2Action#isFuncOk(tbox2))]]></param>
<message>func okk function call message here</message>
</field-validator>
</field>
<field name="user.username">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[(!(tbox2 == "aa" && user.username.equals("")))]]></param>
<message>tbox2 eq aa and username is empty messssage2 here</message>
</field-validator>
</field>
</validators>
***updated java class
package uk.co.nhbc.userRegistration.action;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import uk.co.nhbc.common.action.BaseAction;
import uk.co.nhbc.userRegistration.model.Users;
public class Test2Action extends BaseAction {
private String tbox1;
private String tbox2;
private Users user;
private static final Log log = LogFactory.getLog(Test2Action.class);
public String execute() {
return SUCCESS;
}
public String getTbox2() {
return tbox2;
}
public void setTbox2(String tbox2) {
this.tbox2 = tbox2;
}
public String getTbox1() {
return tbox1;
}
public void setTbox1(String tbox1) {
this.tbox1 = tbox1;
}
public Users getUser() {
log.debug("get user called");
return user;
}
public void setUser(Users user) {
log.debug("set user called");
this.user = user;
}
public boolean isCaptchaOk(String challenge, String response) {
//dummy test function
log.debug("captcha function called");
log.debug("captcha function called"+challenge+response);
if (response.equals("true"))
return true;
return false;
}
public String getString (){
log.debug("getString function called");
return "hello";
}
public static boolean isFuncOk (String response){
log.debug("isFuncOk function called"+response);
if (response.equals("true"))
return true;
return false;
}
}

Struts2 + Jquery Library - What is wrong on this AJAX call?

Im trying some ajax call with Struts2 and the JQuery library. This is the code of the jsp :
<s:div id="my_result" >
Nothing
</s:div>
<s:form action="UserManager" theme="simple">
<s:textfield name="nickname" />
<s:password name="password" />
<s:label cssClass="menu_span">
<sj:submit targets="my_result" value="Login" />
</s:label>
</s:form>
this is the code of the bean/action with the xml :
public class UserManager extends ActionSupport {
private String nickname;
private String password;
#Override
public String execute() throws Exception {
String output="i have insered "+this.getNickname()+" and "+this.getPassword();
System.out.println(output);
return output;
}
public String getNickname() { return nickname; }
public void setNickname(String newValue) { nickname=newValue; }
public String getPassword() { return password; }
public void setPassword(String newValue) { password=newValue; }
}
<package name="model" extends="struts-default">
<action name="UserManager" class="model.UserManager">
<result>index.jsp</result>
</action>
</package>
If i return SUCCESS on the UserManager bean, it load on my_result the same calling page. Else, if i return output i get Messages:
No result defined for action model.UserManager and result i have insered aaa and bbb
messagge (with nickname=aaa and password=bbb).
What's happened? How can i return that string? I think it's a struts.xml fault
Cheers
UPDATE
public class UserManager extends ActionSupport {
private String nickname;
private String password;
private String error;
#Override
public String execute() throws Exception {
error="i have insered "+this.getNickname()+" and "+this.getPassword();
return SUCCESS;
}
public String getNickname() { return nickname; }
public void setNickname(String newValue) { nickname=newValue; }
public String getPassword() { return password; }
public void setPassword(String newValue) { password=newValue; }
public String getError() { return error; }
public void setError(String newValue) { error=newValue; }
}
<s:div id="my_result" >
<s:property value="error" />
</s:div>
<s:form action="UserManager" theme="simple">
<s:textfield name="nickname" />
<s:password name="password" />
<s:label cssClass="menu_span">
<sj:submit targets="my_result" value="Login" />
</s:label>
</s:form>
UPDATE 2
<package name="model" extends="json-default">
<action name="UserManager" class="model.UserManager">
<result type="json" />
</action>
</package>
The return value that you return in the execute() method must be one of the mapped as values.
As you have not a result name like this:
<result name="resultName"></result>
It will use the default one: "success". You have to return "success" value in execute() method.
The result value of the execute method is the name of the result mapped. To send a message you have to add a String attribute in your Action (and a getter to obtain it), and in the execute method do something like
this.message = "my message";
Edit: how to get the message with ajax:
If you have set the message in your action in an attribute called "String message" and you have a getter for that attribute (public String getMessage()) you can get this information accesing this property in the object you get in the ajax call. If you do the ajax call for example with jquery you can get it in this way:
$.ajax({
url: '/yourAjaxUrl',
success: function(data) {
var message = data.message;
alert('The message is '+message);
$('#my_result').html(message);
}
});
Edit:
In the struts xml in your package tag you have to define the json type:
<result-types>
<result-type name="json" class="com.googlecode.jsonplugin.JSONResult">
...
</result-types>
and add the json-plugin.jar which can be downloaded here
more info in http://code.google.com/p/jsonplugin/wiki/Documentation

Resources