我已阅读此文并在我的应用中使用它 article link
我想添加日期验证,以查看如果此人超过18
我很难理解如何以及在何处实施 validate.js 要求的解析和格式化方法。我正在使用react native并实现其余的验证,如文章中所述。
答案 0 :(得分:0)
我使用<?php
namespace Application\Sonata\UserBundle\Controller;
use Sonata\UserBundle\Controller\ProfileFOSUser1Controller as BaseController;
/**
* Overwrite methods from the ProfileFOSUser1Controller if you want to change the behavior
* for the current profile
*
*/
class ProfileUserController extends BaseController
{
/**
* @throws AccessDeniedException
*
* @return Response|RedirectResponse
*/
public function editAuthenticationAction()
{
echo "here"; die;
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
$form = $this->get('sonata.user.authentication.form');
$formHandler = $this->get('sonata.user.authentication.form_handler');
$process = $formHandler->process($user);
if ($process) {
$this->setFlash('sonata_user_success', 'profile.flash.updated');
return $this->redirect($this->generateUrl('sonata_user_profile_show'));
}
return $this->render('SonataUserBundle:Profile:edit_authentication.html.twig', [
'form' => $form->createView(),
]);
}
}
进行生日验证
moment.js
这是我为生日验证所做的功能,并且在某些组件中使用它,只需导入并使用即可。如果没有返回错误,则用户超过18岁,否则它将在返回语句中返回一些错误。
编辑:我这样使用它。 creted utils文件夹和validateAge.js里面(里面是上面那段代码) 并且在您验证年龄的形式中,您可以导入该功能
import moment from 'moment';
export const isOverEighteen = (date) => {
var eighteenYearsAgo = moment().subtract(18, "years");
var birthday = moment(date);
if (!birthday.isValid()) {
return "invalid date";
}
else if (!eighteenYearsAgo.isAfter(birthday)) {
return "To successfully open an account you have to be at least 18 years old.";
}
return;
};
并使用这样设置dateOfBirth的错误(在我的情况下是属性名称)
import { isOverEighteen } from 'here-goes-your-path/utils/validateAge';